Skip to content

Commit 72807a5

Browse files
committed
Fix explode() tests on PHP 8
1 parent bde2a3d commit 72807a5

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/Type/Php/ExplodeFunctionDynamicReturnTypeExtension.php

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node\Expr\FuncCall;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\Php\PhpVersion;
78
use PHPStan\Reflection\FunctionReflection;
89
use PHPStan\Reflection\ParametersAcceptorSelector;
910
use PHPStan\Type\Accessory\NonEmptyArrayType;
@@ -13,6 +14,7 @@
1314
use PHPStan\Type\IntegerRangeType;
1415
use PHPStan\Type\IntegerType;
1516
use PHPStan\Type\MixedType;
17+
use PHPStan\Type\NeverType;
1618
use PHPStan\Type\StringType;
1719
use PHPStan\Type\Type;
1820
use PHPStan\Type\TypeCombinator;
@@ -21,6 +23,13 @@
2123
class ExplodeFunctionDynamicReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
2224
{
2325

26+
private PhpVersion $phpVersion;
27+
28+
public function __construct(PhpVersion $phpVersion)
29+
{
30+
$this->phpVersion = $phpVersion;
31+
}
32+
2433
public function isFunctionSupported(FunctionReflection $functionReflection): bool
2534
{
2635
return $functionReflection->getName() === 'explode';
@@ -39,6 +48,9 @@ public function getTypeFromFunctionCall(
3948
$delimiterType = $scope->getType($functionCall->args[0]->value);
4049
$isSuperset = (new ConstantStringType(''))->isSuperTypeOf($delimiterType);
4150
if ($isSuperset->yes()) {
51+
if ($this->phpVersion->getVersionId() >= 80000) {
52+
return new NeverType();
53+
}
4254
return new ConstantBooleanType(false);
4355
} elseif ($isSuperset->no()) {
4456
$arrayType = new ArrayType(new IntegerType(), new StringType());

tests/PHPStan/Analyser/NodeScopeResolverTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -10187,6 +10187,10 @@ public function dataClassConstantOnExpression(): array
1018710187

1018810188
public function dataBug3961(): array
1018910189
{
10190+
if (PHP_VERSION >= 80000) {
10191+
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3961-php8.php');
10192+
}
10193+
1019010194
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3961.php');
1019110195
}
1019210196

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Bug3961Php8;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
class Foo
8+
{
9+
10+
public function doFoo(string $v, string $d, $m): void
11+
{
12+
assertType('array<int, string>&nonEmpty', explode('.', $v));
13+
assertType('*NEVER*', explode('', $v));
14+
assertType('array<int, string>', explode('.', $v, -2));
15+
assertType('array<int, string>&nonEmpty', explode('.', $v, 0));
16+
assertType('array<int, string>&nonEmpty', explode('.', $v, 1));
17+
assertType('array<int, string>', explode($d, $v));
18+
assertType('array<int, string>', explode($m, $v));
19+
}
20+
21+
}

0 commit comments

Comments
 (0)