]> granicus.if.org Git - php/commitdiff
Add option to print parameter name stats to gen_stub
authorNikita Popov <nikita.ppv@gmail.com>
Thu, 24 Sep 2020 13:16:07 +0000 (15:16 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Thu, 24 Sep 2020 13:16:29 +0000 (15:16 +0200)
build/gen_stub.php
ext/spl/spl_fixedarray_arginfo.h

index 6cbfb06b276b4f1c667aca4f4a14e9ea01034aea..bd8b33759fcdbe5a454d8cc4df205ce597c68fd4 100755 (executable)
@@ -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";
+}
index 6817380c6bfa5b475084dcd29a8b4ff1cb2d6023..e82ea83a6a76c7474d23ab5a9afdd04f2151a824 100644 (file)
@@ -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")