Skip to content

Commit 1220d8e

Browse files
Allow getenv(null) for PHP 8.0+
Co-authored-by: Ondrej Mirtes <[email protected]>
1 parent f356b16 commit 1220d8e

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

resources/functionMap_php80delta.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
'imagejpeg' => ['bool', 'im'=>'GdImage', 'filename='=>'string|resource|null', 'quality='=>'int'],
7979
'imagerotate' => ['false|object', 'src_im'=>'resource', 'angle'=>'float', 'bgdcolor'=>'int', 'ignoretransparent='=>'int'],
8080
'imagescale' => ['false|object', 'im'=>'resource', 'new_width'=>'int', 'new_height='=>'int', 'method='=>'int'],
81+
'getenv' => ['string|false', 'varname'=>'string', 'local_only='=>'bool'],
82+
'getenv\'1' => ['array<string, string>', 'varname='=>'null', 'local_only='=>'bool'],
8183
'ldap_set_rebind_proc' => ['bool', 'ldap'=>'resource', 'callback'=>'?callable'],
8284
'mb_decode_numericentity' => ['string|false', 'string'=>'string', 'convmap'=>'array', 'encoding='=>'string'],
8385
'mb_encoding_aliases' => ['list<non-falsy-string>', 'encoding'=>'string'],
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php // lint < 8.0
2+
3+
namespace GetenvPHP74;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
/**
10+
* @param string|null $stringOrNull
11+
* @param mixed $mixed
12+
*/
13+
public function test($stringOrNull, $mixed)
14+
{
15+
assertType('string|false', getenv(null));
16+
assertType('array<string, string>', getenv());
17+
assertType('string|false', getenv('foo'));
18+
19+
assertType('string|false', getenv($stringOrNull));
20+
assertType('string|false', getenv($mixed));
21+
}
22+
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php // lint >= 8.0
2+
3+
namespace GetenvPHP80;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
public function test(string|null $stringOrNull, mixed $mixed)
11+
{
12+
assertType('array<string, string>', getenv(null));
13+
assertType('array<string, string>', getenv());
14+
assertType('string|false', getenv('foo'));
15+
16+
assertType('array<string, string>|string|false', getenv($stringOrNull));
17+
assertType('array<string, string>|string|false', getenv($mixed));
18+
}
19+
20+
}

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,4 +2102,17 @@ public function testBug12499(): void
21022102
$this->analyse([__DIR__ . '/data/bug-12499.php'], []);
21032103
}
21042104

2105+
public function testBug13065(): void
2106+
{
2107+
$errors = [];
2108+
if (PHP_VERSION_ID < 80000) {
2109+
$errors[] = [
2110+
'Parameter #1 $varname of function getenv expects string, null given.',
2111+
10,
2112+
];
2113+
}
2114+
2115+
$this->analyse([__DIR__ . '/data/bug-13065.php'], $errors);
2116+
}
2117+
21052118
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Bug13065;
4+
5+
class Foo
6+
{
7+
8+
public function test()
9+
{
10+
getenv(null);
11+
getenv();
12+
getenv('foo');
13+
}
14+
15+
}

0 commit comments

Comments
 (0)