-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bleeding edge - rule for detecting overwriting exit points in finally
- Loading branch information
1 parent
00a2eea
commit 3f712be
Showing
6 changed files
with
200 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Node; | ||
|
||
use PhpParser\NodeAbstract; | ||
use PHPStan\Analyser\StatementExitPoint; | ||
|
||
class FinallyExitPointsNode extends NodeAbstract implements VirtualNode | ||
{ | ||
|
||
/** @var StatementExitPoint[] */ | ||
private array $finallyExitPoints; | ||
|
||
/** @var StatementExitPoint[] */ | ||
private array $tryCatchExitPoints; | ||
|
||
/** | ||
* @param StatementExitPoint[] $finallyExitPoints | ||
* @param StatementExitPoint[] $tryCatchExitPoints | ||
*/ | ||
public function __construct(array $finallyExitPoints, array $tryCatchExitPoints) | ||
{ | ||
parent::__construct([]); | ||
$this->finallyExitPoints = $finallyExitPoints; | ||
$this->tryCatchExitPoints = $tryCatchExitPoints; | ||
} | ||
|
||
/** | ||
* @return StatementExitPoint[] | ||
*/ | ||
public function getFinallyExitPoints(): array | ||
{ | ||
return $this->finallyExitPoints; | ||
} | ||
|
||
/** | ||
* @return StatementExitPoint[] | ||
*/ | ||
public function getTryCatchExitPoints(): array | ||
{ | ||
return $this->tryCatchExitPoints; | ||
} | ||
|
||
public function getType(): string | ||
{ | ||
return 'PHPStan_Node_FinallyExitPointsNode'; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getSubNodeNames(): array | ||
{ | ||
return []; | ||
} | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
src/Rules/Exceptions/OverwrittenExitPointByFinallyRule.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,61 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Exceptions; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\FinallyExitPointsNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<FinallyExitPointsNode> | ||
*/ | ||
class OverwrittenExitPointByFinallyRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return FinallyExitPointsNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (count($node->getTryCatchExitPoints()) === 0) { | ||
return []; | ||
} | ||
|
||
$errors = []; | ||
foreach ($node->getTryCatchExitPoints() as $exitPoint) { | ||
$errors[] = RuleErrorBuilder::message(sprintf('This %s is overwritten by a different one in the finally block below.', $this->describeExitPoint($exitPoint->getStatement())))->line($exitPoint->getStatement()->getLine())->build(); | ||
} | ||
|
||
foreach ($node->getFinallyExitPoints() as $exitPoint) { | ||
$errors[] = RuleErrorBuilder::message(sprintf('The overwriting %s is on this line.', $this->describeExitPoint($exitPoint->getStatement())))->line($exitPoint->getStatement()->getLine())->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
private function describeExitPoint(Node\Stmt $stmt): string | ||
{ | ||
if ($stmt instanceof Node\Stmt\Return_) { | ||
return 'return'; | ||
} | ||
|
||
if ($stmt instanceof Node\Stmt\Throw_) { | ||
return 'throw'; | ||
} | ||
|
||
if ($stmt instanceof Node\Stmt\Continue_) { | ||
return 'continue'; | ||
} | ||
|
||
if ($stmt instanceof Node\Stmt\Break_) { | ||
return 'break'; | ||
} | ||
|
||
return 'exit point'; | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
tests/PHPStan/Rules/Exceptions/OverwrittenExitPointByFinallyRuleTest.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,37 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Exceptions; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<OverwrittenExitPointByFinallyRule> | ||
*/ | ||
class OverwrittenExitPointByFinallyRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new OverwrittenExitPointByFinallyRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/overwritten-exit-point.php'], [ | ||
[ | ||
'This return is overwritten by a different one in the finally block below.', | ||
11, | ||
], | ||
[ | ||
'This return is overwritten by a different one in the finally block below.', | ||
13, | ||
], | ||
[ | ||
'The overwriting return is on this line.', | ||
15, | ||
], | ||
]); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
tests/PHPStan/Rules/Exceptions/data/overwritten-exit-point.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,33 @@ | ||
<?php | ||
|
||
namespace OverwrittenExitPoint; | ||
|
||
function (): string { | ||
try { | ||
if (rand(0, 1)) { | ||
throw new \Exception(); | ||
} | ||
|
||
return 'foo'; | ||
} catch (\Exception $e) { | ||
return 'bar'; | ||
} finally { | ||
return 'baz'; | ||
} | ||
}; | ||
|
||
function (): string { | ||
try { | ||
|
||
} finally { | ||
return 'foo'; | ||
} | ||
}; | ||
|
||
function (): string { | ||
try { | ||
return 'foo'; | ||
} finally { | ||
|
||
} | ||
}; |