Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d93776c

Browse files
Fix impossible type checks for array offset/property fetch
1 parent 213863e commit d93776c

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

‎src/Analyser/NodeScopeResolver.php‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,11 +2144,7 @@ static function (?Type $offsetType, Type $valueType, bool $optional) use (&$arra
21442144
$className = $scope->resolveName($expr->class);
21452145
if ($this->reflectionProvider->hasClass($className)) {
21462146
$classReflection = $this->reflectionProvider->getClass($className);
2147-
if (is_string($expr->name)) {
2148-
$methodName = $expr->name;
2149-
} else {
2150-
$methodName = $expr->name->name;
2151-
}
2147+
$methodName = $expr->name->name;
21522148
if ($classReflection->hasMethod($methodName)) {
21532149
$methodReflection = $classReflection->getMethod($methodName, $scope);
21542150
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(

‎src/Rules/Comparison/ImpossibleCheckTypeHelper.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ private static function isSpecified(Scope $scope, Expr $node, Expr $expr): bool
262262
return is_string($expr->name) && !$scope->hasVariableType($expr->name)->yes();
263263
}
264264

265+
if ($expr instanceof Expr\ArrayDimFetch || $expr instanceof Expr\PropertyFetch) {
266+
return self::isSpecified($scope, $node, $expr->var) && !$scope->hasExpressionType($expr)->yes();
267+
}
268+
269+
if ($expr instanceof Expr\StaticPropertyFetch) {
270+
return !$scope->hasExpressionType($expr)->yes();
271+
}
272+
265273
if ($expr instanceof Expr\BooleanNot) {
266274
return self::isSpecified($scope, $node, $expr->expr);
267275
}

‎tests/PHPStan/Analyser/NodeScopeResolverTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,7 @@ public function dataFileAsserts(): iterable
11181118
yield from $this->gatherAssertTypes(__DIR__ . '/data/composer-treatPhpDocTypesAsCertainBug.php');
11191119
yield from $this->gatherAssertTypes(__DIR__ . '/data/closure-retain-expression-types.php');
11201120
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7913.php');
1121+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8249.php');
11211122
}
11221123

11231124
/**
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Bug8249;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
function foo(): mixed
8+
{
9+
return null;
10+
}
11+
12+
class Foo
13+
{
14+
public $x;
15+
}
16+
17+
class Bar
18+
{
19+
public static $x;
20+
}
21+
22+
function () {
23+
$x = foo();
24+
25+
if (is_int($x)) {
26+
assertType('int', $x);
27+
assertType('true', is_int($x));
28+
} else {
29+
assertType('mixed~int', $x);
30+
assertType('false', is_int($x));
31+
}
32+
};
33+
34+
function () {
35+
$x = ['x' => foo()];
36+
37+
if (is_int($x['x'])) {
38+
assertType('int', $x['x']);
39+
assertType('true', is_int($x['x']));
40+
} else {
41+
assertType('mixed~int', $x['x']);
42+
assertType('false', is_int($x['x']));
43+
}
44+
};
45+
46+
function (Foo $x) {
47+
if (is_int($x->x)) {
48+
assertType('int', $x->x);
49+
assertType('true', is_int($x->x));
50+
} else {
51+
assertType('mixed~int', $x->x);
52+
assertType('false', is_int($x->x));
53+
}
54+
};
55+
56+
57+
function () {
58+
if (is_int(Bar::$x)) {
59+
assertType('int', Bar::$x);
60+
assertType('true', is_int(Bar::$x));
61+
} else {
62+
assertType('mixed~int', Bar::$x);
63+
assertType('false', is_int(Bar::$x));
64+
}
65+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /