Skip to content

Commit ca1918c

Browse files
phpstan-botstaabm
andauthored
Remember method/static call expression as truthy/falsey alongside @phpstan-assert-if-true argument narrowing (#5880)
Co-authored-by: Markus Staab <maggus.staab@googlemail.com> Co-authored-by: Markus Staab <markus.staab@redaxo.de>
1 parent ef0dff1 commit ca1918c

8 files changed

Lines changed: 224 additions & 3 deletions

File tree

‎src/Analyser/ExprHandler/MethodCallHandler.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
328328
));
329329
$specifiedTypes = $typeSpecifier->specifyTypesFromAsserts($context, $expr, $asserts, $parametersAcceptor, $scope);
330330
if ($specifiedTypes !== null) {
331-
return $specifiedTypes;
331+
return $specifiedTypes
332+
->unionWith($typeSpecifier->handleDefaultTruthyOrFalseyContext($context, $expr, $scope))
333+
->setRootExpr($specifiedTypes->getRootExpr());
332334
}
333335
}
334336
}

‎src/Analyser/ExprHandler/StaticCallHandler.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,9 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e
429429
));
430430
$specifiedTypes = $typeSpecifier->specifyTypesFromAsserts($context, $expr, $asserts, $parametersAcceptor, $scope);
431431
if ($specifiedTypes !== null) {
432-
return $specifiedTypes;
432+
return $specifiedTypes
433+
->unionWith($typeSpecifier->handleDefaultTruthyOrFalseyContext($context, $expr, $scope))
434+
->setRootExpr($specifiedTypes->getRootExpr());
433435
}
434436
}
435437
}

‎src/Type/StaticType.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ private function transformStaticType(Type $type, ClassMemberAccessAnswerer $scop
380380
}
381381

382382
if (!$isFinal || $type instanceof ThisType) {
383-
return $traverse($type);
383+
return RecursionGuard::run($type, static fn () => $traverse($type));
384384
}
385385

386386
return $traverse($type->getStaticObjectType());

‎tests/PHPStan/Analyser/AnalyserIntegrationTest.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,15 @@ public function testBug9172(): void
15931593
$this->assertNotEmpty($errors);
15941594
}
15951595

1596+
#[RequiresPhp('>= 8.1.0')]
1597+
public function testPr5880(): void
1598+
{
1599+
// endless loop
1600+
$errors = $this->runAnalyse(__DIR__ . '/data/pr-5880.php');
1601+
$this->assertCount(1, $errors);
1602+
$this->assertSame('Call to an undefined method (T of PR5880EndlessRecursion\A = PR5880EndlessRecursion\A|PR5880EndlessRecursion\B)|T of PR5880EndlessRecursion\B = PR5880EndlessRecursion\A|PR5880EndlessRecursion\B::a().', $errors[0]->getMessage());
1603+
}
1604+
15961605
public function testBug14707(): void
15971606
{
15981607
$errors = $this->runAnalyse(__DIR__ . '/data/bug-14707.php');
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php // lint >= 8.0
2+
3+
declare(strict_types = 1);
4+
5+
namespace PR5880EndlessRecursion;
6+
7+
class A {
8+
public function a(): void {}
9+
}
10+
class B {}
11+
12+
/** @template-covariant T of A|B = A|B */
13+
interface FooInterface
14+
{
15+
/**
16+
* @phpstan-assert-if-true static<A> $this
17+
*/
18+
public function isA(): bool;
19+
20+
/** @return T */
21+
public function get(): A|B;
22+
}
23+
24+
/**
25+
* @template-covariant T of A|B = A|B
26+
* @implements FooInterface<T>
27+
*/
28+
abstract class Foo implements FooInterface
29+
{
30+
public function other(): void
31+
{
32+
if ($this->isA()) {
33+
$this->get()->a();
34+
}
35+
}
36+
37+
}

‎tests/PHPStan/Analyser/nsrt/bug-14829.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44

55
use function PHPStan\Testing\assertType;
66

7+
class Checker
8+
{
9+
10+
/** @phpstan-assert-if-true =non-empty-string $path */
11+
public function isReadable(string $path): bool
12+
{
13+
return $path !== '';
14+
}
15+
16+
/** @phpstan-assert-if-true =non-empty-string $path */
17+
public static function staticIsReadable(string $path): bool
18+
{
19+
return $path !== '';
20+
}
21+
22+
}
23+
724
function testFunction(string $path): void
825
{
926
// is_readable() has @phpstan-assert-if-true in stubs/file.stub
@@ -17,3 +34,19 @@ function testFunction(string $path): void
1734
}
1835
assertType('true', is_readable($path));
1936
}
37+
38+
function testMethod(Checker $c, string $path): void
39+
{
40+
if ($c->isReadable($path)) {
41+
assertType('true', $c->isReadable($path));
42+
assertType('non-empty-string', $path);
43+
}
44+
}
45+
46+
function testStaticMethod(string $path): void
47+
{
48+
if (Checker::staticIsReadable($path)) {
49+
assertType('true', Checker::staticIsReadable($path));
50+
assertType('non-empty-string', $path);
51+
}
52+
}

‎tests/PHPStan/Rules/Comparison/BooleanAndConstantConditionRuleTest.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,31 @@ public function testBug8555(): void
452452
$this->analyse([__DIR__ . '/data/bug-8555.php'], []);
453453
}
454454

455+
public function testSelfContradiction(): void
456+
{
457+
$this->treatPhpDocTypesAsCertain = true;
458+
$this->analyse([__DIR__ . '/data/self-contradiction.php'], [
459+
[
460+
'Result of && is always false.',
461+
25,
462+
],
463+
[
464+
'Result of && is always false.',
465+
51,
466+
],
467+
[
468+
'Result of && is always false.',
469+
77,
470+
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
471+
],
472+
[
473+
'Result of && is always false.',
474+
103,
475+
'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
476+
],
477+
]);
478+
}
479+
455480
#[RequiresPhp('>= 8.1.0')]
456481
public function testBug14807(): void
457482
{
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace SelfContradiction;
4+
5+
use PhpParser\Node\Expr;
6+
use PhpParser\Node\Expr\BinaryOp\Identical;
7+
use PhpParser\Node\Expr\ClassConstFetch;
8+
use PhpParser\Node\Expr\ConstFetch;
9+
use PhpParser\Node\Scalar;
10+
11+
class AssertStaticCall {
12+
/**
13+
* @phpstan-assert-if-true Scalar|ClassConstFetch|ConstFetch $node
14+
*/
15+
private static function isSubjectNode(Expr $node): bool
16+
{
17+
return $node instanceof Scalar || $node instanceof ClassConstFetch || $node instanceof ConstFetch;
18+
}
19+
20+
/**
21+
* @return array{subject: Expr, value: Scalar|ClassConstFetch|ConstFetch}|null
22+
*/
23+
private function getSubjectAndValue(Identical $comparison): ?array
24+
{
25+
if (self::isSubjectNode($comparison->left) && !self::isSubjectNode($comparison->left)) {
26+
return ['subject' => $comparison->right, 'value' => $comparison->left];
27+
}
28+
29+
if (!self::isSubjectNode($comparison->left) && self::isSubjectNode($comparison->right)) {
30+
return ['subject' => $comparison->left, 'value' => $comparison->right];
31+
}
32+
33+
return null;
34+
}
35+
}
36+
37+
class AssertMethodCall {
38+
/**
39+
* @phpstan-assert-if-true Scalar|ClassConstFetch|ConstFetch $node
40+
*/
41+
private function isSubjectNode(Expr $node): bool
42+
{
43+
return $node instanceof Scalar || $node instanceof ClassConstFetch || $node instanceof ConstFetch;
44+
}
45+
46+
/**
47+
* @return array{subject: Expr, value: Scalar|ClassConstFetch|ConstFetch}|null
48+
*/
49+
private function getSubjectAndValue(Identical $comparison): ?array
50+
{
51+
if ($this->isSubjectNode($comparison->left) && !$this->isSubjectNode($comparison->left)) {
52+
return ['subject' => $comparison->right, 'value' => $comparison->left];
53+
}
54+
55+
if (!$this->isSubjectNode($comparison->left) && $this->isSubjectNode($comparison->right)) {
56+
return ['subject' => $comparison->left, 'value' => $comparison->right];
57+
}
58+
59+
return null;
60+
}
61+
}
62+
63+
class AssertStaticConditionalReturn {
64+
/**
65+
* @return ($node is Scalar|ClassConstFetch|ConstFetch ? true : false)
66+
*/
67+
private static function isSubjectNode(Expr $node): bool
68+
{
69+
return $node instanceof Scalar || $node instanceof ClassConstFetch || $node instanceof ConstFetch;
70+
}
71+
72+
/**
73+
* @return array{subject: Expr, value: Scalar|ClassConstFetch|ConstFetch}|null
74+
*/
75+
private function getSubjectAndValue(Identical $comparison): ?array
76+
{
77+
if (self::isSubjectNode($comparison->left) && !self::isSubjectNode($comparison->left)) {
78+
return ['subject' => $comparison->right, 'value' => $comparison->left];
79+
}
80+
81+
if (!self::isSubjectNode($comparison->left) && self::isSubjectNode($comparison->right)) {
82+
return ['subject' => $comparison->left, 'value' => $comparison->right];
83+
}
84+
85+
return null;
86+
}
87+
}
88+
89+
class AssertInstanceConditionalReturn {
90+
/**
91+
* @return ($node is Scalar|ClassConstFetch|ConstFetch ? true : false)
92+
*/
93+
private function isSubjectNode(Expr $node): bool
94+
{
95+
return $node instanceof Scalar || $node instanceof ClassConstFetch || $node instanceof ConstFetch;
96+
}
97+
98+
/**
99+
* @return array{subject: Expr, value: Scalar|ClassConstFetch|ConstFetch}|null
100+
*/
101+
private function getSubjectAndValue(Identical $comparison): ?array
102+
{
103+
if ($this->isSubjectNode($comparison->left) && !$this->isSubjectNode($comparison->left)) {
104+
return ['subject' => $comparison->right, 'value' => $comparison->left];
105+
}
106+
107+
if (!$this->isSubjectNode($comparison->left) && $this->isSubjectNode($comparison->right)) {
108+
return ['subject' => $comparison->left, 'value' => $comparison->right];
109+
}
110+
111+
return null;
112+
}
113+
}

0 commit comments

Comments
 (0)