Skip to content

Commit c426883

Browse files
staabmondrejmirtes
authored andcommitted
Added regression tests
1 parent 0c1a698 commit c426883

File tree

6 files changed

+146
-3
lines changed

6 files changed

+146
-3
lines changed

src/Rules/Comparison/ConstantConditionRuleHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public function __construct(
2121

2222
public function shouldReportAlwaysTrueByDefault(Expr $expr): bool
2323
{
24+
if ($expr->getAttribute(LastConditionVisitor::ATTRIBUTE_NAME) === true) {
25+
return false;
26+
}
27+
2428
return $expr instanceof Expr\BooleanNot
2529
|| $expr instanceof Expr\BinaryOp\BooleanOr
2630
|| $expr instanceof Expr\BinaryOp\BooleanAnd

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ public function dataFileAsserts(): iterable
11491149
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8467b.php');
11501150
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8442.php');
11511151
yield from $this->gatherAssertTypes(__DIR__ . '/data/PDOStatement.php');
1152-
if (PHP_VERSION_ID >= 80000) {
1152+
if (PHP_VERSION_ID >= 80100) {
11531153
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Comparison/data/bug-8485.php');
11541154
}
11551155
yield from $this->gatherAssertTypes(__DIR__ . '/data/discussion-8447.php');

tests/PHPStan/Rules/Classes/ImpossibleInstanceOfRuleTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,24 @@ public function testBug8042(): void
380380
$this->analyse([__DIR__ . '/data/bug-8042.php'], [
381381
[
382382
'Instanceof between Bug8042\B and Bug8042\B will always evaluate to true.',
383-
18
383+
18,
384384
],
385385
[
386386
'Instanceof between Bug8042\B and Bug8042\B will always evaluate to true.',
387-
26
387+
26,
388388
],
389389
]);
390390
}
391391

392+
public function testBug7721(): void
393+
{
394+
if (PHP_VERSION_ID < 80100) {
395+
$this->markTestSkipped('This test needs PHP 8.1');
396+
}
397+
398+
$this->checkAlwaysTrueInstanceOf = true;
399+
$this->treatPhpDocTypesAsCertain = true;
400+
$this->analyse([__DIR__ . '/data/bug-7721.php'], []);
401+
}
402+
392403
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug7721;
4+
5+
final class A { }
6+
final class B { }
7+
final class C
8+
{
9+
public function __construct(public readonly A|B $value) { }
10+
}
11+
12+
$c = new C(value: new A());
13+
14+
echo match (true) {
15+
$c->value instanceof A => 'A',
16+
$c->value instanceof B => 'B'
17+
};

tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,16 @@ public function testBug8586(): void
653653
$this->checkAlwaysTrueStrictComparison = true;
654654
$this->analyse([__DIR__ . '/data/bug-8586.php'], []);
655655
}
656+
657+
public function testBug4242(): void
658+
{
659+
if (PHP_VERSION_ID < 80100) {
660+
$this->markTestSkipped('Test requires PHP 8.1.');
661+
}
662+
663+
$this->checkAlwaysTrueStrictComparison = true;
664+
$this->analyse([__DIR__ . '/data/bug-4242.php'], []);
665+
}
656666

657667
public function testBug3633(): void
658668
{
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug4242;
4+
5+
class Enum
6+
{
7+
public const TYPE_A = 1;
8+
public const TYPE_B = 2;
9+
public const TYPE_C = 3;
10+
public const TYPE_D = 4;
11+
}
12+
13+
class Data
14+
{
15+
private int $type;
16+
private int $someLoad;
17+
public function __construct(int $type)
18+
{
19+
$this->type=$type;
20+
}
21+
public function getType(): int
22+
{
23+
return $this->type;
24+
}
25+
public function someLoad(int $type): self
26+
{
27+
$this->someLoad=$type;
28+
return $this;
29+
}
30+
public function getSomeLoad(): int
31+
{
32+
return $this->someLoad;
33+
}
34+
}
35+
36+
class HelloWorld
37+
{
38+
public function case1(): void
39+
{
40+
$data=(new Data(Enum::TYPE_A));
41+
if($data->getType()===Enum::TYPE_A){
42+
$data->someLoad(4);
43+
}elseif(\in_array($data->getType(), [7,8,9,100], true)){
44+
$data->someLoad(6);
45+
}elseif($data->getType()===Enum::TYPE_B){
46+
$data->someLoad(6);
47+
}elseif($data->getType()===Enum::TYPE_C){
48+
$data->someLoad(6);
49+
}elseif($data->getType()===Enum::TYPE_D){
50+
$data->someLoad(6);
51+
}else{
52+
return;
53+
}
54+
55+
56+
if($data->getType()===Enum::TYPE_A){
57+
$data->someLoad(4);
58+
}elseif(\in_array($data->getType(), [7,8,9,100], true)){
59+
$data->someLoad(6);
60+
}elseif($data->getType()===Enum::TYPE_B){
61+
$data->someLoad(6);
62+
}elseif($data->getType()===Enum::TYPE_C){
63+
$data->someLoad(6);
64+
}elseif($data->getType()===Enum::TYPE_D){ // expected to work without an error
65+
$data->someLoad(6);
66+
}
67+
68+
}
69+
70+
public function case2(): void
71+
{
72+
$data=(new Data(Enum::TYPE_A));
73+
if($data->getType()===Enum::TYPE_A){
74+
$data->someLoad(4);
75+
}elseif(\in_array($data->getType(), [7,8,9,100], true)){
76+
$data->someLoad(6);
77+
}elseif($data->getType()===Enum::TYPE_B){
78+
$data->someLoad(6);
79+
}elseif($data->getType()===Enum::TYPE_C){
80+
$data->someLoad(6);
81+
}elseif($data->getType()===Enum::TYPE_D){
82+
$data->someLoad(6);
83+
}else{
84+
return;
85+
}
86+
87+
// code above is the same as in case1. code bellow with sorted elseif's
88+
if($data->getType()===Enum::TYPE_A){
89+
$data->someLoad(4);
90+
}elseif($data->getType()===Enum::TYPE_B){
91+
$data->someLoad(6);
92+
}elseif($data->getType()===Enum::TYPE_C){
93+
$data->someLoad(6);
94+
}elseif($data->getType()===Enum::TYPE_D){
95+
$data->someLoad(6);
96+
}elseif(\in_array($data->getType(), [7,8,9,100], true)){
97+
$data->someLoad(6);
98+
}
99+
100+
}
101+
}

0 commit comments

Comments
 (0)