-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to remember constant and impure expressions in match condition
Closes phpstan/phpstan#4451 Closes phpstan/phpstan#9357 Closes phpstan/phpstan#9007
- Loading branch information
1 parent
b5cf52b
commit 0cdda0b
Showing
12 changed files
with
236 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Node\Expr; | ||
|
||
use PhpParser\Node\Expr; | ||
use PHPStan\Node\VirtualNode; | ||
use PHPStan\Type\Type; | ||
|
||
class AlwaysRememberedExpr extends Expr implements VirtualNode | ||
{ | ||
|
||
public function __construct(private Expr $expr, private Type $type, private Type $nativeType) | ||
{ | ||
parent::__construct([]); | ||
} | ||
|
||
public function getExpr(): Expr | ||
{ | ||
return $this->expr; | ||
} | ||
|
||
public function getExprType(): Type | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function getNativeExprType(): Type | ||
{ | ||
return $this->nativeType; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return 'PHPStan_Node_AlwaysRememberedExpr'; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getSubNodeNames(): array | ||
{ | ||
return []; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
tests/PHPStan/Rules/Comparison/MatchExpressionDoNotRememberPossiblyImpureValuesRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Comparison; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use function array_merge; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<MatchExpressionRule> | ||
*/ | ||
class MatchExpressionDoNotRememberPossiblyImpureValuesRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return self::getContainer()->getByType(MatchExpressionRule::class); | ||
} | ||
|
||
public function testBug9357(): void | ||
{ | ||
if (PHP_VERSION_ID < 80100) { | ||
$this->markTestSkipped('Test requires PHP 8.1.'); | ||
} | ||
|
||
$this->analyse([__DIR__ . '/data/bug-9357.php'], []); | ||
} | ||
|
||
public function testBug9007(): void | ||
{ | ||
if (PHP_VERSION_ID < 80100) { | ||
$this->markTestSkipped('Test requires PHP 8.1.'); | ||
} | ||
|
||
$this->analyse([__DIR__ . '/data/bug-9007.php'], []); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return array_merge( | ||
parent::getAdditionalConfigFiles(), | ||
[ | ||
__DIR__ . '/doNotRememberPossiblyImpureValues.neon', | ||
], | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Bug4451; | ||
|
||
class HelloWorld | ||
{ | ||
public function sayHello(): int | ||
{ | ||
$verified = fn(): bool => rand() === 1; | ||
|
||
return match([$verified(), $verified()]) { | ||
[true, true] => 1, | ||
[true, false] => 2, | ||
[false, true] => 3, | ||
[false, false] => 4, | ||
}; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php // lint >= 8.1 | ||
|
||
namespace Bug9007; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
enum Country: string { | ||
case Usa = 'USA'; | ||
case Canada = 'CAN'; | ||
case Mexico = 'MEX'; | ||
} | ||
|
||
function doStuff(string $countryString): int { | ||
assertType(Country::class, Country::from($countryString)); | ||
return match (Country::from($countryString)) { | ||
Country::Usa => 1, | ||
Country::Canada => 2, | ||
Country::Mexico => 3, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php // lint >= 8.1 | ||
|
||
namespace Bug9357; | ||
|
||
enum MyEnum: string { | ||
case A = 'a'; | ||
case B = 'b'; | ||
} | ||
|
||
class My { | ||
/** @phpstan-impure */ | ||
public function getType(): MyEnum { | ||
echo "called!"; | ||
return rand() > 0.5 ? MyEnum::A : MyEnum::B; | ||
} | ||
} | ||
|
||
function test(My $m): void { | ||
echo match ($m->getType()) { | ||
MyEnum::A => 1, | ||
MyEnum::B => 2, | ||
}; | ||
} |
2 changes: 2 additions & 0 deletions
2
tests/PHPStan/Rules/Comparison/doNotRememberPossiblyImpureValues.neon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
parameters: | ||
rememberPossiblyImpureFunctionValues: false |