forked from symplify/phpstan-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceAndValueObjectHaveSameStartsRule.php
155 lines (131 loc) · 4.79 KB
/
ServiceAndValueObjectHaveSameStartsRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace Symplify\PHPStanRules\Rules;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use Symplify\Astral\Naming\SimpleNameResolver;
use Symplify\PHPStanRules\NodeAnalyzer\Symfony\SymfonyConfigMethodCallAnalyzer;
use Symplify\PHPStanRules\NodeAnalyzer\Symfony\SymfonyConfigRectorValueObjectResolver;
use Symplify\RuleDocGenerator\Contract\ConfigurableRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Symplify\PHPStanRules\Tests\Rules\ServiceAndValueObjectHaveSameStartsRule\ServiceAndValueObjectHaveSameStartsRuleTest
*/
final class ServiceAndValueObjectHaveSameStartsRule extends AbstractSymplifyRule implements ConfigurableRuleInterface
{
/**
* @var string
*/
public const ERROR_MESSAGE = 'Value object "%s" should be named "%s" instead to respect used service';
/**
* @param string[] $classSuffixes
*/
public function __construct(
private SimpleNameResolver $simpleNameResolver,
private SymfonyConfigRectorValueObjectResolver $symfonyConfigRectorValueObjectResolver,
private SymfonyConfigMethodCallAnalyzer $symfonyConfigMethodCallAnalyzer,
private array $classSuffixes
) {
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
* @return string[]
*/
public function process(Node $node, Scope $scope): array
{
if (! $this->symfonyConfigMethodCallAnalyzer->isServicesSet($node, $scope)) {
return [];
}
$shortClass = $this->resolveSetMethodCallShortClass($node);
if ($shortClass === null) {
return [];
}
$valueObjectShortClass = $this->resolveValueObjectShortClass($node);
if ($valueObjectShortClass === null) {
return [];
}
foreach ($this->classSuffixes as $classSuffix) {
if (! \str_ends_with($shortClass, $classSuffix)) {
continue;
}
$expectedValueObjectShortClass = Strings::substring($shortClass, 0, -Strings::length($classSuffix));
if ($expectedValueObjectShortClass === $valueObjectShortClass) {
return [];
}
$errorMessage = sprintf(self::ERROR_MESSAGE, $valueObjectShortClass, $expectedValueObjectShortClass);
return [$errorMessage];
}
return [];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Make specific service suffix to use similar value object names for configuring in Symfony configs',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(SomeRector::class)
->call('configure', [[
new Another()
]]);
};
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(SomeRector::class)
->call('configure', [[
new Some()
]]);
};
CODE_SAMPLE
,
[
'classSuffixes' => ['Rector'],
]
),
]
);
}
private function resolveSetMethodCallShortClass(MethodCall $methodCall): ?string
{
$setFirstArgValue = $methodCall->args[0]->value;
if (! $setFirstArgValue instanceof ClassConstFetch) {
return null;
}
$rectorClass = $this->simpleNameResolver->getName($setFirstArgValue->class);
if ($rectorClass === null) {
return null;
}
return $this->simpleNameResolver->resolveShortName($rectorClass);
}
private function resolveValueObjectShortClass(MethodCall $methodCall): ?string
{
$valueObjectClass = $this->symfonyConfigRectorValueObjectResolver->resolveFromSetMethodCall($methodCall);
if ($valueObjectClass === null) {
return null;
}
// is it implements interface, it can have many forms
$interfaces = class_implements($valueObjectClass);
if ($interfaces !== []) {
return null;
}
return $this->simpleNameResolver->resolveShortName($valueObjectClass);
}
}