Skip to content

Verify parameter names of function aliases #6335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 85 additions & 25 deletions build/gen_stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,30 @@

error_reporting(E_ALL);

function processDirectory(string $dir, Context $context) {
/**
* @return FileInfo[]
*/
function processDirectory(string $dir, Context $context): array {
$fileInfos = [];

$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($it as $file) {
$pathName = $file->getPathName();
if (preg_match('/\.stub\.php$/', $pathName)) {
processStubFile($pathName, $context);
$fileInfo = processStubFile($pathName, $context);
if ($fileInfo) {
$fileInfos[] = $fileInfo;
}
}
}

return $fileInfos;
}

function processStubFile(string $stubFile, Context $context) {
function processStubFile(string $stubFile, Context $context): ?FileInfo {
try {
if (!file_exists($stubFile)) {
throw new Exception("File $stubFile does not exist");
Expand All @@ -39,13 +49,13 @@ function processStubFile(string $stubFile, Context $context) {
$oldStubHash = extractStubHash($arginfoFile);
if ($stubHash === $oldStubHash && $context->forceRegeneration === false) {
/* Stub file did not change, do not regenerate. */
return;
return null;
}

initPhpParser();
$fileInfo = parseStubFile($stubCode);
$arginfoCode = generateArgInfoCode($fileInfo, $stubHash);
if (file_put_contents($arginfoFile, $arginfoCode)) {
if ($context->forceRegeneration === true && file_put_contents($arginfoFile, $arginfoCode)) {
echo "Saved $arginfoFile\n";
}

Expand All @@ -54,20 +64,12 @@ function processStubFile(string $stubFile, Context $context) {
$funcInfo->discardInfoForOldPhpVersions();
}
$arginfoCode = generateArgInfoCode($fileInfo, $stubHash);
if (file_put_contents($legacyFile, $arginfoCode)) {
if ($context->forceRegeneration === true && file_put_contents($legacyFile, $arginfoCode)) {
echo "Saved $legacyFile\n";
}
}

// 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]++;
}
}
return $fileInfo;
} catch (Exception $e) {
echo "In $stubFile:\n{$e->getMessage()}\n";
exit(1);
Expand All @@ -92,10 +94,8 @@ function extractStubHash(string $arginfoFile): ?string {
}

class Context {
/** @var bool */
/** @var bool|null */
public $forceRegeneration = false;
/** @var array */
public $parameterStats = [];
}

class SimpleType {
Expand Down Expand Up @@ -1262,29 +1262,89 @@ function initPhpParser() {
}

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

$context = new Context;
$printParameterStats = isset($options["parameter-stats"]);
$verify = isset($options["verify"]);
$context->forceRegeneration =
isset($options["f"]) || isset($options["force-regeneration"]) || $printParameterStats;
isset($options["f"]) || isset($options["force-regeneration"]) ? true : ($printParameterStats || $verify ? null : false);

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

$fileInfos = [];
$location = $argv[$optind] ?? ".";
if (is_file($location)) {
// Generate single file.
processStubFile($location, $context);
$fileInfo = processStubFile($location, $context);
if ($fileInfo) {
$fileInfos[] = $fileInfo;
}
} else if (is_dir($location)) {
processDirectory($location, $context);
$fileInfos = 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";
$parameterStats = [];

foreach ($fileInfos as $fileInfo) {
foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
foreach ($funcInfo->args as $argInfo) {
if (!isset($context->parameterStats[$argInfo->name])) {
$parameterStats[$argInfo->name] = 0;
}
$parameterStats[$argInfo->name]++;
}
}
}

arsort($parameterStats);
echo json_encode($parameterStats, JSON_PRETTY_PRINT), "\n";
}

if ($verify) {
$errors = [];
$funcMap = [];
$aliases = [];

foreach ($fileInfos as $fileInfo) {
foreach ($fileInfo->getAllFuncInfos() as $funcInfo) {
/** @var FuncInfo $funcInfo */
$funcMap[$funcInfo->name->__toString()] = $funcInfo;

if ($funcInfo->aliasType === "alias") {
$aliases[] = $funcInfo;
}
}
}

foreach ($aliases as $alias) {
if ($alias->alias === null || !isset($funcMap[$alias->alias->__toString()])) {
$errors[] = "Aliased function {$alias->alias}() cannot be found";
continue;
}

$aliasedFunc = $funcMap[$alias->alias->__toString()];

$aliasedArgMap = [];
foreach ($aliasedFunc->args as $arg) {
$aliasedArgMap[$arg->name] = $arg;
}

foreach ($alias->args as $arg) {
if (!isset($aliasedArgMap[$arg->name])) {
$errors[] = "{$alias->name}(): Argument \$$arg->name of aliased function {$aliasedFunc->name}() is missing";
}
}
}

echo implode("\n", $errors), "\n";
if (!empty($errors)) {
exit(1);
}
}
4 changes: 2 additions & 2 deletions ext/date/php_date.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function date_time_set(

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

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

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

Expand Down Expand Up @@ -291,7 +291,7 @@ public function getTimestamp() {}
* @return DateInterval|false
* @alias date_diff
*/
public function diff(DateTimeInterface $object, bool $absolute = false) {}
public function diff(DateTimeInterface $targetObject, bool $absolute = false) {}

/** @return DateTimeImmutable|false */
public function modify(string $modifier) {}
Expand Down
9 changes: 3 additions & 6 deletions ext/date/php_date_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 561c6ad41ccedfc8b778d3b64323c7154dffcdb5 */
* Stub hash: 2129f4a8e4f6011ecdbda8b780d77d40512ad2d6 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_strtotime, 0, 1, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_ARG_TYPE_INFO(0, datetime, IS_STRING, 0)
Expand Down Expand Up @@ -143,7 +143,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_date_isodate_set, 0, 3, DateTime,
ZEND_ARG_OBJ_INFO(0, object, DateTime, 0)
ZEND_ARG_TYPE_INFO(0, year, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, week, IS_LONG, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, day, IS_LONG, 0, "1")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, dayOfWeek, IS_LONG, 0, "1")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_date_timestamp_set, 0, 2, DateTime, 0)
Expand Down Expand Up @@ -335,10 +335,7 @@ ZEND_END_ARG_INFO()

#define arginfo_class_DateTimeImmutable_getTimestamp arginfo_class_DateTimeInterface_getTimezone

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_DateTimeImmutable_diff, 0, 0, 1)
ZEND_ARG_OBJ_INFO(0, object, DateTimeInterface, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, absolute, _IS_BOOL, 0, "false")
ZEND_END_ARG_INFO()
#define arginfo_class_DateTimeImmutable_diff arginfo_class_DateTimeInterface_diff

#define arginfo_class_DateTimeImmutable_modify arginfo_class_DateTime_modify

Expand Down
4 changes: 2 additions & 2 deletions ext/intl/calendar/calendar.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ public function isLenient() {}
public function isWeekend(?float $timestamp = null) {}

/**
* @param int|bool $amountOrUpOrDown
* @param int|bool $value
* @return bool
* @alias intlcal_roll
*/
public function roll(int $field, $amountOrUpOrDown) {}
public function roll(int $field, $value) {}

/**
* @return bool
Expand Down
4 changes: 2 additions & 2 deletions ext/intl/calendar/calendar_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: cac6d4040481ab7de4775315079b1d28a44cbcac */
* Stub hash: ee755d4a500e2d1ba4d589c233b7d09a06b5cce8 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_IntlCalendar___construct, 0, 0, 0)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -106,7 +106,7 @@ ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_IntlCalendar_roll, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, field, IS_LONG, 0)
ZEND_ARG_INFO(0, amountOrUpOrDown)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()

#define arginfo_class_IntlCalendar_isSet arginfo_class_IntlCalendar_get
Expand Down
6 changes: 3 additions & 3 deletions ext/intl/php_intl.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function intlcal_set_repeated_wall_time_option(IntlCalendar $calendar, int $opti

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

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

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

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

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

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

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

Expand Down Expand Up @@ -349,7 +349,7 @@ function resourcebundle_create(?string $locale, ?string $bundle, bool $fallback
* @param string|int $index
* @return mixed
*/
function resourcebundle_get(ResourceBundle $bundle, $index) {}
function resourcebundle_get(ResourceBundle $bundle, $index, bool $fallback = true) {}

function resourcebundle_count(ResourceBundle $bundle): int {}

Expand Down
11 changes: 4 additions & 7 deletions ext/intl/php_intl_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 8340252225ea91a68fe61657f20c08a1876949c8 */
* Stub hash: c890e3cde79ffeade4623001cc369aa734812959 */

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_intlcal_create_instance, 0, 0, IntlCalendar, 1)
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, timezone, "null")
Expand Down Expand Up @@ -162,7 +162,7 @@ ZEND_END_ARG_INFO()
#define arginfo_intlcal_set_skipped_wall_time_option arginfo_intlcal_set_repeated_wall_time_option

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_intlcal_from_date_time, 0, 1, IntlCalendar, 1)
ZEND_ARG_OBJ_TYPE_MASK(0, dateTime, DateTime, MAY_BE_STRING, NULL)
ZEND_ARG_OBJ_TYPE_MASK(0, datetime, DateTime, MAY_BE_STRING, NULL)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, locale, IS_STRING, 1, "null")
ZEND_END_ARG_INFO()

Expand Down Expand Up @@ -242,11 +242,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_collator_sort_with_sort_keys, 0,
ZEND_ARG_TYPE_INFO(1, array, IS_ARRAY, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_collator_asort, 0, 2, _IS_BOOL, 0)
ZEND_ARG_OBJ_INFO(0, object, Collator, 0)
ZEND_ARG_TYPE_INFO(1, arr, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "Collator::SORT_REGULAR")
ZEND_END_ARG_INFO()
#define arginfo_collator_asort arginfo_collator_sort

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_collator_get_locale, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_OBJ_INFO(0, object, Collator, 0)
Expand Down Expand Up @@ -637,6 +633,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_resourcebundle_get, 0, 0, 2)
ZEND_ARG_OBJ_INFO(0, bundle, ResourceBundle, 0)
ZEND_ARG_INFO(0, index)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, fallback, _IS_BOOL, 0, "true")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_resourcebundle_count, 0, 1, IS_LONG, 0)
Expand Down
2 changes: 1 addition & 1 deletion ext/intl/timezone/timezone.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function getDSTSavings() {}
* @return string|false
* @alias intltz_get_equivalent_id
*/
public static function getEquivalentID(string $timezoneId, int $index) {}
public static function getEquivalentID(string $timezoneId, int $offset) {}

/**
* @return int|false
Expand Down
4 changes: 2 additions & 2 deletions ext/intl/timezone/timezone_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 6bdd65d7ba5a32b32c96511e391beb876d9d20c3 */
* Stub hash: afd0e74b29d54cde9789787b924af9b43539a7f4 */

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_IntlTimeZone___construct, 0, 0, 0)
ZEND_END_ARG_INFO()
Expand Down Expand Up @@ -41,7 +41,7 @@ ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_class_IntlTimeZone_getEquivalentID, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, timezoneId, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, index, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, offset, IS_LONG, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_IntlTimeZone_getErrorCode arginfo_class_IntlTimeZone___construct
Expand Down
7 changes: 3 additions & 4 deletions ext/mysqli/mysqli.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function dump_debug_info() {}
* @return bool
* @alias mysqli_debug
*/
public function debug(string $options) {}
public function debug(string $message) {}

/**
* @return object|null
Expand Down Expand Up @@ -112,7 +112,6 @@ public function get_warnings() {}

/**
* @return mysqli|false
* @alias mysqli_init_method
*/
public function init() {}

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

function mysqli_dump_debug_info(mysqli $mysql): bool {}

function mysqli_debug(string $debug): bool {}
function mysqli_debug(string $message): bool {}

function mysqli_errno(mysqli $mysql): int {}

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

function mysqli_fetch_fields(mysqli_result $result): array {}

function mysqli_fetch_field_direct(mysqli_result $result, int $offset): object|false {}
function mysqli_fetch_field_direct(mysqli_result $result, int $index): object|false {}

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

Expand Down
7 changes: 0 additions & 7 deletions ext/mysqli/mysqli_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1482,13 +1482,6 @@ PHP_FUNCTION(mysqli_init)
}
/* }}} */

/* {{{ Initialize mysqli and return a resource for use with mysql_real_connect */
PHP_FUNCTION(mysqli_init_method)
{
php_mysqli_init(INTERNAL_FUNCTION_PARAM_PASSTHRU, TRUE);
}
/* }}} */

/* {{{ Get the ID generated from the previous INSERT operation */
PHP_FUNCTION(mysqli_insert_id)
{
Expand Down
Loading