Skip to content

Commit ec7a0ad

Browse files
committed
Verify parameter names of function aliases
1 parent 9b4094c commit ec7a0ad

17 files changed

+144
-79
lines changed

build/gen_stub.php

Lines changed: 85 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,30 @@
1212

1313
error_reporting(E_ALL);
1414

15-
function processDirectory(string $dir, Context $context) {
15+
/**
16+
* @return FileInfo[]
17+
*/
18+
function processDirectory(string $dir, Context $context): array {
19+
$fileInfos = [];
20+
1621
$it = new RecursiveIteratorIterator(
1722
new RecursiveDirectoryIterator($dir),
1823
RecursiveIteratorIterator::LEAVES_ONLY
1924
);
2025
foreach ($it as $file) {
2126
$pathName = $file->getPathName();
2227
if (preg_match('/\.stub\.php$/', $pathName)) {
23-
processStubFile($pathName, $context);
28+
$fileInfo = processStubFile($pathName, $context);
29+
if ($fileInfo) {
30+
$fileInfos[] = $fileInfo;
31+
}
2432
}
2533
}
34+
35+
return $fileInfos;
2636
}
2737

28-
function processStubFile(string $stubFile, Context $context) {
38+
function processStubFile(string $stubFile, Context $context): ?FileInfo {
2939
try {
3040
if (!file_exists($stubFile)) {
3141
throw new Exception("File $stubFile does not exist");
@@ -39,13 +49,13 @@ function processStubFile(string $stubFile, Context $context) {
3949
$oldStubHash = extractStubHash($arginfoFile);
4050
if ($stubHash === $oldStubHash && $context->forceRegeneration === false) {
4151
/* Stub file did not change, do not regenerate. */
42-
return;
52+
return null;
4353
}
4454

4555
initPhpParser();
4656
$fileInfo = parseStubFile($stubCode);
4757
$arginfoCode = generateArgInfoCode($fileInfo, $stubHash);
48-
if (file_put_contents($arginfoFile, $arginfoCode)) {
58+
if ($context->forceRegeneration === true && file_put_contents($arginfoFile, $arginfoCode)) {
4959
echo "Saved $arginfoFile\n";
5060
}
5161

@@ -54,20 +64,12 @@ function processStubFile(string $stubFile, Context $context) {
5464
$funcInfo->discardInfoForOldPhpVersions();
5565
}
5666
$arginfoCode = generateArgInfoCode($fileInfo, $stubHash);
57-
if (file_put_contents($legacyFile, $arginfoCode)) {
67+
if ($context->forceRegeneration === true && file_put_contents($legacyFile, $arginfoCode)) {
5868
echo "Saved $legacyFile\n";
5969
}
6070
}
6171

62-
// Collect parameter name statistics.
63-
foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
64-
foreach ($funcInfo->args as $argInfo) {
65-
if (!isset($context->parameterStats[$argInfo->name])) {
66-
$context->parameterStats[$argInfo->name] = 0;
67-
}
68-
$context->parameterStats[$argInfo->name]++;
69-
}
70-
}
72+
return $fileInfo;
7173
} catch (Exception $e) {
7274
echo "In $stubFile:\n{$e->getMessage()}\n";
7375
exit(1);
@@ -92,10 +94,8 @@ function extractStubHash(string $arginfoFile): ?string {
9294
}
9395

9496
class Context {
95-
/** @var bool */
97+
/** @var bool|null */
9698
public $forceRegeneration = false;
97-
/** @var array */
98-
public $parameterStats = [];
9999
}
100100

101101
class SimpleType {
@@ -1262,29 +1262,89 @@ function initPhpParser() {
12621262
}
12631263

12641264
$optind = null;
1265-
$options = getopt("fh", ["force-regeneration", "parameter-stats", "help"], $optind);
1265+
$options = getopt("fh", ["force-regeneration", "parameter-stats", "help", "verify"], $optind);
12661266

12671267
$context = new Context;
12681268
$printParameterStats = isset($options["parameter-stats"]);
1269+
$verify = isset($options["verify"]);
12691270
$context->forceRegeneration =
1270-
isset($options["f"]) || isset($options["force-regeneration"]) || $printParameterStats;
1271+
isset($options["f"]) || isset($options["force-regeneration"]) ? true : ($printParameterStats || $verify ? null : false);
12711272

12721273
if (isset($options["h"]) || isset($options["help"])) {
1273-
die("\nusage: gen-stub.php [ -f | --force-regeneration ] [ --parameter-stats ] [ -h | --help ] [ name.stub.php | directory ]\n\n");
1274+
die("\nusage: gen-stub.php [ -f | --force-regeneration ] [ --parameter-stats ] [ --verify ] [ -h | --help ] [ name.stub.php | directory ]\n\n");
12741275
}
12751276

1277+
$fileInfos = [];
12761278
$location = $argv[$optind] ?? ".";
12771279
if (is_file($location)) {
12781280
// Generate single file.
1279-
processStubFile($location, $context);
1281+
$fileInfo = processStubFile($location, $context);
1282+
if ($fileInfo) {
1283+
$fileInfos[] = $fileInfo;
1284+
}
12801285
} else if (is_dir($location)) {
1281-
processDirectory($location, $context);
1286+
$fileInfos = processDirectory($location, $context);
12821287
} else {
12831288
echo "$location is neither a file nor a directory.\n";
12841289
exit(1);
12851290
}
12861291

12871292
if ($printParameterStats) {
1288-
arsort($context->parameterStats);
1289-
echo json_encode($context->parameterStats, JSON_PRETTY_PRINT), "\n";
1293+
$parameterStats = [];
1294+
1295+
foreach ($fileInfos as $fileInfo) {
1296+
foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
1297+
foreach ($funcInfo->args as $argInfo) {
1298+
if (!isset($context->parameterStats[$argInfo->name])) {
1299+
$parameterStats[$argInfo->name] = 0;
1300+
}
1301+
$parameterStats[$argInfo->name]++;
1302+
}
1303+
}
1304+
}
1305+
1306+
arsort($parameterStats);
1307+
echo json_encode($parameterStats, JSON_PRETTY_PRINT), "\n";
1308+
}
1309+
1310+
if ($verify) {
1311+
$errors = [];
1312+
$funcMap = [];
1313+
$aliases = [];
1314+
1315+
foreach ($fileInfos as $fileInfo) {
1316+
foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
1317+
/** @var FuncInfo $funcInfo */
1318+
$funcMap[$funcInfo->name->__toString()] = $funcInfo;
1319+
1320+
if ($funcInfo->aliasType === "alias") {
1321+
$aliases[] = $funcInfo;
1322+
}
1323+
}
1324+
}
1325+
1326+
foreach ($aliases as $alias) {
1327+
if ($alias->alias === null || !isset($funcMap[$alias->alias->__toString()])) {
1328+
$errors[] = "Aliased function {$alias->alias}() cannot be found";
1329+
continue;
1330+
}
1331+
1332+
$aliasedFunc = $funcMap[$alias->alias->__toString()];
1333+
1334+
$aliasedArgMap = [];
1335+
foreach ($aliasedFunc->args as $arg) {
1336+
$aliasedArgMap[$arg->name] = $arg;
1337+
}
1338+
1339+
foreach ($alias->args as $arg) {
1340+
if (!isset($aliasedArgMap[$arg->name])) {
1341+
$errors[] = "{$alias->name}(): Argument \$$arg->name of aliased function {$aliasedFunc->name}() is missing";
1342+
}
1343+
}
1344+
}
1345+
1346+
echo implode("\n", $errors), "\n";
1347+
if (!empty($errors)) {
1348+
exit(1);
1349+
}
12901350
}

ext/date/php_date.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function date_time_set(
6969

7070
function date_date_set(DateTime $object, int $year, int $month, int $day): DateTime {}
7171

72-
function date_isodate_set(DateTime $object, int $year, int $week, int $day = 1): DateTime {}
72+
function date_isodate_set(DateTime $object, int $year, int $week, int $dayOfWeek = 1): DateTime {}
7373

7474
function date_timestamp_set(DateTime $object, int $timestamp): DateTime {}
7575

@@ -291,7 +291,7 @@ public function getTimestamp() {}
291291
* @return DateInterval|false
292292
* @alias date_diff
293293
*/
294-
public function diff(DateTimeInterface $object, bool $absolute = false) {}
294+
public function diff(DateTimeInterface $targetObject, bool $absolute = false) {}
295295

296296
/** @return DateTimeImmutable|false */
297297
public function modify(string $modifier) {}

ext/date/php_date_arginfo.h

Lines changed: 3 additions & 6 deletions
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: 561c6ad41ccedfc8b778d3b64323c7154dffcdb5 */
2+
* Stub hash: 2129f4a8e4f6011ecdbda8b780d77d40512ad2d6 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strtotime, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)
55
ZEND_ARG_TYPE_INFO(0, datetime, IS_STRING, 0)
@@ -143,7 +143,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_date_isodate_set, 0, 3, DateTime,
143143
ZEND_ARG_OBJ_INFO(0, object, DateTime, 0)
144144
ZEND_ARG_TYPE_INFO(0, year, IS_LONG, 0)
145145
ZEND_ARG_TYPE_INFO(0, week, IS_LONG, 0)
146-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, day, IS_LONG, 0, "1")
146+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, dayOfWeek, IS_LONG, 0, "1")
147147
ZEND_END_ARG_INFO()
148148

149149
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_date_timestamp_set, 0, 2, DateTime, 0)
@@ -335,10 +335,7 @@ ZEND_END_ARG_INFO()
335335

336336
#define arginfo_class_DateTimeImmutable_getTimestamp arginfo_class_DateTimeInterface_getTimezone
337337

338-
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_DateTimeImmutable_diff, 0, 0, 1)
339-
ZEND_ARG_OBJ_INFO(0, object, DateTimeInterface, 0)
340-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, absolute, _IS_BOOL, 0, "false")
341-
ZEND_END_ARG_INFO()
338+
#define arginfo_class_DateTimeImmutable_diff arginfo_class_DateTimeInterface_diff
342339

343340
#define arginfo_class_DateTimeImmutable_modify arginfo_class_DateTime_modify
344341

ext/intl/calendar/calendar.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,11 @@ public function isLenient() {}
218218
public function isWeekend(?float $timestamp = null) {}
219219

220220
/**
221-
* @param int|bool $amountOrUpOrDown
221+
* @param int|bool $value
222222
* @return bool
223223
* @alias intlcal_roll
224224
*/
225-
public function roll(int $field, $amountOrUpOrDown) {}
225+
public function roll(int $field, $value) {}
226226

227227
/**
228228
* @return bool

ext/intl/calendar/calendar_arginfo.h

Lines changed: 2 additions & 2 deletions
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: cac6d4040481ab7de4775315079b1d28a44cbcac */
2+
* Stub hash: ee755d4a500e2d1ba4d589c233b7d09a06b5cce8 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_IntlCalendar___construct, 0, 0, 0)
55
ZEND_END_ARG_INFO()
@@ -106,7 +106,7 @@ ZEND_END_ARG_INFO()
106106

107107
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_IntlCalendar_roll, 0, 0, 2)
108108
ZEND_ARG_TYPE_INFO(0, field, IS_LONG, 0)
109-
ZEND_ARG_INFO(0, amountOrUpOrDown)
109+
ZEND_ARG_INFO(0, value)
110110
ZEND_END_ARG_INFO()
111111

112112
#define arginfo_class_IntlCalendar_isSet arginfo_class_IntlCalendar_get

ext/intl/php_intl.stub.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function intlcal_set_repeated_wall_time_option(IntlCalendar $calendar, int $opti
8989

9090
function intlcal_set_skipped_wall_time_option(IntlCalendar $calendar, int $option): bool {}
9191

92-
function intlcal_from_date_time(DateTime|string $dateTime, ?string $locale = null): ?IntlCalendar {}
92+
function intlcal_from_date_time(DateTime|string $datetime, ?string $locale = null): ?IntlCalendar {}
9393

9494
function intlcal_to_date_time(IntlCalendar $calendar): DateTime|false {}
9595

@@ -131,7 +131,7 @@ function collator_sort(Collator $object, array &$array, int $flags = Collator::S
131131

132132
function collator_sort_with_sort_keys(Collator $object, array &$array): bool {}
133133

134-
function collator_asort(Collator $object, array &$arr, int $flags = Collator::SORT_REGULAR): bool {}
134+
function collator_asort(Collator $object, array &$array, int $flags = Collator::SORT_REGULAR): bool {}
135135

136136
function collator_get_locale(Collator $object, int $type): string|false {}
137137

@@ -349,7 +349,7 @@ function resourcebundle_create(?string $locale, ?string $bundle, bool $fallback
349349
* @param string|int $index
350350
* @return mixed
351351
*/
352-
function resourcebundle_get(ResourceBundle $bundle, $index) {}
352+
function resourcebundle_get(ResourceBundle $bundle, $index, bool $fallback = true) {}
353353

354354
function resourcebundle_count(ResourceBundle $bundle): int {}
355355

ext/intl/php_intl_arginfo.h

Lines changed: 4 additions & 7 deletions
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: 8340252225ea91a68fe61657f20c08a1876949c8 */
2+
* Stub hash: c890e3cde79ffeade4623001cc369aa734812959 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_intlcal_create_instance, 0, 0, IntlCalendar, 1)
55
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, timezone, "null")
@@ -162,7 +162,7 @@ ZEND_END_ARG_INFO()
162162
#define arginfo_intlcal_set_skipped_wall_time_option arginfo_intlcal_set_repeated_wall_time_option
163163

164164
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_intlcal_from_date_time, 0, 1, IntlCalendar, 1)
165-
ZEND_ARG_OBJ_TYPE_MASK(0, dateTime, DateTime, MAY_BE_STRING, NULL)
165+
ZEND_ARG_OBJ_TYPE_MASK(0, datetime, DateTime, MAY_BE_STRING, NULL)
166166
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, locale, IS_STRING, 1, "null")
167167
ZEND_END_ARG_INFO()
168168

@@ -242,11 +242,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_collator_sort_with_sort_keys, 0,
242242
ZEND_ARG_TYPE_INFO(1, array, IS_ARRAY, 0)
243243
ZEND_END_ARG_INFO()
244244

245-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_collator_asort, 0, 2, _IS_BOOL, 0)
246-
ZEND_ARG_OBJ_INFO(0, object, Collator, 0)
247-
ZEND_ARG_TYPE_INFO(1, arr, IS_ARRAY, 0)
248-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "Collator::SORT_REGULAR")
249-
ZEND_END_ARG_INFO()
245+
#define arginfo_collator_asort arginfo_collator_sort
250246

251247
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_collator_get_locale, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
252248
ZEND_ARG_OBJ_INFO(0, object, Collator, 0)
@@ -637,6 +633,7 @@ ZEND_END_ARG_INFO()
637633
ZEND_BEGIN_ARG_INFO_EX(arginfo_resourcebundle_get, 0, 0, 2)
638634
ZEND_ARG_OBJ_INFO(0, bundle, ResourceBundle, 0)
639635
ZEND_ARG_INFO(0, index)
636+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, fallback, _IS_BOOL, 0, "true")
640637
ZEND_END_ARG_INFO()
641638

642639
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_resourcebundle_count, 0, 1, IS_LONG, 0)

ext/intl/timezone/timezone.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function getDSTSavings() {}
6666
* @return string|false
6767
* @alias intltz_get_equivalent_id
6868
*/
69-
public static function getEquivalentID(string $timezoneId, int $index) {}
69+
public static function getEquivalentID(string $timezoneId, int $offset) {}
7070

7171
/**
7272
* @return int|false

ext/intl/timezone/timezone_arginfo.h

Lines changed: 2 additions & 2 deletions
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: 6bdd65d7ba5a32b32c96511e391beb876d9d20c3 */
2+
* Stub hash: afd0e74b29d54cde9789787b924af9b43539a7f4 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_IntlTimeZone___construct, 0, 0, 0)
55
ZEND_END_ARG_INFO()
@@ -41,7 +41,7 @@ ZEND_END_ARG_INFO()
4141

4242
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_IntlTimeZone_getEquivalentID, 0, 0, 2)
4343
ZEND_ARG_TYPE_INFO(0, timezoneId, IS_STRING, 0)
44-
ZEND_ARG_TYPE_INFO(0, index, IS_LONG, 0)
44+
ZEND_ARG_TYPE_INFO(0, offset, IS_LONG, 0)
4545
ZEND_END_ARG_INFO()
4646

4747
#define arginfo_class_IntlTimeZone_getErrorCode arginfo_class_IntlTimeZone___construct

ext/mysqli/mysqli.stub.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function dump_debug_info() {}
7676
* @return bool
7777
* @alias mysqli_debug
7878
*/
79-
public function debug(string $options) {}
79+
public function debug(string $message) {}
8080

8181
/**
8282
* @return object|null
@@ -112,7 +112,6 @@ public function get_warnings() {}
112112

113113
/**
114114
* @return mysqli|false
115-
* @alias mysqli_init_method
116115
*/
117116
public function init() {}
118117

@@ -551,7 +550,7 @@ function mysqli_data_seek(mysqli_result $result, int $offset): bool {}
551550

552551
function mysqli_dump_debug_info(mysqli $mysql): bool {}
553552

554-
function mysqli_debug(string $debug): bool {}
553+
function mysqli_debug(string $message): bool {}
555554

556555
function mysqli_errno(mysqli $mysql): int {}
557556

@@ -568,7 +567,7 @@ function mysqli_fetch_field(mysqli_result $result): object|false {}
568567

569568
function mysqli_fetch_fields(mysqli_result $result): array {}
570569

571-
function mysqli_fetch_field_direct(mysqli_result $result, int $offset): object|false {}
570+
function mysqli_fetch_field_direct(mysqli_result $result, int $index): object|false {}
572571

573572
function mysqli_fetch_lengths(mysqli_result $result): array|false {}
574573

ext/mysqli/mysqli_api.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,13 +1482,6 @@ PHP_FUNCTION(mysqli_init)
14821482
}
14831483
/* }}} */
14841484

1485-
/* {{{ Initialize mysqli and return a resource for use with mysql_real_connect */
1486-
PHP_FUNCTION(mysqli_init_method)
1487-
{
1488-
php_mysqli_init(INTERNAL_FUNCTION_PARAM_PASSTHRU, TRUE);
1489-
}
1490-
/* }}} */
1491-
14921485
/* {{{ Get the ID generated from the previous INSERT operation */
14931486
PHP_FUNCTION(mysqli_insert_id)
14941487
{

0 commit comments

Comments
 (0)