/
RegistryGetCalledWithEditionNamespaceRule.php
66 lines (53 loc) · 1.98 KB
/
RegistryGetCalledWithEditionNamespaceRule.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
<?php declare(strict_types=1);
namespace dreikern\PhpStanOxid\Rule;
use dreikern\PhpStanOxid\Resolver\ResolverInterface;
use OxidEsales\EshopCommunity\Core\Registry;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantStringType;
/**
* @implements \PHPStan\Rules\Rule<Node\Expr\StaticCall>
*/
class RegistryGetCalledWithEditionNamespaceRule implements Rule
{
private ResolverInterface $resolver;
private ReflectionProvider $reflectionProvider;
public function __construct(ResolverInterface $resolver, ReflectionProvider $reflectionProvider)
{
$this->resolver = $resolver;
$this->reflectionProvider = $reflectionProvider;
}
public function getNodeType(): string
{
return Node\Expr\StaticCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Identifier || !$node->class instanceof Node\Name\FullyQualified) {
return [];
}
$funcCallName = $node->name->toString();
$className = $node->class->toString();
if (!class_exists($className) || !$this->reflectionProvider->getClass($className)->isSubclassOf(Registry::class)
|| 'get' !== strtolower($funcCallName)) {
return [];
}
$firstArg = $node->getArgs()[0];
$formatArgType = $scope->getType($firstArg->value);
if (!$formatArgType instanceof ConstantStringType) {
return [];
}
$newFqdn = $this->resolver->getUnifiedClassName($formatArgType->getValue());
if ($formatArgType->getValue() === $newFqdn) {
return [];
}
return [
RuleErrorBuilder::message(
sprintf('Registry::get() call with edition namespace for class %s. Use %s instead.', $formatArgType->getValue(), $newFqdn)
)->build(),
];
}
}