Skip to content

Commit aea75e2

Browse files
committed
Implement CompositeRule
Allows testing of rules which delegate work to NodeCallbackInvoker
1 parent c8a1ae1 commit aea75e2

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/Testing/CompositeRule.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Testing;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\NodeCallbackInvoker;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Rules\DirectRegistry;
9+
use PHPStan\Rules\Rule;
10+
use function get_class;
11+
12+
/**
13+
* Allows testing of rules which delegate work to NodeCallbackInvoker.
14+
*
15+
* @implements Rule<Node>
16+
*
17+
* @api
18+
*/
19+
final class CompositeRule implements Rule
20+
{
21+
22+
private DirectRegistry $registry;
23+
24+
public function __construct(DirectRegistry $ruleRegisty)
25+
{
26+
$this->registry = $ruleRegisty;
27+
}
28+
29+
public function getNodeType(): string
30+
{
31+
return Node::class;
32+
}
33+
34+
public function processNode(Node $node, Scope&NodeCallbackInvoker $scope): array
35+
{
36+
$errors = [];
37+
38+
$nodeType = get_class($node);
39+
foreach ($this->registry->getRules($nodeType) as $rule) {
40+
foreach ($rule->processNode($node, $scope) as $error) {
41+
$errors[] = $error;
42+
}
43+
}
44+
45+
return $errors;
46+
}
47+
48+
}

0 commit comments

Comments
 (0)