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 5 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
5 changes: 5 additions & 0 deletions src/Php/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,9 @@ public function substrReturnFalseInsteadOfEmptyString(): bool
return $this->versionId < 80000;
}

public function getenvAcceptsNull(): bool
{
return $this->versionId >= 80000;
}

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ private static function findTestFiles(): iterable
yield __DIR__ . '/data/explode-php80.php';
}

if (PHP_VERSION_ID < 80000) {
yield __DIR__ . '/data/getenv-php74.php';
} else {
yield __DIR__ . '/data/getenv-php80.php';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to be in NodeScopeResolverTest. Move the files to nsrt/ and label them with proper // lint conditions.

Copy link
Contributor Author

@VincentLanglet VincentLanglet May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already similar conditions checks like above

if (PHP_VERSION_ID < 80000) {
			yield __DIR__ . '/data/explode-php74.php';
		} else {
			yield __DIR__ . '/data/explode-php80.php';
		}

The expectated type are different based on the PHP version, that's why I split the files.

The // lint condition is only used for the linter, not for the tests right ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Files in nsrt/ when testing type inference are filtered based on// lint as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ! Awesome !

}

if (PHP_VERSION_ID >= 80000) {
yield __DIR__ . '/../Reflection/data/unionTypes.php';
yield __DIR__ . '/../Reflection/data/mixedType.php';
Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Analyser/data/getenv-php74.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace GetenvPHP74;

use function PHPStan\Testing\assertType;

class Foo
{

public function test()
{
assertType('string|false', getenv(null));
assertType('array<string, string>', getenv());
assertType('string|false', getenv('foo'));
}

}
17 changes: 17 additions & 0 deletions tests/PHPStan/Analyser/data/getenv-php80.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace GetenvPHP80;

use function PHPStan\Testing\assertType;

class Foo
{

public function test()
{
assertType('array<string, string>', getenv(null));
assertType('array<string, string>', getenv());
assertType('string|false', getenv('foo'));
}

}
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