Skip to content

Commit ca797f1

Browse files
committed
RegisteredRule attribute
1 parent c2b8368 commit ca797f1

16 files changed

+102
-3
lines changed

conf/config.level0.neon

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ parameters:
22
customRulesetUsed: false
33

44
rules:
5-
- PHPStan\Rules\Api\ApiInstanceofRule
65
- PHPStan\Rules\Api\ApiInstanceofTypeRule
76
- PHPStan\Rules\Api\ApiInstantiationRule
87
- PHPStan\Rules\Api\ApiClassConstFetchRule
@@ -103,6 +102,10 @@ rules:
103102
- PHPStan\Rules\Variables\UnsetRule
104103
- PHPStan\Rules\Whitespace\FileWhitespaceRule
105104

105+
autowiredAttributeServices:
106+
# registers rules with #[RegisteredRule] attribute
107+
level: 0
108+
106109
conditionalTags:
107110
PHPStan\Rules\InternalTag\RestrictedInternalClassConstantUsageExtension:
108111
phpstan.restrictedClassConstantUsageExtension: %featureToggles.internalTag%

conf/config.level1.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ rules:
1414
- PHPStan\Rules\Variables\IssetRule
1515
- PHPStan\Rules\Variables\NullCoalesceRule
1616

17+
autowiredAttributeServices:
18+
# registers rules with #[RegisteredRule] attribute
19+
level: 1
20+
1721
services:
1822
-
1923
class: PHPStan\Rules\Constants\ConstantRule

conf/config.level10.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ includes:
33

44
parameters:
55
checkImplicitMixed: true
6+
7+
autowiredAttributeServices:
8+
# registers rules with #[RegisteredRule] attribute
9+
level: 10

conf/config.level2.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ rules:
6767
- PHPStan\Rules\Pure\PureFunctionRule
6868
- PHPStan\Rules\Pure\PureMethodRule
6969

70+
autowiredAttributeServices:
71+
# registers rules with #[RegisteredRule] attribute
72+
level: 2
73+
7074
conditionalTags:
7175
PHPStan\Rules\InternalTag\RestrictedInternalPropertyUsageExtension:
7276
phpstan.restrictedPropertyUsageExtension: %featureToggles.internalTag%

conf/config.level3.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ rules:
2727
- PHPStan\Rules\Variables\ParameterOutExecutionEndTypeRule
2828
- PHPStan\Rules\Variables\VariableCloningRule
2929

30+
autowiredAttributeServices:
31+
# registers rules with #[RegisteredRule] attribute
32+
level: 3
33+
3034
parameters:
3135
checkPhpDocMethodSignatures: true
3236

conf/config.level4.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ rules:
2626
- PHPStan\Rules\TooWideTypehints\TooWidePropertyTypeRule
2727
- PHPStan\Rules\Traits\NotAnalysedTraitRule
2828

29+
autowiredAttributeServices:
30+
# registers rules with #[RegisteredRule] attribute
31+
level: 4
32+
2933
conditionalTags:
3034
PHPStan\Rules\Exceptions\TooWideFunctionThrowTypeRule:
3135
phpstan.rules.rule: %exceptions.check.tooWideThrowType%

conf/config.level5.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ rules:
1717
- PHPStan\Rules\Functions\SortParameterCastableToStringRule
1818
- PHPStan\Rules\Regexp\RegularExpressionQuotingRule
1919

20+
autowiredAttributeServices:
21+
# registers rules with #[RegisteredRule] attribute
22+
level: 5
23+
2024
services:
2125
-
2226
class: PHPStan\Rules\Functions\RandomIntParametersRule

conf/config.level6.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ rules:
1313
- PHPStan\Rules\Methods\MissingMethodReturnTypehintRule
1414
- PHPStan\Rules\Methods\MissingMethodSelfOutTypeRule
1515
- PHPStan\Rules\Properties\MissingPropertyTypehintRule
16+
17+
autowiredAttributeServices:
18+
# registers rules with #[RegisteredRule] attribute
19+
level: 6

conf/config.level7.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ includes:
44
parameters:
55
checkUnionTypes: true
66
reportMaybes: true
7+
8+
autowiredAttributeServices:
9+
# registers rules with #[RegisteredRule] attribute
10+
level: 7

conf/config.level8.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ includes:
33

44
parameters:
55
checkNullables: true
6+
7+
autowiredAttributeServices:
8+
# registers rules with #[RegisteredRule] attribute
9+
level: 8

conf/config.level9.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ includes:
33

44
parameters:
55
checkExplicitMixed: true
6+
7+
autowiredAttributeServices:
8+
# registers rules with #[RegisteredRule] attribute
9+
level: 9

conf/config.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ extensions:
218218
autowiredAttributeServices: PHPStan\DependencyInjection\AutowiredAttributeServicesExtension
219219
validateServiceTags: PHPStan\DependencyInjection\ValidateServiceTagsExtension
220220

221+
autowiredAttributeServices:
222+
level: null
223+
221224
conditionalTags:
222225
PHPStan\Rules\Exceptions\MissingCheckedExceptionInFunctionThrowsRule:
223226
phpstan.rules.rule: %exceptions.check.missingCheckedExceptionInThrows%

src/DependencyInjection/AutowiredAttributeServicesExtension.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,35 @@
66
use Nette\DI\Definitions\Reference;
77
use Nette\DI\Definitions\ServiceDefinition;
88
use Nette\DI\Helpers;
9+
use Nette\Schema\Expect;
10+
use Nette\Schema\Schema;
911
use Nette\Utils\Strings;
1012
use olvlvl\ComposerAttributeCollector\Attributes;
1113
use olvlvl\ComposerAttributeCollector\TargetMethodParameter;
14+
use PHPStan\Rules\LazyRegistry;
1215
use ReflectionClass;
16+
use stdClass;
1317
use function strtolower;
1418
use function substr;
1519

1620
final class AutowiredAttributeServicesExtension extends CompilerExtension
1721
{
1822

23+
public function getConfigSchema(): Schema
24+
{
25+
return Expect::structure([
26+
'level' => Expect::int()->nullable()->required(),
27+
]);
28+
}
29+
1930
public function loadConfiguration(): void
2031
{
2132
require_once __DIR__ . '/../../vendor/attributes.php';
22-
$autowiredServiceClasses = Attributes::findTargetClasses(AutowiredService::class);
2333
$builder = $this->getContainerBuilder();
2434

2535
$autowiredParameters = Attributes::findTargetMethodParameters(AutowiredParameter::class);
2636

27-
foreach ($autowiredServiceClasses as $class) {
37+
foreach (Attributes::findTargetClasses(AutowiredService::class) as $class) {
2838
$reflection = new ReflectionClass($class->name);
2939
$attribute = $class->attribute;
3040

@@ -42,6 +52,24 @@ public function loadConfiguration(): void
4252
$definition->addTag($tag);
4353
}
4454
}
55+
56+
/** @var stdClass&object{level: int|null} $config */
57+
$config = $this->getConfig();
58+
if ($config->level === null) {
59+
return;
60+
}
61+
62+
foreach (Attributes::findTargetClasses(RegisteredRule::class) as $class) {
63+
$attribute = $class->attribute;
64+
if ($attribute->level > $config->level) {
65+
continue;
66+
}
67+
68+
$builder->addDefinition(null)
69+
->setFactory($class->name)
70+
->setAutowired($class->name)
71+
->addTag(LazyRegistry::RULE_TAG);
72+
}
4573
}
4674

4775
/**
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 rule 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 RegisteredRule
15+
{
16+
17+
public function __construct(public int $level)
18+
{
19+
}
20+
21+
}

src/Rules/Api/ApiInstanceofRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\DependencyInjection\RegisteredRule;
78
use PHPStan\Reflection\ClassReflection;
89
use PHPStan\Reflection\ReflectionProvider;
910
use PHPStan\Rules\IdentifierRuleError;
@@ -19,6 +20,7 @@
1920
/**
2021
* @implements Rule<Node\Expr\Instanceof_>
2122
*/
23+
#[RegisteredRule(level: 0)]
2224
final class ApiInstanceofRule implements Rule
2325
{
2426

src/Rules/Classes/UnusedConstructorParametersRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpParser\Node\Expr\Variable;
77
use PhpParser\Node\Param;
88
use PHPStan\Analyser\Scope;
9+
use PHPStan\DependencyInjection\RegisteredRule;
910
use PHPStan\Internal\SprintfHelper;
1011
use PHPStan\Node\InClassMethodNode;
1112
use PHPStan\Rules\Rule;
@@ -20,6 +21,7 @@
2021
/**
2122
* @implements Rule<InClassMethodNode>
2223
*/
24+
#[RegisteredRule(level: 1)]
2325
final class UnusedConstructorParametersRule implements Rule
2426
{
2527

0 commit comments

Comments
 (0)