-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scope - function call stack includes parameters too
- Loading branch information
1 parent
98a1037
commit b87e5c4
Showing
8 changed files
with
114 additions
and
17 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
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
42 changes: 42 additions & 0 deletions
42
tests/PHPStan/Rules/ScopeFunctionCallStackWithParametersRule.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,42 @@ | ||
<?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 PHPStan\ShouldNotHappenException; | ||
use function implode; | ||
use function sprintf; | ||
|
||
/** @implements Rule<Throw_> */ | ||
class ScopeFunctionCallStackWithParametersRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Throw_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$messages = []; | ||
foreach ($scope->getFunctionCallStackWithParameters() as [$reflection, $parameter]) { | ||
if ($parameter === null) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
if ($reflection instanceof FunctionReflection) { | ||
$messages[] = sprintf('%s ($%s)', $reflection->getName(), $parameter->getName()); | ||
continue; | ||
} | ||
|
||
$messages[] = sprintf('%s::%s ($%s)', $reflection->getDeclaringClass()->getDisplayName(), $reflection->getName(), $parameter->getName()); | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(implode("\n", $messages))->identifier('dummy')->build(), | ||
]; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
tests/PHPStan/Rules/ScopeFunctionCallStackWithParametersRuleTest.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,41 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules; | ||
|
||
use PHPStan\Testing\RuleTestCase; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<ScopeFunctionCallStackWithParametersRule> | ||
*/ | ||
class ScopeFunctionCallStackWithParametersRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new ScopeFunctionCallStackWithParametersRule(); | ||
} | ||
|
||
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 (\$value)\nprint_r (\$value)\nsleep (\$seconds)", | ||
7, | ||
], | ||
[ | ||
"var_dump (\$value)\nprint_r (\$value)\nsleep (\$seconds)", | ||
10, | ||
], | ||
[ | ||
"var_dump (\$value)\nprint_r (\$value)\nsleep (\$seconds)", | ||
13, | ||
], | ||
]); | ||
} | ||
|
||
} |