Skip to content

Commit f8ed869

Browse files
committed
#[RegisteredCollector] attribute
1 parent 23bdda8 commit f8ed869

13 files changed

+57
-47
lines changed

conf/config.level4.neon

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ includes:
22
- config.level3.neon
33

44
autowiredAttributeServices:
5-
# registers rules with #[RegisteredRule] attribute
5+
# registers rules with #[RegisteredRule] and #[RegisteredCollector] attributes
66
level: 4
77

88
conditionalTags:
@@ -17,41 +17,6 @@ parameters:
1717
checkAdvancedIsset: true
1818

1919
services:
20-
-
21-
class: PHPStan\Rules\DeadCode\ConstructorWithoutImpurePointsCollector
22-
tags:
23-
- phpstan.collector
24-
25-
-
26-
class: PHPStan\Rules\DeadCode\PossiblyPureNewCollector
27-
tags:
28-
- phpstan.collector
29-
30-
-
31-
class: PHPStan\Rules\DeadCode\FunctionWithoutImpurePointsCollector
32-
tags:
33-
- phpstan.collector
34-
35-
-
36-
class: PHPStan\Rules\DeadCode\PossiblyPureFuncCallCollector
37-
tags:
38-
- phpstan.collector
39-
40-
-
41-
class: PHPStan\Rules\DeadCode\MethodWithoutImpurePointsCollector
42-
tags:
43-
- phpstan.collector
44-
45-
-
46-
class: PHPStan\Rules\DeadCode\PossiblyPureMethodCallCollector
47-
tags:
48-
- phpstan.collector
49-
50-
-
51-
class: PHPStan\Rules\DeadCode\PossiblyPureStaticCallCollector
52-
tags:
53-
- phpstan.collector
54-
5520
-
5621
class: PHPStan\Rules\Exceptions\TooWideFunctionThrowTypeRule
5722

@@ -60,13 +25,3 @@ services:
6025

6126
-
6227
class: PHPStan\Rules\Exceptions\TooWidePropertyHookThrowTypeRule
63-
64-
-
65-
class: PHPStan\Rules\Traits\TraitDeclarationCollector
66-
tags:
67-
- phpstan.collector
68-
69-
-
70-
class: PHPStan\Rules\Traits\TraitUseCollector
71-
tags:
72-
- phpstan.collector

src/DependencyInjection/AutowiredAttributeServicesExtension.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Nette\Utils\Strings;
1212
use olvlvl\ComposerAttributeCollector\Attributes;
1313
use olvlvl\ComposerAttributeCollector\TargetMethodParameter;
14+
use PHPStan\Collectors\RegistryFactory;
1415
use PHPStan\Rules\LazyRegistry;
1516
use ReflectionClass;
1617
use stdClass;
@@ -72,6 +73,20 @@ public function loadConfiguration(): void
7273

7374
$this->processParameters($class->name, $definition, $autowiredParameters);
7475
}
76+
77+
foreach (Attributes::findTargetClasses(RegisteredCollector::class) as $class) {
78+
$attribute = $class->attribute;
79+
if ($attribute->level > $config->level) {
80+
continue;
81+
}
82+
83+
$definition = $builder->addDefinition(null)
84+
->setFactory($class->name)
85+
->setAutowired($class->name)
86+
->addTag(RegistryFactory::COLLECTOR_TAG);
87+
88+
$this->processParameters($class->name, $definition, $autowiredParameters);
89+
}
7590
}
7691

7792
/**

src/DependencyInjection/AutowiredParameter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use Attribute;
66

77
/**
8-
* Autowires constructor parameters in service classes using #[AutowiredService] or #[RegisteredRule] attributes.
8+
* Autowires constructor parameters in service classes using #[AutowiredService],
9+
* #[RegisteredRule] or #[RegisteredCollector] attributes.
910
*
1011
* If ref is omitted, it looks for parameter of the same name.
1112
*
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
use Attribute;
6+
7+
/**
8+
* Registers a collector in the DI container on the set rule level.
9+
*
10+
* Works thanks to https://github.com/ondrejmirtes/composer-attribute-collector
11+
* and AutowiredAttributeServicesExtension.
12+
*/
13+
#[Attribute(flags: Attribute::TARGET_CLASS)]
14+
final class RegisteredCollector
15+
{
16+
17+
public function __construct(public int $level)
18+
{
19+
}
20+
21+
}

src/Rules/DeadCode/ConstructorWithoutImpurePointsCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Collectors\Collector;
8+
use PHPStan\DependencyInjection\RegisteredCollector;
89
use PHPStan\Node\MethodReturnStatementsNode;
910
use function count;
1011

1112
/**
1213
* @implements Collector<MethodReturnStatementsNode, string>
1314
*/
15+
#[RegisteredCollector(level: 4)]
1416
final class ConstructorWithoutImpurePointsCollector implements Collector
1517
{
1618

src/Rules/DeadCode/FunctionWithoutImpurePointsCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Collectors\Collector;
8+
use PHPStan\DependencyInjection\RegisteredCollector;
89
use PHPStan\Node\FunctionReturnStatementsNode;
910
use function count;
1011

1112
/**
1213
* @implements Collector<FunctionReturnStatementsNode, string>
1314
*/
15+
#[RegisteredCollector(level: 4)]
1416
final class FunctionWithoutImpurePointsCollector implements Collector
1517
{
1618

src/Rules/DeadCode/MethodWithoutImpurePointsCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Collectors\Collector;
8+
use PHPStan\DependencyInjection\RegisteredCollector;
89
use PHPStan\Node\MethodReturnStatementsNode;
910
use function count;
1011

1112
/**
1213
* @implements Collector<MethodReturnStatementsNode, array{class-string, string, string}>
1314
*/
15+
#[RegisteredCollector(level: 4)]
1416
final class MethodWithoutImpurePointsCollector implements Collector
1517
{
1618

src/Rules/DeadCode/PossiblyPureFuncCallCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
use PhpParser\Node\Stmt\Expression;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Collectors\Collector;
9+
use PHPStan\DependencyInjection\RegisteredCollector;
910
use PHPStan\Reflection\ReflectionProvider;
1011

1112
/**
1213
* @implements Collector<Node\Stmt\Expression, array{string, int}>
1314
*/
15+
#[RegisteredCollector(level: 4)]
1416
final class PossiblyPureFuncCallCollector implements Collector
1517
{
1618

src/Rules/DeadCode/PossiblyPureMethodCallCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use PhpParser\Node\Stmt\Expression;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Collectors\Collector;
9+
use PHPStan\DependencyInjection\RegisteredCollector;
910

1011
/**
1112
* @implements Collector<Node\Stmt\Expression, array{non-empty-list<class-string>, string, int}>
1213
*/
14+
#[RegisteredCollector(level: 4)]
1315
final class PossiblyPureMethodCallCollector implements Collector
1416
{
1517

src/Rules/DeadCode/PossiblyPureNewCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
use PhpParser\Node\Stmt\Expression;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Collectors\Collector;
9+
use PHPStan\DependencyInjection\RegisteredCollector;
910
use PHPStan\Reflection\ReflectionProvider;
1011
use function strtolower;
1112

1213
/**
1314
* @implements Collector<Expression, array{string, int}>
1415
*/
16+
#[RegisteredCollector(level: 4)]
1517
final class PossiblyPureNewCollector implements Collector
1618
{
1719

src/Rules/DeadCode/PossiblyPureStaticCallCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use PhpParser\Node\Stmt\Expression;
77
use PHPStan\Analyser\Scope;
88
use PHPStan\Collectors\Collector;
9+
use PHPStan\DependencyInjection\RegisteredCollector;
910

1011
/**
1112
* @implements Collector<Node\Stmt\Expression, array{class-string, string, int}>
1213
*/
14+
#[RegisteredCollector(level: 4)]
1315
final class PossiblyPureStaticCallCollector implements Collector
1416
{
1517

src/Rules/Traits/TraitDeclarationCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Collectors\Collector;
8+
use PHPStan\DependencyInjection\RegisteredCollector;
89

910
/**
1011
* @implements Collector<Node\Stmt\Trait_, array{string, int}>
1112
*/
13+
#[RegisteredCollector(level: 4)]
1214
final class TraitDeclarationCollector implements Collector
1315
{
1416

src/Rules/Traits/TraitUseCollector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Collectors\Collector;
8+
use PHPStan\DependencyInjection\RegisteredCollector;
89
use function array_map;
910
use function array_values;
1011

1112
/**
1213
* @implements Collector<Node\Stmt\TraitUse, list<string>>
1314
*/
15+
#[RegisteredCollector(level: 4)]
1416
final class TraitUseCollector implements Collector
1517
{
1618

0 commit comments

Comments
 (0)