Skip to content

Commit 9428e16

Browse files
committed
Add option to print parameter name stats to gen_stub
1 parent 555e7ec commit 9428e16

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

build/gen_stub.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212

1313
error_reporting(E_ALL);
1414

15-
function processDirectory(string $dir, bool $forceRegeneration) {
15+
function processDirectory(string $dir, Context $context) {
1616
$it = new RecursiveIteratorIterator(
1717
new RecursiveDirectoryIterator($dir),
1818
RecursiveIteratorIterator::LEAVES_ONLY
1919
);
2020
foreach ($it as $file) {
2121
$pathName = $file->getPathName();
2222
if (preg_match('/\.stub\.php$/', $pathName)) {
23-
processStubFile($pathName, $forceRegeneration);
23+
processStubFile($pathName, $context);
2424
}
2525
}
2626
}
2727

28-
function processStubFile(string $stubFile, bool $forceRegeneration) {
28+
function processStubFile(string $stubFile, Context $context) {
2929
try {
3030
if (!file_exists($stubFile)) {
3131
throw new Exception("File $stubFile does not exist");
@@ -35,7 +35,7 @@ function processStubFile(string $stubFile, bool $forceRegeneration) {
3535
$stubCode = file_get_contents($stubFile);
3636
$stubHash = computeStubHash($stubCode);
3737
$oldStubHash = extractStubHash($arginfoFile);
38-
if ($stubHash === $oldStubHash && $forceRegeneration === false) {
38+
if ($stubHash === $oldStubHash && $context->forceRegeneration === false) {
3939
/* Stub file did not change, do not regenerate. */
4040
return;
4141
}
@@ -44,6 +44,16 @@ function processStubFile(string $stubFile, bool $forceRegeneration) {
4444
$fileInfo = parseStubFile($stubCode);
4545
$arginfoCode = generateArgInfoCode($fileInfo, $stubHash);
4646
file_put_contents($arginfoFile, $arginfoCode);
47+
48+
// Collect parameter name statistics.
49+
foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
50+
foreach ($funcInfo->args as $argInfo) {
51+
if (!isset($context->parameterStats[$argInfo->name])) {
52+
$context->parameterStats[$argInfo->name] = 0;
53+
}
54+
$context->parameterStats[$argInfo->name]++;
55+
}
56+
}
4757
} catch (Exception $e) {
4858
echo "In $stubFile:\n{$e->getMessage()}\n";
4959
exit(1);
@@ -67,6 +77,13 @@ function extractStubHash(string $arginfoFile): ?string {
6777
return $matches[1];
6878
}
6979

80+
class Context {
81+
/** @var bool */
82+
public $forceRegeneration = false;
83+
/** @var array */
84+
public $parameterStats = [];
85+
}
86+
7087
class SimpleType {
7188
/** @var string */
7289
public $name;
@@ -1131,16 +1148,25 @@ function initPhpParser() {
11311148
}
11321149

11331150
$optind = null;
1134-
$options = getopt("f", ["force-regeneration"], $optind);
1135-
$forceRegeneration = isset($options["f"]) || isset($options["force-regeneration"]);
1136-
$location = $argv[$optind] ?? ".";
1151+
$options = getopt("f", ["force-regeneration", "parameter-stats"], $optind);
1152+
1153+
$context = new Context;
1154+
$printParameterStats = isset($options["parameter-stats"]);
1155+
$context->forceRegeneration =
1156+
isset($options["f"]) || isset($options["force-regeneration"]) || $printParameterStats;
11371157

1158+
$location = $argv[$optind] ?? ".";
11381159
if (is_file($location)) {
11391160
// Generate single file.
1140-
processStubFile($location, $forceRegeneration);
1161+
processStubFile($location, $context);
11411162
} else if (is_dir($location)) {
1142-
processDirectory($location, $forceRegeneration);
1163+
processDirectory($location, $context);
11431164
} else {
11441165
echo "$location is neither a file nor a directory.\n";
11451166
exit(1);
11461167
}
1168+
1169+
if ($printParameterStats) {
1170+
arsort($context->parameterStats);
1171+
echo json_encode($context->parameterStats, JSON_PRETTY_PRINT), "\n";
1172+
}

ext/spl/spl_fixedarray_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 2075619a330ed636ce7f7c85aeb208a8f9f55ca7 */
2+
* Stub hash: c820bad6bcfcc7c60a221464008a882515b5c05e */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SplFixedArray___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, size, IS_LONG, 0, "0")

0 commit comments

Comments
 (0)