-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbd9a68
commit 9be1376
Showing
5 changed files
with
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Throw_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use function implode; | ||
use function sprintf; | ||
|
||
/** @implements Rule<Throw_> */ | ||
class ScopeFunctionCallStackRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Throw_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$messages = []; | ||
foreach ($scope->getFunctionCallStack() as $reflection) { | ||
if ($reflection instanceof FunctionReflection) { | ||
$messages[] = $reflection->getName(); | ||
continue; | ||
} | ||
|
||
$messages[] = sprintf('%s::%s', $reflection->getDeclaringClass()->getDisplayName(), $reflection->getName()); | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(implode("\n", $messages))->identifier('dummy')->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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules; | ||
|
||
use PHPStan\Testing\RuleTestCase; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<ScopeFunctionCallStackRule> | ||
*/ | ||
class ScopeFunctionCallStackRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new ScopeFunctionCallStackRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
if (PHP_VERSION_ID < 80000) { | ||
$this->markTestSkipped('Test requires PHP 8.0.'); | ||
} | ||
|
||
$this->analyse([__DIR__ . '/data/scope-function-call-stack.php'], [ | ||
[ | ||
"var_dump\nprint_r\nsleep", | ||
7, | ||
], | ||
]); | ||
} | ||
|
||
} |
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,8 @@ | ||
<?php // lint >= 8.0 | ||
|
||
namespace ScopeFunctionCallStack; | ||
|
||
function (): void | ||
{ | ||
var_dump(print_r(sleep(throw new \Exception()))); | ||
}; |