Skip to content

Allow getenv(null) for PHP > 8 #4007

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

Merged
merged 8 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions resources/functionMap_php80delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Analyser/nsrt/getenv-php74.php
Original file line number Diff line number Diff line change
@@ -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));
}

}
20 changes: 20 additions & 0 deletions tests/PHPStan/Analyser/nsrt/getenv-php80.php
Original file line number Diff line number Diff line change
@@ -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));
}

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-13065.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Bug13065;

class Foo
{

public function test()
{
getenv(null);
getenv();
getenv('foo');
}

}
Loading