-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for throw expression (PHP 8.0)
- Loading branch information
1 parent
7a38424
commit 5f2d42c
Showing
8 changed files
with
146 additions
and
1 deletion.
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,40 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Exceptions; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<Node\Expr\Throw_> | ||
*/ | ||
class ThrowExpressionRule implements Rule | ||
{ | ||
|
||
private PhpVersion $phpVersion; | ||
|
||
public function __construct(PhpVersion $phpVersion) | ||
{ | ||
$this->phpVersion = $phpVersion; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\Throw_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->phpVersion->supportsThrowExpression()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message('Throw expression is supported only on PHP 8.0 and later.')->nonIgnorable()->build(), | ||
]; | ||
} | ||
|
||
} |
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,21 @@ | ||
<?php // lint >= 8.0 | ||
|
||
namespace ThrowExpr; | ||
|
||
use function PHPStan\Analyser\assertType; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo(bool $b): void | ||
{ | ||
$result = $b ? true : throw new \Exception(); | ||
assertType('true', $result); | ||
} | ||
|
||
public function doBar(): void | ||
{ | ||
assertType('*NEVER*', throw new \Exception()); | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
tests/PHPStan/Rules/Exceptions/ThrowExpressionRuleTest.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,53 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Exceptions; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<ThrowExpressionRule> | ||
*/ | ||
class ThrowExpressionRuleTest extends RuleTestCase | ||
{ | ||
|
||
/** @var PhpVersion */ | ||
private $phpVersion; | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new ThrowExpressionRule($this->phpVersion); | ||
} | ||
|
||
public function dataRule(): array | ||
{ | ||
return [ | ||
[ | ||
70400, | ||
[ | ||
[ | ||
'Throw expression is supported only on PHP 8.0 and later.', | ||
10, | ||
], | ||
], | ||
], | ||
[ | ||
80000, | ||
[], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataRule | ||
* @param int $phpVersion | ||
* @param mixed[] $expectedErrors | ||
*/ | ||
public function testRule(int $phpVersion, array $expectedErrors): void | ||
{ | ||
$this->phpVersion = new PhpVersion($phpVersion); | ||
$this->analyse([__DIR__ . '/data/throw-expr.php'], $expectedErrors); | ||
} | ||
|
||
} |
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,15 @@ | ||
<?php | ||
|
||
namespace ThrowExpr; | ||
|
||
class Bar | ||
{ | ||
|
||
public function doFoo(bool $b): void | ||
{ | ||
$b ? true : throw new \Exception(); | ||
|
||
throw new \Exception(); | ||
} | ||
|
||
} |