From 4b334734d2e37a99b16bf58b7457ce87dd1fa624 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Sun, 15 Nov 2026 11:48:44 +0000 Subject: [PATCH 1/4] Reject objects for array parameters PHP 8.6 deprecates them for PHP 8.6 deprecates passing objects that are interpreted as arrays to array_walk(), array_walk_recursive(), deflate_init(), inflate_init(), http_build_query() and mb_convert_variables(). functionMap_php86delta.php drops object from those parameter types. Merging a function map signature with its php-8-stubs signature widened such a type back to the native array|object. A parameter whose type the last version delta changes now keeps the delta's type when the native type accepts it. deflate_init() and inflate_init() accept objects since PHP 7.0, which the base function map now says too. The tests copy the php-src tests of the implementation commits. Amp-Thread-ID: https://ampcode.com/threads/T-01a1def5-2aa3-73fc-be3e-a45c7762cafb --- resources/functionMap.php | 4 +- resources/functionMap_php86delta.php | 34 ++++ .../FunctionSignatureMapProvider.php | 119 ++++++++--- .../SignatureMap/Php8SignatureMapProvider.php | 34 +++- .../CallToFunctionParametersRulePhp86Test.php | 187 ++++++++++++++++++ .../data/bug-15369-array-walk-php84.php | 48 +++++ .../Functions/data/bug-15369-array-walk.php | 170 ++++++++++++++++ .../data/bug-15369-http-build-query.php | 146 ++++++++++++++ .../data/bug-15369-mb-convert-variables.php | 119 +++++++++++ .../Functions/data/bug-15369-zlib-bz2.php | 99 ++++++++++ 10 files changed, 918 insertions(+), 42 deletions(-) create mode 100644 resources/functionMap_php86delta.php create mode 100644 tests/PHPStan/Rules/Functions/data/bug-15369-array-walk-php84.php create mode 100644 tests/PHPStan/Rules/Functions/data/bug-15369-array-walk.php create mode 100644 tests/PHPStan/Rules/Functions/data/bug-15369-http-build-query.php create mode 100644 tests/PHPStan/Rules/Functions/data/bug-15369-mb-convert-variables.php create mode 100644 tests/PHPStan/Rules/Functions/data/bug-15369-zlib-bz2.php diff --git a/resources/functionMap.php b/resources/functionMap.php index 5c9baee1cb5..8ca6ed515f0 100644 --- a/resources/functionMap.php +++ b/resources/functionMap.php @@ -1423,7 +1423,7 @@ 'define_syslog_variables' => ['void'], 'defined' => ['bool', 'name'=>'string'], 'deflate_add' => ['string|false', 'context'=>'resource', 'data'=>'string', 'flush_mode='=>'int'], -'deflate_init' => ['resource|false', 'encoding'=>'int', 'options='=>'array'], +'deflate_init' => ['resource|false', 'encoding'=>'int', 'options='=>'array|object'], 'deg2rad' => ['float', 'number'=>'float'], 'dgettext' => ['string', 'domain_name'=>'string', 'msgid'=>'string'], 'dio_close' => ['void', 'fd'=>'resource'], @@ -4721,7 +4721,7 @@ 'inflate_add' => ['string|false', 'context'=>'resource', 'encoded_data'=>'string', 'flush_mode='=>'int'], 'inflate_get_read_len' => ['int|false', 'resource'=>'resource'], 'inflate_get_status' => ['int|false', 'resource'=>'resource'], -'inflate_init' => ['resource|false', 'encoding'=>'int', 'options='=>'array'], +'inflate_init' => ['resource|false', 'encoding'=>'int', 'options='=>'array|object'], 'ingres_autocommit' => ['bool', 'link'=>'resource'], 'ingres_autocommit_state' => ['bool', 'link'=>'resource'], 'ingres_charset' => ['string', 'link'=>'resource'], diff --git a/resources/functionMap_php86delta.php b/resources/functionMap_php86delta.php new file mode 100644 index 00000000000..22181e6205b --- /dev/null +++ b/resources/functionMap_php86delta.php @@ -0,0 +1,34 @@ + [ + // https://wiki.php.net/rfc/deprecations_php_8_6#passing_objects_which_are_interpreted_as_arrays + 'array_walk' => ['bool', '&rw_input'=>'array', 'callback'=>'callable', 'userdata='=>'mixed'], + 'array_walk_recursive' => ['bool', '&rw_input'=>'array', 'callback'=>'callable', 'userdata='=>'mixed'], + 'deflate_init' => ['resource|false', 'encoding'=>'int', 'options='=>'array'], + 'http_build_query' => ['string', 'querydata'=>'array', 'prefix='=>'string', 'arg_separator='=>'string', 'enc_type='=>'int'], + 'inflate_init' => ['resource|false', 'encoding'=>'int', 'options='=>'array'], + 'mb_convert_variables' => ['string|false', 'to_encoding'=>'string', 'from_encoding'=>'array|string', '&rw_vars'=>'string|array', '&...rw_vars='=>'string|array'], + ], + 'old' => [ + 'array_walk' => ['bool', '&rw_input'=>'array|object', 'callback'=>'callable', 'userdata='=>'mixed'], + 'array_walk_recursive' => ['bool', '&rw_input'=>'array|object', 'callback'=>'callable', 'userdata='=>'mixed'], + 'deflate_init' => ['resource|false', 'encoding'=>'int', 'options='=>'array|object'], + 'http_build_query' => ['string', 'querydata'=>'array|object', 'prefix='=>'string', 'arg_separator='=>'string', 'enc_type='=>'int'], + 'inflate_init' => ['resource|false', 'encoding'=>'int', 'options='=>'array|object'], + 'mb_convert_variables' => ['string|false', 'to_encoding'=>'string', 'from_encoding'=>'array|string', '&rw_vars'=>'string|array|object', '&...rw_vars='=>'string|array|object'], + ], +]; diff --git a/src/Reflection/SignatureMap/FunctionSignatureMapProvider.php b/src/Reflection/SignatureMap/FunctionSignatureMapProvider.php index 70f2c28b622..b4de245e4f1 100644 --- a/src/Reflection/SignatureMap/FunctionSignatureMapProvider.php +++ b/src/Reflection/SignatureMap/FunctionSignatureMapProvider.php @@ -17,6 +17,8 @@ use function array_change_key_case; use function array_key_exists; use function array_keys; +use function array_slice; +use function array_values; use function is_array; use function sprintf; use function strtolower; @@ -38,6 +40,9 @@ final class FunctionSignatureMapProvider implements SignatureMapProvider */ private static array $arenaSignatureRows = []; + /** @var array>> */ + private static array $parametersChangedByDelta = []; + /** @var array|null */ private static ?array $functionMetadata = null; @@ -276,60 +281,114 @@ public function getSignatureMap(): array $signatureMap = array_change_key_case($signatureMap, CASE_LOWER); - if ($this->stricterFunctionMap) { - $signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_bleedingEdge.php'); + foreach ($this->getSignatureMapDeltaFiles() as $file) { + $signatureMap = $this->computeSignatureMap($signatureMap, $this->loadSignatureMapDelta($file)); } - if ($this->phpVersion->getVersionId() >= 70400) { - $signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php74delta.php'); + self::$signatureMaps[$cacheKey] = $signatureMap; + ArenaCache::publishHash('sigmap-' . $cacheKey, $signatureMap); + + return $signatureMap; + } + + /** + * Positions of the parameters whose type the last delta replacing the signature changes. + * + * Such a type describes exactly what the parameter accepts, so it may leave out members + * of the native union type, like objects that PHP still accepts but deprecates. + * Other function map types leave them in, because the native type can be newer than them. + * + * @return list + */ + public function getParametersChangedByDelta(string $name): array + { + $cacheKey = sprintf('%d-%d', $this->phpVersion->getVersionId(), $this->stricterFunctionMap ? 1 : 0); + if (!array_key_exists($cacheKey, self::$parametersChangedByDelta)) { + $changedParameters = []; + foreach ($this->getSignatureMapDeltaFiles() as $file) { + $delta = $this->loadSignatureMapDelta($file); + foreach (array_keys($delta['old'] + $delta['new']) as $key) { + unset($changedParameters[strtolower($key)]); + } + foreach ($delta['new'] as $key => $signature) { + if (!array_key_exists($key, $delta['old'])) { + continue; + } + + $oldParameterTypes = array_values(array_slice($delta['old'][$key], 1)); + $positions = []; + foreach (array_values(array_slice($signature, 1)) as $i => $parameterType) { + if (($oldParameterTypes[$i] ?? null) === $parameterType) { + continue; + } + + $positions[] = $i; + } + + $changedParameters[strtolower($key)] = $positions; + } + } + + self::$parametersChangedByDelta[$cacheKey] = $changedParameters; } - if ($this->phpVersion->getVersionId() >= 80000) { - $signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php80delta.php'); + return self::$parametersChangedByDelta[$cacheKey][strtolower($name)] ?? []; + } + /** + * @return list + */ + private function getSignatureMapDeltaFiles(): array + { + $directory = __DIR__ . '/../../../resources'; + $versionId = $this->phpVersion->getVersionId(); + $files = []; + if ($this->stricterFunctionMap) { + $files[] = $directory . '/functionMap_bleedingEdge.php'; + } + if ($versionId >= 70400) { + $files[] = $directory . '/functionMap_php74delta.php'; + } + if ($versionId >= 80000) { + $files[] = $directory . '/functionMap_php80delta.php'; if ($this->stricterFunctionMap) { - $signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php80delta_bleedingEdge.php'); + $files[] = $directory . '/functionMap_php80delta_bleedingEdge.php'; } } - - if ($this->phpVersion->getVersionId() >= 80100) { - $signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php81delta.php'); + if ($versionId >= 80100) { + $files[] = $directory . '/functionMap_php81delta.php'; } - - if ($this->phpVersion->getVersionId() >= 80200) { - $signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php82delta.php'); + if ($versionId >= 80200) { + $files[] = $directory . '/functionMap_php82delta.php'; } - - if ($this->phpVersion->getVersionId() >= 80300) { - $signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php83delta.php'); + if ($versionId >= 80300) { + $files[] = $directory . '/functionMap_php83delta.php'; } - - if ($this->phpVersion->getVersionId() >= 80400) { - $signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php84delta.php'); + if ($versionId >= 80400) { + $files[] = $directory . '/functionMap_php84delta.php'; } - - if ($this->phpVersion->getVersionId() >= 80500) { - $signatureMap = $this->computeSignatureMapFile($signatureMap, __DIR__ . '/../../../resources/functionMap_php85delta.php'); + if ($versionId >= 80500) { + $files[] = $directory . '/functionMap_php85delta.php'; + } + if ($versionId >= 80600) { + $files[] = $directory . '/functionMap_php86delta.php'; } - self::$signatureMaps[$cacheKey] = $signatureMap; - ArenaCache::publishHash('sigmap-' . $cacheKey, $signatureMap); - - return $signatureMap; + return $files; } /** - * @param array $signatureMap - * @return array + * @return array{new: array, old: array} */ - private function computeSignatureMapFile(array $signatureMap, string $file): array + private function loadSignatureMapDelta(string $file): array { $signatureMapDelta = include $file; if (!is_array($signatureMapDelta)) { throw new ShouldNotHappenException(sprintf('Signature map file "%s" could not be loaded.', $file)); } - return $this->computeSignatureMap($signatureMap, $signatureMapDelta); + /** @var array{new: array, old: array} $signatureMapDelta */ + return $signatureMapDelta; } /** diff --git a/src/Reflection/SignatureMap/Php8SignatureMapProvider.php b/src/Reflection/SignatureMap/Php8SignatureMapProvider.php index 34e69363775..27c5403928f 100644 --- a/src/Reflection/SignatureMap/Php8SignatureMapProvider.php +++ b/src/Reflection/SignatureMap/Php8SignatureMapProvider.php @@ -32,6 +32,7 @@ use function array_map; use function count; use function explode; +use function in_array; use function is_string; use function sprintf; use function strtolower; @@ -274,7 +275,7 @@ private function getMethodSignaturesWithoutTentativeReturnType(string $className if ($this->functionSignatureMapProvider->hasMethodSignature($className, $methodName)) { $functionMapSignatures = $this->functionSignatureMapProvider->getMethodSignatures($className, $methodName, $reflectionMethod); - return $this->getMergedSignatures($signature, $functionMapSignatures); + return $this->getMergedSignatures($signature, $functionMapSignatures, $this->functionSignatureMapProvider->getParametersChangedByDelta(sprintf('%s::%s', $className, $methodName))); } return ['positional' => [$signature], 'named' => null]; @@ -293,7 +294,7 @@ public function getFunctionSignatures(string $functionName, ?string $className, if ($this->functionSignatureMapProvider->hasFunctionSignature($functionName)) { $functionMapSignatures = $this->functionSignatureMapProvider->getFunctionSignatures($functionName, $className, $reflectionFunction); - return $this->getMergedSignatures($signature, $functionMapSignatures); + return $this->getMergedSignatures($signature, $functionMapSignatures, $this->functionSignatureMapProvider->getParametersChangedByDelta($functionName)); } return ['positional' => [$signature], 'named' => null]; @@ -366,12 +367,13 @@ private function hasDeprecatedAttribute(array $attrGroups): bool /** * @param array{positional: array, named: ?array} $functionMapSignatures + * @param list $parametersChangedByDelta * @return array{positional: array, named: ?array} */ - private function getMergedSignatures(FunctionSignature $nativeSignature, array $functionMapSignatures): array + private function getMergedSignatures(FunctionSignature $nativeSignature, array $functionMapSignatures, array $parametersChangedByDelta): array { if (count($functionMapSignatures['positional']) === 1) { - return ['positional' => [$this->mergeSignatures($nativeSignature, $functionMapSignatures['positional'][0])], 'named' => null]; + return ['positional' => [$this->mergeSignatures($nativeSignature, $functionMapSignatures['positional'][0], $parametersChangedByDelta)], 'named' => null]; } if (count($functionMapSignatures['positional']) === 0) { @@ -425,7 +427,10 @@ private function withUnknownDefaultValues(FunctionSignature $nativeSignature, Fu ); } - private function mergeSignatures(FunctionSignature $nativeSignature, FunctionSignature $functionMapSignature): FunctionSignature + /** + * @param list $parametersChangedByDelta + */ + private function mergeSignatures(FunctionSignature $nativeSignature, FunctionSignature $functionMapSignature, array $parametersChangedByDelta): FunctionSignature { $parameters = []; foreach ($nativeSignature->getParameters() as $i => $nativeParameter) { @@ -436,16 +441,25 @@ private function mergeSignatures(FunctionSignature $nativeSignature, FunctionSig $functionMapParameter = $functionMapSignature->getParameters()[$i]; $nativeParameterType = $nativeParameter->getNativeType(); - $parameters[] = new ParameterSignature( - $nativeParameter->getName(), - $nativeParameter->isOptional(), - TypehintHelper::decideType( + if ( + in_array($i, $parametersChangedByDelta, true) + && $nativeParameterType->isSuperTypeOf($functionMapParameter->getType())->yes() + ) { + // unlike decideType(), keeps out members of the native union type the delta leaves out + $parameterType = $functionMapParameter->getType(); + } else { + $parameterType = TypehintHelper::decideType( $nativeParameterType, TypehintHelper::decideType( $nativeParameter->getType(), $functionMapParameter->getType(), ), - ), + ); + } + $parameters[] = new ParameterSignature( + $nativeParameter->getName(), + $nativeParameter->isOptional(), + $parameterType, $nativeParameterType, $nativeParameter->passedByReference()->yes() ? $functionMapParameter->passedByReference() : $nativeParameter->passedByReference(), $nativeParameter->isVariadic(), diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRulePhp86Test.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRulePhp86Test.php index d2057375e62..e4afda633ac 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRulePhp86Test.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRulePhp86Test.php @@ -56,6 +56,193 @@ public function testBug15372(): void ]); } + public function testBug15369ArrayWalk(): void + { + $this->analyse([__DIR__ . '/data/bug-15369-array-walk.php'], [ + [ + 'Parameter #1 $array of function array_walk expects array, TypeError given.', + 10, + ], + [ + 'Parameter #1 $array of function array_walk expects array, FFI\CData given.', + 23, + ], + [ + 'Parameter #1 $array of function array_walk expects array, Bug15369ArrayWalk\ArrayWalkObject1\MyClass given.', + 54, + ], + [ + 'Parameter #1 $array of function array_walk expects array, stdClass given.', + 74, + ], + [ + 'Parameter #1 $array of function array_walk expects array, Bug15369ArrayWalk\ArrayWalkObjects\test given.', + 77, + ], + [ + 'Parameter #1 $array of function array_walk expects array, string given.', + 83, + ], + [ + 'Parameter #1 $array of function array_walk_recursive expects array, stdClass given.', + 106, + ], + [ + 'Parameter #1 $array of function array_walk_recursive expects array, Bug15369ArrayWalk\ArrayWalkRecObjects\test given.', + 109, + ], + [ + 'Parameter #1 $array of function array_walk_recursive expects array, string given.', + 115, + ], + [ + 'Parameter #1 $array of function array_walk_recursive expects array, Bug15369ArrayWalk\ArrayWalkRecursiveObject1\MyClass given.', + 149, + ], + [ + 'Parameter #1 $array of function array_walk expects array, Bug15369ArrayWalk\Bug79839\Test given.', + 162, + ], + ]); + $this->analyse([__DIR__ . '/data/bug-15369-array-walk-php84.php'], [ + [ + 'Parameter #1 $array of function array_walk expects array, Bug15369ArrayWalkPhp84\LazyObjects\C given.', + 17, + ], + [ + 'Parameter #1 $array of function array_walk expects array, Bug15369ArrayWalkPhp84\Gh18268\B given.', + 44, + ], + ]); + } + + public function testBug15369ZlibAndBz2(): void + { + $this->analyse([__DIR__ . '/data/bug-15369-zlib-bz2.php'], [ + [ + 'Parameter #2 $options of function deflate_init expects array, stdClass given.', + 11, + ], + [ + 'Parameter #2 $options of function deflate_init expects array, Bug15369Zlib\Gh17745\Options given.', + 16, + ], + [ + 'Parameter #2 $options of function inflate_init expects array, stdClass given.', + 25, + ], + [ + 'Parameter #2 $options of function inflate_init expects array, Bug15369Zlib\Gh17745Inflate\Options given.', + 30, + ], + [ + 'Parameter #2 $options of function deflate_init expects array, Bug15369Zlib\Gh22142\Options given.', + 42, + ], + [ + 'Parameter #2 $options of function inflate_init expects array, Bug15369Zlib\Gh22142Inflate\Options given.', + 57, + ], + // the object filter parameters are not reported, only that fopen() can return false + [ + 'Parameter #1 $stream of function stream_filter_append expects resource, resource|false given.', + 75, + ], + [ + 'Parameter #1 $stream of function stream_filter_append expects resource, resource|false given.', + 76, + ], + [ + 'Parameter #1 $stream of function fwrite expects resource, resource|false given.', + 77, + ], + [ + 'Parameter #1 $stream of function stream_filter_append expects resource, resource|false given.', + 95, + ], + [ + 'Parameter #1 $stream of function stream_filter_append expects resource, resource|false given.', + 96, + ], + [ + 'Parameter #1 $stream of function fwrite expects resource, resource|false given.', + 97, + ], + ]); + } + + public function testBug15369MbConvertVariables(): void + { + $this->analyse([__DIR__ . '/data/bug-15369-mb-convert-variables.php'], [ + [ + 'Parameter #3 $var of function mb_convert_variables expects array|string, Bug15369MbConvertVariables\Gh16261\Test given.', + 19, + ], + [ + 'Parameter #3 $var of function mb_convert_variables expects array|string, Bug15369MbConvertVariables\MbConvertVariables\foo given.', + 71, + ], + [ + 'Parameter #7 ...$vars of function mb_convert_variables expects array|string, Bug15369MbConvertVariables\MbConvertVariables\foo given.', + 81, + ], + [ + 'Parameter #3 $var of function mb_convert_variables expects array|string, int given.', + 109, + ], + ]); + } + + public function testBug15369HttpBuildQuery(): void + { + $this->analyse([__DIR__ . '/data/bug-15369-http-build-query.php'], [ + [ + 'Parameter #1 $data of function http_build_query expects array, $this(Bug15369HttpBuildQuery\Bug26817\test) given.', + 19, + ], + [ + 'Parameter #1 $data of function http_build_query expects array, Bug15369HttpBuildQuery\Bug26817\test given.', + 25, + ], + [ + 'Parameter #1 $data of function http_build_query expects array, Bug15369HttpBuildQuery\ObjectBasic\KeyVal given.', + 41, + ], + [ + 'Parameter #1 $data of function http_build_query expects array, Bug15369HttpBuildQuery\ObjectEmpty\EmptyObj given.', + 51, + ], + [ + 'Parameter #1 $data of function http_build_query expects array, Bug15369HttpBuildQuery\ObjectJustStringable\StringableObject given.', + 67, + ], + [ + 'Parameter #1 $data of function http_build_query expects array, Bug15369HttpBuildQuery\ObjectJustStringable\StringableObject given.', + 69, + ], + [ + 'Parameter #1 $data of function http_build_query expects array, Bug15369HttpBuildQuery\ObjectKeyValStringable\KeyValStringable given.', + 88, + ], + [ + 'Parameter #1 $data of function http_build_query expects array, Bug15369HttpBuildQuery\ObjectNested\KeyVal given.', + 107, + ], + [ + 'Parameter #1 $data of function http_build_query expects array, Bug15369HttpBuildQuery\ObjectRecursif\KeyVal given.', + 123, + ], + [ + 'Parameter #1 $data of function http_build_query expects array, stdClass given.', + 143, + ], + [ + 'Parameter #1 $data of function http_build_query expects array, Bug15369HttpBuildQuery\Variation1\UrlBuilder given.', + 144, + ], + ]); + } + public static function getAdditionalConfigFiles(): array { return [ diff --git a/tests/PHPStan/Rules/Functions/data/bug-15369-array-walk-php84.php b/tests/PHPStan/Rules/Functions/data/bug-15369-array-walk-php84.php new file mode 100644 index 00000000000..f5a88bc204d --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-15369-array-walk-php84.php @@ -0,0 +1,48 @@ += 8.4 + +// php-src tests of https://github.com/php/php-src/commit/7e0d5c75e7e03f3a9abe488a3df0ab893e36c740 that need PHP 8.4 + +namespace Bug15369ArrayWalkPhp84\LazyObjects { + + // Zend/tests/lazy_objects/array_walk.phpt + class C { + public int $a = 1; + } + + $reflector = new \ReflectionClass(C::class); + $obj = $reflector->newLazyProxy(function () { + return new C(); + }); + + array_walk($obj, function (&$value, $key) { + try { + $value = 'string'; + } catch (\Error $e) { + printf("%s: %s\n", $e::class, $e->getMessage()); + } + $value = 2; + }); + + var_dump($obj); + +} + +namespace Bug15369ArrayWalkPhp84\Gh18268 { + + // Zend/tests/property_hooks/gh18268.phpt + class A { + public $prop = 42; + } + + class B extends A { + public $prop = 42 { + set {} + } + } + + $b = new B; + array_walk($b, function (&$item) { + var_dump($item); + }); + +} diff --git a/tests/PHPStan/Rules/Functions/data/bug-15369-array-walk.php b/tests/PHPStan/Rules/Functions/data/bug-15369-array-walk.php new file mode 100644 index 00000000000..a05b9db21ae --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-15369-array-walk.php @@ -0,0 +1,170 @@ +getTraceAsString()); + +} + +namespace Bug15369ArrayWalk\Gh9697 { + + // ext/ffi/tests/gh9697.phpt + $x = \FFI::cdef()->new('int'); + array_walk($x, function($x) { echo "test\n"; }); + +} + +namespace Bug15369ArrayWalk\ArrayWalkObject1 { + + // ext/standard/tests/array/array_walk/array_walk_object1.phpt + function callback($value, $key, $user_data) + { + var_dump($key); + var_dump($value); + var_dump($user_data); + echo "\n"; + } + + class MyClass + { + private $pri_value; + public $pub_value; + protected $pro_value; + public function __construct($setVal) + { + $this->pri_value = $setVal; + $this->pub_value = $setVal; + $this->pro_value = $setVal; + } + }; + + // object for 'input' argument + $input = new MyClass(10); + + var_dump( array_walk($input, 'Bug15369ArrayWalk\ArrayWalkObject1\callback', 1)); + +} + +namespace Bug15369ArrayWalk\ArrayWalkObjects { + + // ext/standard/tests/array/array_walk/array_walk_objects.phpt + function walk($key, $value) { + var_dump($value, $key); + } + + class test { + private $var_pri = "test_private"; + protected $var_pro = "test_protected"; + public $var_pub = "test_public"; + } + + $stdclass = new \stdclass; + $stdclass->foo = "foo"; + $stdclass->bar = "bar"; + array_walk($stdclass, 'Bug15369ArrayWalk\ArrayWalkObjects\walk'); + + $t = new test; + array_walk($t, 'Bug15369ArrayWalk\ArrayWalkObjects\walk'); + + $var = array(); + array_walk($var, 'Bug15369ArrayWalk\ArrayWalkObjects\walk'); + $var = ""; + try { + array_walk($var, 'Bug15369ArrayWalk\ArrayWalkObjects\walk'); + } catch (\TypeError $e) { + echo $e->getMessage(), "\n"; + } + +} + +namespace Bug15369ArrayWalk\ArrayWalkRecObjects { + + // ext/standard/tests/array/array_walk/array_walk_rec_objects.phpt + function walk($key, $value) { + var_dump($value, $key); + } + + class test { + private $var_pri = "test_private"; + protected $var_pro = "test_protected"; + public $var_pub = "test_public"; + } + + $stdclass = new \stdclass; + $stdclass->foo = "foo"; + $stdclass->bar = "bar"; + array_walk_recursive($stdclass, 'Bug15369ArrayWalk\ArrayWalkRecObjects\walk'); + + $t = new test; + array_walk_recursive($t, 'Bug15369ArrayWalk\ArrayWalkRecObjects\walk'); + + $var = array(); + array_walk_recursive($var, 'Bug15369ArrayWalk\ArrayWalkRecObjects\walk'); + $var = ""; + try { + array_walk_recursive($var, 'Bug15369ArrayWalk\ArrayWalkRecObjects\walk'); + } catch (\TypeError $e) { + echo $e->getMessage(), "\n"; + } + +} + +namespace Bug15369ArrayWalk\ArrayWalkRecursiveObject1 { + + // ext/standard/tests/array/array_walk/array_walk_recursive_object1.phpt + function callback($value, $key, $user_data) + { + var_dump($key); + var_dump($value); + var_dump($user_data); + echo "\n"; + } + + class MyClass + { + private $pri_value; + public $pub_value; + protected $pro_value; + public function __construct($setVal) + { + $this->pri_value = $setVal; + $this->pub_value = $setVal; + $this->pro_value = $setVal; + } + }; + + // object for 'input' argument + $input = new MyClass(10); + + var_dump( array_walk_recursive($input, 'Bug15369ArrayWalk\ArrayWalkRecursiveObject1\callback', 1)); + +} + +namespace Bug15369ArrayWalk\Bug79839 { + + // ext/standard/tests/array/array_walk/bug79839.phpt + class Test { + public int $prop = 42; + } + + $test = new Test; + try { + array_walk($test, function(&$ref) { + $ref = []; // Should throw + }); + } catch (\TypeError $e) { + echo $e->getMessage(), "\n"; + } + var_dump($test); + +} diff --git a/tests/PHPStan/Rules/Functions/data/bug-15369-http-build-query.php b/tests/PHPStan/Rules/Functions/data/bug-15369-http-build-query.php new file mode 100644 index 00000000000..be1e336606e --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-15369-http-build-query.php @@ -0,0 +1,146 @@ +bar = 'meuh'; + $this->foo = 'lala'; + $this->test = 'test'; + + var_dump(http_build_query($this)); + } + } + + $obj = new test(); + $obj->foo(); + var_dump(http_build_query($obj)); + +} + +namespace Bug15369HttpBuildQuery\ObjectBasic { + + // ext/standard/tests/http/http_build_query/http_build_query_object_basic.phpt + class KeyVal { + public $public = "input"; + protected $protected = "hello"; + private $private = "world"; + } + + $o = new KeyVal(); + + // Percent encoded "public=input" + var_dump(http_build_query($o)); + +} + +namespace Bug15369HttpBuildQuery\ObjectEmpty { + + // ext/standard/tests/http/http_build_query/http_build_query_object_empty.phpt + class EmptyObj {} + $o = new EmptyObj(); + + var_dump(http_build_query($o)); + +} + +namespace Bug15369HttpBuildQuery\ObjectJustStringable { + + // ext/standard/tests/http/http_build_query/http_build_query_object_just_stringable.phpt + class StringableObject { + public function __toString() : string { + return "Stringable"; + } + } + + $o = new StringableObject(); + + var_dump(http_build_query(['hello', $o])); + var_dump(http_build_query($o)); + var_dump(http_build_query(['hello', $o], numeric_prefix: 'prefix_')); + var_dump(http_build_query($o, numeric_prefix: 'prefix_')); + +} + +namespace Bug15369HttpBuildQuery\ObjectKeyValStringable { + + // ext/standard/tests/http/http_build_query/http_build_query_object_key_val_stringable.phpt + class KeyValStringable { + public $public = "input"; + protected $protected = "hello"; + private $private = "world"; + + public function __toString(): string { + return "Stringable"; + } + } + + $o = new KeyValStringable(); + + var_dump(http_build_query($o)); + +} + +namespace Bug15369HttpBuildQuery\ObjectNested { + + // ext/standard/tests/http/http_build_query/http_build_query_object_nested.phpt + class KeyVal { + public $public = "input"; + protected $protected = "hello"; + private $private = "world"; + } + + $o = new KeyVal(); + $nested = new KeyVal(); + + $o->public = $nested; + + // Percent encoded "public[public]=input" + var_dump(http_build_query($o)); + +} + +namespace Bug15369HttpBuildQuery\ObjectRecursif { + + // ext/standard/tests/http/http_build_query/http_build_query_object_recursif.phpt + class KeyVal { + public $public = "input"; + protected $protected = "hello"; + private $private = "world"; + } + + $o = new KeyVal(); + $o->public = $o; + + var_dump(http_build_query($o)); + +} + +namespace Bug15369HttpBuildQuery\Variation1 { + + // ext/standard/tests/http/http_build_query/http_build_query_variation1.phpt + class UrlBuilder + { + public $name = 'homepage'; + public $page = 1; + protected $sort = 'desc,name'; + private $access = 'admin'; + } + + $obj = new \stdClass; + $obj->name = 'homepage'; + $obj->page = 1; + $obj->sort = 'desc,name'; + + echo http_build_query($obj) . PHP_EOL; + echo http_build_query(new UrlBuilder()); + +} diff --git a/tests/PHPStan/Rules/Functions/data/bug-15369-mb-convert-variables.php b/tests/PHPStan/Rules/Functions/data/bug-15369-mb-convert-variables.php new file mode 100644 index 00000000000..f79e01c3e5f --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-15369-mb-convert-variables.php @@ -0,0 +1,119 @@ +x =& $ref; + $test->z =& $ref3; + mb_convert_variables("EUC-JP", "Shift_JIS", $test); + + class Test2 { + public function __construct(public string $x) {} + } + $test2 = new Test2("foo"); + + mb_convert_variables("EUC-JP", "Shift_JIS", $test->x); + + var_dump($test, $test2); + +} + +namespace Bug15369MbConvertVariables\MbConvertVariables { + + // ext/mbstring/tests/mb_convert_variables.phpt + $euc_jp = mb_convert_encoding("日本語テキストです。0123456789。", 'EUC-JP', 'UTF-8'); + + // Test for single scalar + $s = $euc_jp; + $encoding = mb_convert_variables('SJIS', 'EUC-JP', $s); + + // Test for multiple scalar + $s1 = $euc_jp; + $s2 = $euc_jp; + $s3 = $euc_jp; + $encoding = mb_convert_variables('EUC-JP', 'auto', $s1, $s2, $s3); + + // Note: Mixing encoding in array/object is not supported + $a = array($s3, $s2, $s1); + $aa = $a; + $encoding = mb_convert_variables('EUC-JP', 'auto', $aa); + + // Test for object + class foo + { + public $s1; + public $s2; + public $s3; + + function __construct() + { + global $sjis, $jis, $euc_jp; + + $this->s1 = $euc_jp; + $this->s2 = $euc_jp; + $this->s3 = $euc_jp; + } + } + + $o = new foo; + $oo = $o; + $encoding = mb_convert_variables('EUC-JP', 'auto', $oo); + + // Test for scalar, array and object + $s1 = $euc_jp; + $s2 = $euc_jp; + $s3 = $euc_jp; + $a = array($s1, $s2, $s3); + $aa = $a; + $oo = $o; + + $encoding = mb_convert_variables('EUC-JP', 'auto', $s1, $s2, $s3, $aa, $oo); + + class Nested + { + public $inner; + + function __construct($value) + { + $this->inner = $value; + } + } + + $deeplyNested = array(new Nested(array(new Nested(array(new Nested("BLAH")))))); + + $encoding = mb_convert_variables('UTF-16LE', 'UTF-8', $deeplyNested); + + $bad_utf7 = "abc + abc"; + var_dump(mb_convert_variables('UTF-8', 'UTF-7,UTF-8', $bad_utf7)); + +} + +namespace Bug15369MbConvertVariables\InvalidTypes { + + // ext/mbstring/tests/mb_convert_variables_invalid_types.phpt + $i = 5; + $a = [$i]; + + try { + var_dump( mb_convert_variables('UTF-8', 'EUC-JP', $i, $a)); + } catch (\ValueError $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; + } + try { + var_dump( mb_convert_variables('UTF-8', 'EUC-JP', $a)); + } catch (\ValueError $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; + } + +} diff --git a/tests/PHPStan/Rules/Functions/data/bug-15369-zlib-bz2.php b/tests/PHPStan/Rules/Functions/data/bug-15369-zlib-bz2.php new file mode 100644 index 00000000000..89360c605f6 --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-15369-zlib-bz2.php @@ -0,0 +1,99 @@ +level = 3; + var_dump(deflate_init(ZLIB_ENCODING_RAW, $obj)); + + class Options { + public int $level = 3; + } + var_dump(deflate_init(ZLIB_ENCODING_RAW, new Options)); + +} + +namespace Bug15369Zlib\Gh17745Inflate { + + // ext/zlib/tests/gh17745_inflate.phpt + $obj = new \stdClass; + $obj->level = 3; + var_dump(inflate_init(ZLIB_ENCODING_RAW, $obj)); + + class Options { + public int $level = 3; + } + var_dump(inflate_init(ZLIB_ENCODING_RAW, new Options)); + +} + +namespace Bug15369Zlib\Gh22142 { + + // ext/zlib/tests/gh22142.phpt + class Options { + public int $level; + } + + try { + deflate_init(ZLIB_ENCODING_DEFLATE, new Options()); + } catch (\TypeError $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; + } + +} + +namespace Bug15369Zlib\Gh22142Inflate { + + // ext/zlib/tests/gh22142_inflate.phpt + class Options { + public int $window; + } + + try { + inflate_init(ZLIB_ENCODING_DEFLATE, new Options()); + } catch (\TypeError $e) { + echo $e::class, ': ', $e->getMessage(), PHP_EOL; + } + +} + +namespace Bug15369Zlib\FilterBrokenObjectOptions { + + // ext/zlib/tests/filter_broken_object_options.phpt + // $params of stream_filter_append() stays mixed, a user stream filter may expect an object + class Params { + public int $memory = 6; + public int $window = 15; + public int $level = 6; + } + + $fp = fopen('php://stdout', 'w'); + stream_filter_append($fp, 'zlib.deflate', STREAM_FILTER_WRITE, new Params); + stream_filter_append($fp, 'zlib.inflate', STREAM_FILTER_WRITE, new Params); + fwrite($fp, "Hello world, hopefully not broken\n"); + +} + +namespace Bug15369Bz2\FilterBrokenObjectOptions { + + // ext/bz2/tests/filter_broken_object_options.phpt + class ParamsCompress { + public int $blocks = 5; + public int $work = 10; + } + + class ParamsDecompress { + public bool $concatenated = true; + public bool $small = true; + } + + $fp = fopen('php://stdout', 'w'); + stream_filter_append($fp, 'bzip2.compress', STREAM_FILTER_WRITE, new ParamsCompress); + stream_filter_append($fp, 'bzip2.decompress', STREAM_FILTER_WRITE, new ParamsDecompress); + fwrite($fp, "Hello world, hopefully not broken\n"); + +} From 424216231d4bd172b9bf81b0339fd8014d3a1de5 Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Sun, 15 Nov 2026 12:02:16 +0000 Subject: [PATCH 2/4] Mark the PHP 8.0 syntax of the PHP 8.6 signature fixtures The http_build_query() fixture uses named arguments and the mb_convert_variables() fixture uses promoted properties, so they need a `// lint >= 8.0` comment. Amp-Thread-ID: https://ampcode.com/threads/T-01a1def5-2aa3-73fc-be3e-a45c7762cafb --- .../PHPStan/Rules/Functions/data/bug-15369-http-build-query.php | 2 +- .../Rules/Functions/data/bug-15369-mb-convert-variables.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/PHPStan/Rules/Functions/data/bug-15369-http-build-query.php b/tests/PHPStan/Rules/Functions/data/bug-15369-http-build-query.php index be1e336606e..6b164a68cbd 100644 --- a/tests/PHPStan/Rules/Functions/data/bug-15369-http-build-query.php +++ b/tests/PHPStan/Rules/Functions/data/bug-15369-http-build-query.php @@ -1,4 +1,4 @@ -= 8.0 // php-src tests of https://github.com/php/php-src/commit/a49acfa9a7aeaa44a0c49bea15058d97d9238960 diff --git a/tests/PHPStan/Rules/Functions/data/bug-15369-mb-convert-variables.php b/tests/PHPStan/Rules/Functions/data/bug-15369-mb-convert-variables.php index f79e01c3e5f..ce366cc282b 100644 --- a/tests/PHPStan/Rules/Functions/data/bug-15369-mb-convert-variables.php +++ b/tests/PHPStan/Rules/Functions/data/bug-15369-mb-convert-variables.php @@ -1,4 +1,4 @@ -= 8.0 // php-src tests of https://github.com/php/php-src/commit/7cc143235d2be7b989348d37f465860835833a25 From 3a115de6ba3210fd0e8d7eab1badc14dce86c9ab Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Sun, 15 Nov 2026 12:09:54 +0000 Subject: [PATCH 3/4] Update existing array_walk() expectations for PHP 8.6 On PHP 8.6, the partial application of array_walk() takes an array, and passing a stdClass object to array_walk() and array_walk_recursive() is reported. Amp-Thread-ID: https://ampcode.com/threads/T-01a1def5-2aa3-73fc-be3e-a45c7762cafb --- .../partial-function-application-signature.php | 2 +- .../CallToFunctionParametersRuleTest.php | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/PHPStan/Analyser/nsrt/partial-function-application-signature.php b/tests/PHPStan/Analyser/nsrt/partial-function-application-signature.php index 6ac9a99ab37..da52ee66e64 100644 --- a/tests/PHPStan/Analyser/nsrt/partial-function-application-signature.php +++ b/tests/PHPStan/Analyser/nsrt/partial-function-application-signature.php @@ -160,7 +160,7 @@ function functions(): void assertType('Closure', mt_rand(?, ...)); assertType('Closure', array_walk(?, ?, ...)); assertType('static-Closure(array, mixed, bool=): list', array_keys(?, ?, ...)); - assertType('static-Closure(array|object, callable): true', array_walk(?, ?)); + assertType('static-Closure(array, callable): true', array_walk(?, ?)); // templates assertType('static-Closure(T): T', identity(?)); diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index 43e424ce46b..8b60dce77cd 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -717,7 +717,7 @@ public function testArrayReduceArrowFunctionCallback(): void public function testArrayWalkCallback(): void { - $this->analyse([__DIR__ . '/data/array_walk.php'], [ + $errors = [ [ 'Parameter #2 $callback of function array_walk expects callable(1|2, \'bar\'|\'foo\'): mixed, Closure(stdClass, float): \'\' given.', 6, @@ -731,7 +731,18 @@ public function testArrayWalkCallback(): void 23, 'Parameter #3 $extra of passed callable is required but accepting callable does not have that parameter. It will be called without it.', ], - ]); + ]; + if (PHP_VERSION_ID >= 80600) { + $errors[] = [ + 'Parameter #1 $array of function array_walk expects array, object{foo: string}&stdClass given.', + 30, + ]; + $errors[] = [ + 'Parameter #1 $array of function array_walk_recursive expects array, object{foo: string}&stdClass given.', + 37, + ]; + } + $this->analyse([__DIR__ . '/data/array_walk.php'], $errors); } public function testArrayWalkArrowFunctionCallback(): void From 41bb005fbc080acec70054cdca7844f8218b565d Mon Sep 17 00:00:00 2001 From: "ondrej@mirtes.cz" Date: Sun, 15 Nov 2026 12:25:41 +0000 Subject: [PATCH 4/4] Intersect a delta-changed parameter type with the native type Every parameter type a version delta changes is accepted by the native type, so checking that before keeping it was never false. Intersecting with the native type keeps the same types and leaves out what PHP rejects should a delta ever describe more. Amp-Thread-ID: https://ampcode.com/threads/T-01a1def5-2aa3-73fc-be3e-a45c7762cafb --- src/Reflection/SignatureMap/Php8SignatureMapProvider.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Reflection/SignatureMap/Php8SignatureMapProvider.php b/src/Reflection/SignatureMap/Php8SignatureMapProvider.php index 27c5403928f..5d76c78ff8f 100644 --- a/src/Reflection/SignatureMap/Php8SignatureMapProvider.php +++ b/src/Reflection/SignatureMap/Php8SignatureMapProvider.php @@ -441,12 +441,9 @@ private function mergeSignatures(FunctionSignature $nativeSignature, FunctionSig $functionMapParameter = $functionMapSignature->getParameters()[$i]; $nativeParameterType = $nativeParameter->getNativeType(); - if ( - in_array($i, $parametersChangedByDelta, true) - && $nativeParameterType->isSuperTypeOf($functionMapParameter->getType())->yes() - ) { + if (in_array($i, $parametersChangedByDelta, true)) { // unlike decideType(), keeps out members of the native union type the delta leaves out - $parameterType = $functionMapParameter->getType(); + $parameterType = TypeCombinator::intersect($nativeParameterType, $functionMapParameter->getType()); } else { $parameterType = TypehintHelper::decideType( $nativeParameterType,