12
12
13
13
error_reporting (E_ALL );
14
14
15
- function processDirectory (string $ dir , bool $ forceRegeneration ) {
15
+ function processDirectory (string $ dir , Context $ context ) {
16
16
$ it = new RecursiveIteratorIterator (
17
17
new RecursiveDirectoryIterator ($ dir ),
18
18
RecursiveIteratorIterator::LEAVES_ONLY
19
19
);
20
20
foreach ($ it as $ file ) {
21
21
$ pathName = $ file ->getPathName ();
22
22
if (preg_match ('/\.stub\.php$/ ' , $ pathName )) {
23
- processStubFile ($ pathName , $ forceRegeneration );
23
+ processStubFile ($ pathName , $ context );
24
24
}
25
25
}
26
26
}
27
27
28
- function processStubFile (string $ stubFile , bool $ forceRegeneration ) {
28
+ function processStubFile (string $ stubFile , Context $ context ) {
29
29
try {
30
30
if (!file_exists ($ stubFile )) {
31
31
throw new Exception ("File $ stubFile does not exist " );
@@ -35,7 +35,7 @@ function processStubFile(string $stubFile, bool $forceRegeneration) {
35
35
$ stubCode = file_get_contents ($ stubFile );
36
36
$ stubHash = computeStubHash ($ stubCode );
37
37
$ oldStubHash = extractStubHash ($ arginfoFile );
38
- if ($ stubHash === $ oldStubHash && $ forceRegeneration === false ) {
38
+ if ($ stubHash === $ oldStubHash && $ context -> forceRegeneration === false ) {
39
39
/* Stub file did not change, do not regenerate. */
40
40
return ;
41
41
}
@@ -44,6 +44,16 @@ function processStubFile(string $stubFile, bool $forceRegeneration) {
44
44
$ fileInfo = parseStubFile ($ stubCode );
45
45
$ arginfoCode = generateArgInfoCode ($ fileInfo , $ stubHash );
46
46
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
+ }
47
57
} catch (Exception $ e ) {
48
58
echo "In $ stubFile: \n{$ e ->getMessage ()}\n" ;
49
59
exit (1 );
@@ -67,6 +77,13 @@ function extractStubHash(string $arginfoFile): ?string {
67
77
return $ matches [1 ];
68
78
}
69
79
80
+ class Context {
81
+ /** @var bool */
82
+ public $ forceRegeneration = false ;
83
+ /** @var array */
84
+ public $ parameterStats = [];
85
+ }
86
+
70
87
class SimpleType {
71
88
/** @var string */
72
89
public $ name ;
@@ -1131,16 +1148,25 @@ function initPhpParser() {
1131
1148
}
1132
1149
1133
1150
$ 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 ;
1137
1157
1158
+ $ location = $ argv [$ optind ] ?? ". " ;
1138
1159
if (is_file ($ location )) {
1139
1160
// Generate single file.
1140
- processStubFile ($ location , $ forceRegeneration );
1161
+ processStubFile ($ location , $ context );
1141
1162
} else if (is_dir ($ location )) {
1142
- processDirectory ($ location , $ forceRegeneration );
1163
+ processDirectory ($ location , $ context );
1143
1164
} else {
1144
1165
echo "$ location is neither a file nor a directory. \n" ;
1145
1166
exit (1 );
1146
1167
}
1168
+
1169
+ if ($ printParameterStats ) {
1170
+ arsort ($ context ->parameterStats );
1171
+ echo json_encode ($ context ->parameterStats , JSON_PRETTY_PRINT ), "\n" ;
1172
+ }
0 commit comments