From 9428e161a822177728669826e6bb1664c9d20184 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 24 Sep 2020 15:16:07 +0200 Subject: [PATCH] Add option to print parameter name stats to gen_stub --- build/gen_stub.php | 44 +++++++++++++++++++++++++------- ext/spl/spl_fixedarray_arginfo.h | 2 +- 2 files changed, 36 insertions(+), 10 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"; +} diff --git a/ext/spl/spl_fixedarray_arginfo.h b/ext/spl/spl_fixedarray_arginfo.h index 6817380c6b..e82ea83a6a 100644 --- a/ext/spl/spl_fixedarray_arginfo.h +++ b/ext/spl/spl_fixedarray_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 2075619a330ed636ce7f7c85aeb208a8f9f55ca7 */ + * Stub hash: c820bad6bcfcc7c60a221464008a882515b5c05e */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_SplFixedArray___construct, 0, 0, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, size, IS_LONG, 0, "0") -- 2.40.0