diff --git a/resources/functionMap_php80delta.php b/resources/functionMap_php80delta.php index c6a9dfe91a..ec070388a3 100644 --- a/resources/functionMap_php80delta.php +++ b/resources/functionMap_php80delta.php @@ -78,6 +78,8 @@ 'imagejpeg' => ['bool', 'im'=>'GdImage', 'filename='=>'string|resource|null', 'quality='=>'int'], 'imagerotate' => ['false|object', 'src_im'=>'resource', 'angle'=>'float', 'bgdcolor'=>'int', 'ignoretransparent='=>'int'], 'imagescale' => ['false|object', 'im'=>'resource', 'new_width'=>'int', 'new_height='=>'int', 'method='=>'int'], + 'getenv' => ['string|false', 'varname'=>'string', 'local_only='=>'bool'], + 'getenv\'1' => ['array<string, string>', 'varname='=>'null', 'local_only='=>'bool'], 'ldap_set_rebind_proc' => ['bool', 'ldap'=>'resource', 'callback'=>'?callable'], 'mb_decode_numericentity' => ['string|false', 'string'=>'string', 'convmap'=>'array', 'encoding='=>'string'], 'mb_encoding_aliases' => ['list<non-falsy-string>', 'encoding'=>'string'], diff --git a/tests/PHPStan/Analyser/nsrt/getenv-php74.php b/tests/PHPStan/Analyser/nsrt/getenv-php74.php new file mode 100644 index 0000000000..a100e47686 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/getenv-php74.php @@ -0,0 +1,23 @@ +<?php // lint < 8.0 + +namespace GetenvPHP74; + +use function PHPStan\Testing\assertType; + +class Foo +{ + /** + * @param string|null $stringOrNull + * @param mixed $mixed + */ + public function test($stringOrNull, $mixed) + { + assertType('string|false', getenv(null)); + assertType('array<string, string>', getenv()); + assertType('string|false', getenv('foo')); + + assertType('string|false', getenv($stringOrNull)); + assertType('string|false', getenv($mixed)); + } + +} diff --git a/tests/PHPStan/Analyser/nsrt/getenv-php80.php b/tests/PHPStan/Analyser/nsrt/getenv-php80.php new file mode 100644 index 0000000000..c5036fa153 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/getenv-php80.php @@ -0,0 +1,20 @@ +<?php // lint >= 8.0 + +namespace GetenvPHP80; + +use function PHPStan\Testing\assertType; + +class Foo +{ + + public function test(string|null $stringOrNull, mixed $mixed) + { + assertType('array<string, string>', getenv(null)); + assertType('array<string, string>', getenv()); + assertType('string|false', getenv('foo')); + + assertType('array<string, string>|string|false', getenv($stringOrNull)); + assertType('array<string, string>|string|false', getenv($mixed)); + } + +} diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index 31538252d8..9358802c16 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -2102,4 +2102,17 @@ public function testBug12499(): void $this->analyse([__DIR__ . '/data/bug-12499.php'], []); } + public function testBug13065(): void + { + $errors = []; + if (PHP_VERSION_ID < 80000) { + $errors[] = [ + 'Parameter #1 $varname of function getenv expects string, null given.', + 10, + ]; + } + + $this->analyse([__DIR__ . '/data/bug-13065.php'], $errors); + } + } diff --git a/tests/PHPStan/Rules/Functions/data/bug-13065.php b/tests/PHPStan/Rules/Functions/data/bug-13065.php new file mode 100644 index 0000000000..f63acf9989 --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-13065.php @@ -0,0 +1,15 @@ +<?php + +namespace Bug13065; + +class Foo +{ + + public function test() + { + getenv(null); + getenv(); + getenv('foo'); + } + +}