Skip to content

Commit a9434a5

Browse files
committed
Collectors can emit collected data with CollectedDataEmitter
Their scope is Scope&CollectedDataEmitter&DependencyTracker, so a collector can emit data for another collector besides the data it returns, like a rule can. DependencyTracker, DeclarationDependencyTracker, NodeCallbackInvoker and CollectedDataEmitter are @api-do-not-implement: only PHPStan's scope implements them. Amp-Thread-ID: https://ampcode.com/threads/T-01a1ba2c-2885-703b-90d3-0df9bf93b430
1 parent fa3efee commit a9434a5

7 files changed

Lines changed: 78 additions & 3 deletions

‎src/Analyser/CollectedDataEmitter.php‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
use PHPStan\Collectors\Collector;
77

88
/**
9-
* The interface CollectedDataEmitter can be typehinted in 2nd parameter of Rule::processNode():
9+
* The interface CollectedDataEmitter can be typehinted in 2nd parameter of Rule::processNode()
10+
* and Collector::processNode():
1011
*
1112
* ```php
1213
* public function processNode(Node $node, Scope&CollectedDataEmitter $scope): array
1314
* ```
1415
*
1516
* It allows rules to emit collected data directly, without having to write
16-
* a separate complex Collector class. The emitted data is aggregated the same way
17+
* a separate complex Collector class, and collectors to emit data for other collectors
18+
* besides the data they return. The emitted data is aggregated the same way
1719
* as data from Collectors and can be consumed by rules registered
1820
* for CollectedDataNode.
1921
*
@@ -28,6 +30,7 @@
2830
* ```
2931
*
3032
* @api
33+
* @api-do-not-implement
3134
*/
3235
interface CollectedDataEmitter
3336
{

‎src/Analyser/DeclarationDependencyTracker.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* class - referencing it, calling its methods, reading its properties - is analysed again.
1717
*
1818
* @api
19+
* @api-do-not-implement
1920
*/
2021
interface DeclarationDependencyTracker
2122
{

‎src/Analyser/DependencyTracker.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* DeclarationDependencyTracker instead.
3333
*
3434
* @api
35+
* @api-do-not-implement
3536
*/
3637
interface DependencyTracker
3738
{

‎src/Analyser/NodeCallbackInvoker.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* is actually in the code.
2929
*
3030
* @api
31+
* @api-do-not-implement
3132
*/
3233
interface NodeCallbackInvoker
3334
{

‎src/Collectors/Collector.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Collectors;
44

55
use PhpParser\Node;
6+
use PHPStan\Analyser\CollectedDataEmitter;
67
use PHPStan\Analyser\DependencyTracker;
78
use PHPStan\Analyser\Scope;
89
use PHPStan\DependencyInjection\ExtensionInterface;
@@ -38,6 +39,6 @@ public function getNodeType(): string;
3839
* @param TNodeType $node
3940
* @return TValue|null Collected data
4041
*/
41-
public function processNode(Node $node, Scope&DependencyTracker $scope);
42+
public function processNode(Node $node, Scope&CollectedDataEmitter&DependencyTracker $scope);
4243

4344
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\CollectedDataEmitter;
7+
use PHPStan\Analyser\Scope;
8+
use PHPStan\Collectors\Collector;
9+
10+
/**
11+
* @implements Collector<Node\Expr\MethodCall, never>
12+
*/
13+
final class CollectedDataEmitterCollector implements Collector
14+
{
15+
16+
public function getNodeType(): string
17+
{
18+
return Node\Expr\MethodCall::class;
19+
}
20+
21+
public function processNode(Node $node, Scope&CollectedDataEmitter $scope)
22+
{
23+
// same data as DummyCollector, but emitted for it by another collector
24+
if (!$node->name instanceof Node\Identifier) {
25+
return null;
26+
}
27+
28+
$scope->emitCollectedData(DummyCollector::class, $node->name->toString());
29+
30+
return null;
31+
}
32+
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules;
4+
5+
use PHPStan\Testing\RuleTestCase;
6+
7+
/**
8+
* @extends RuleTestCase<DummyCollectorRule>
9+
*/
10+
class CollectedDataEmitterCollectorTest extends RuleTestCase
11+
{
12+
13+
protected function getRule(): Rule
14+
{
15+
return new DummyCollectorRule();
16+
}
17+
18+
protected function getCollectors(): array
19+
{
20+
return [
21+
new CollectedDataEmitterCollector(),
22+
];
23+
}
24+
25+
public function testRule(): void
26+
{
27+
$this->analyse([__DIR__ . '/data/dummy-collector.php'], [
28+
[
29+
'2× doFoo, 2× doBar',
30+
5,
31+
],
32+
]);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)