Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 51f9c39

Browse files
Test real-world NodeCallbackInvoker rule
1 parent bce73c8 commit 51f9c39

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\NodeCallbackInvoker;
7+
use PHPStan\Analyser\Scope;
8+
9+
/**
10+
* @implements Rule<Node\Stmt\Echo_>
11+
*/
12+
class NodeCallbackInvokerRule implements Rule
13+
{
14+
15+
public function getNodeType(): string
16+
{
17+
return Node\Stmt\Echo_::class;
18+
}
19+
20+
public function processNode(Node $node, NodeCallbackInvoker&Scope $scope): array
21+
{
22+
if ((bool) $node->getAttribute('virtual', false)) {
23+
// prevent infinite recursion
24+
return [
25+
RuleErrorBuilder::message('found virtual echo')
26+
->identifier('tests.nodeCallbackInvoker')
27+
->build(),
28+
];
29+
}
30+
31+
$scope->invokeNodeCallback(new Node\Stmt\Echo_(
32+
[new Node\Scalar\String_('virtual')],
33+
['startLine' => $node->getStartLine() + 1, 'virtual' => true],
34+
));
35+
36+
return [
37+
RuleErrorBuilder::message('found echo')
38+
->identifier('tests.nodeCallbackInvoker')
39+
->build(),
40+
];
41+
}
42+
43+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules;
4+
5+
use PHPStan\Testing\RuleTestCase;
6+
7+
/**
8+
* @extends RuleTestCase<NodeCallbackInvokerRule>
9+
*/
10+
class NodeCallbackInvokerRuleTest extends RuleTestCase
11+
{
12+
13+
protected function getRule(): Rule
14+
{
15+
return new NodeCallbackInvokerRule();
16+
}
17+
18+
public function testRule(): void
19+
{
20+
$this->analyse([__DIR__ . '/data/node-callback-invoker.php'], [
21+
[
22+
'found virtual echo',
23+
6,
24+
],
25+
[
26+
'found echo',
27+
5,
28+
],
29+
]);
30+
}
31+
32+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace NodeCallbackInvoker;
4+
5+
echo 'foo';
6+
7+
// something something

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /