From 2f7df630578662d6324048dd056a1b52dd020d47 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 22 Jul 2025 12:27:25 +0200 Subject: [PATCH 1/2] use benevolent union for `stream_get_contents()` to prevent strict-rule errors --- resources/functionMap.php | 2 +- ...GetContentsFunctionReturnTypeExtension.php | 41 ------------------- 2 files changed, 1 insertion(+), 42 deletions(-) delete mode 100644 src/Type/Php/StreamGetContentsFunctionReturnTypeExtension.php diff --git a/resources/functionMap.php b/resources/functionMap.php index fade07b20c..69266f315d 100644 --- a/resources/functionMap.php +++ b/resources/functionMap.php @@ -11661,7 +11661,7 @@ 'stream_filter_prepend' => ['resource|false', 'stream'=>'resource', 'filtername'=>'string', 'read_write='=>'int', 'params='=>'mixed'], 'stream_filter_register' => ['bool', 'filtername'=>'string', 'classname'=>'string'], 'stream_filter_remove' => ['bool', 'stream_filter'=>'resource'], -'stream_get_contents' => ['string|false', 'source'=>'resource', 'maxlen='=>'int', 'offset='=>'int'], +'stream_get_contents' => ['__benevolent', 'source'=>'resource', 'maxlen='=>'int', 'offset='=>'int'], 'stream_get_filters' => ['list'], 'stream_get_line' => ['string|false', 'stream'=>'resource', 'maxlen'=>'int', 'ending='=>'string'], 'stream_get_meta_data' => ['array{timed_out:bool,blocked:bool,eof:bool,unread_bytes:int,stream_type:string,wrapper_type:string,wrapper_data:mixed,mode:string,seekable:bool,uri?:string,mediatype?:string,base64?:bool}', 'fp'=>'resource'], diff --git a/src/Type/Php/StreamGetContentsFunctionReturnTypeExtension.php b/src/Type/Php/StreamGetContentsFunctionReturnTypeExtension.php deleted file mode 100644 index 791f0d85ab..0000000000 --- a/src/Type/Php/StreamGetContentsFunctionReturnTypeExtension.php +++ /dev/null @@ -1,41 +0,0 @@ -getName() === 'stream_get_contents'; - } - - public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type - { - if (count($functionCall->getArgs()) >= 3) { - return null; - } - - // stream_get_contents() does not return FALSE unless an invalid offset is provided. - $returnType = ParametersAcceptorSelector::selectFromArgs( - $scope, - $functionCall->getArgs(), - $functionReflection->getVariants(), - )->getReturnType(); - - return TypeCombinator::remove($returnType, new ConstantBooleanType(false)); - } - -} From 38bebbaeb1ac2019a15975fc727abef1e6ec399d Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 22 Jul 2025 12:31:41 +0200 Subject: [PATCH 2/2] Update CallMethodsRuleTest.php --- tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index d423d80dbb..bf135b42a6 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -3615,12 +3615,7 @@ public function testBug3396(): void $this->checkUnionTypes = true; $this->checkExplicitMixed = false; - $this->analyse([__DIR__ . '/data/bug-3396.php'], [ - [ - 'Parameter #1 $s of method Bug3396\HelloWorld::takesString() expects string, string|false given.', - 18, - ], - ]); + $this->analyse([__DIR__ . '/data/bug-3396.php'], []); } }