summaryrefslogtreecommitdiff
path: root/build/gen_stub.php
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2020-09-24 15:16:07 +0200
committerNikita Popov <nikita.ppv@gmail.com>2020-09-24 15:16:29 +0200
commit9428e161a822177728669826e6bb1664c9d20184 (patch)
tree185168f4f4d58fcab649f1a86a55a9b714a36791 /build/gen_stub.php
parent555e7eccc4f2391046b76caeb8219904f092f843 (diff)
downloadphp-git-9428e161a822177728669826e6bb1664c9d20184.tar.gz
Add option to print parameter name stats to gen_stub
Diffstat (limited to 'build/gen_stub.php')
-rwxr-xr-xbuild/gen_stub.php44
1 files changed, 35 insertions, 9 deletions
diff --git a/build/gen_stub.php b/build/gen_stub.php
index 6cbfb06b27..bd8b33759f 100755
--- a/build/gen_stub.php
+++ b/build/gen_stub.php
@@ -12,7 +12,7 @@ use PhpParser\PrettyPrinterAbstract;
error_reporting(E_ALL);
-function processDirectory(string $dir, bool $forceRegeneration) {
+function processDirectory(string $dir, Context $context) {
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY
@@ -20,12 +20,12 @@ function processDirectory(string $dir, bool $forceRegeneration) {
foreach ($it as $file) {
$pathName = $file->getPathName();
if (preg_match('/\.stub\.php$/', $pathName)) {
- processStubFile($pathName, $forceRegeneration);
+ processStubFile($pathName, $context);
}
}
}
-function processStubFile(string $stubFile, bool $forceRegeneration) {
+function processStubFile(string $stubFile, Context $context) {
try {
if (!file_exists($stubFile)) {
throw new Exception("File $stubFile does not exist");
@@ -35,7 +35,7 @@ function processStubFile(string $stubFile, bool $forceRegeneration) {
$stubCode = file_get_contents($stubFile);
$stubHash = computeStubHash($stubCode);
$oldStubHash = extractStubHash($arginfoFile);
- if ($stubHash === $oldStubHash && $forceRegeneration === false) {
+ if ($stubHash === $oldStubHash && $context->forceRegeneration === false) {
/* Stub file did not change, do not regenerate. */
return;
}
@@ -44,6 +44,16 @@ function processStubFile(string $stubFile, bool $forceRegeneration) {
$fileInfo = parseStubFile($stubCode);
$arginfoCode = generateArgInfoCode($fileInfo, $stubHash);
file_put_contents($arginfoFile, $arginfoCode);
+
+ // Collect parameter name statistics.
+ foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
+ foreach ($funcInfo->args as $argInfo) {
+ if (!isset($context->parameterStats[$argInfo->name])) {
+ $context->parameterStats[$argInfo->name] = 0;
+ }
+ $context->parameterStats[$argInfo->name]++;
+ }
+ }
} catch (Exception $e) {
echo "In $stubFile:\n{$e->getMessage()}\n";
exit(1);
@@ -67,6 +77,13 @@ function extractStubHash(string $arginfoFile): ?string {
return $matches[1];
}
+class Context {
+ /** @var bool */
+ public $forceRegeneration = false;
+ /** @var array */
+ public $parameterStats = [];
+}
+
class SimpleType {
/** @var string */
public $name;
@@ -1131,16 +1148,25 @@ function initPhpParser() {
}
$optind = null;
-$options = getopt("f", ["force-regeneration"], $optind);
-$forceRegeneration = isset($options["f"]) || isset($options["force-regeneration"]);
-$location = $argv[$optind] ?? ".";
+$options = getopt("f", ["force-regeneration", "parameter-stats"], $optind);
+
+$context = new Context;
+$printParameterStats = isset($options["parameter-stats"]);
+$context->forceRegeneration =
+ isset($options["f"]) || isset($options["force-regeneration"]) || $printParameterStats;
+$location = $argv[$optind] ?? ".";
if (is_file($location)) {
// Generate single file.
- processStubFile($location, $forceRegeneration);
+ processStubFile($location, $context);
} else if (is_dir($location)) {
- processDirectory($location, $forceRegeneration);
+ processDirectory($location, $context);
} else {
echo "$location is neither a file nor a directory.\n";
exit(1);
}
+
+if ($printParameterStats) {
+ arsort($context->parameterStats);
+ echo json_encode($context->parameterStats, JSON_PRETTY_PRINT), "\n";
+}