-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOxNewCalledWithEditionNamespaceRule.php
More file actions
63 lines (50 loc) · 1.69 KB
/
OxNewCalledWithEditionNamespaceRule.php
File metadata and controls
63 lines (50 loc) · 1.69 KB
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
<?php declare(strict_types=1);
namespace dreikern\PhpStanOxid\Rule;
use dreikern\PhpStanOxid\Resolver\ResolverInterface;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
/**
* @implements \PHPStan\Rules\Rule<Node\Expr\FuncCall>
*/
class OxNewCalledWithEditionNamespaceRule implements Rule
{
private ResolverInterface $resolver;
public function __construct(ResolverInterface $resolver)
{
$this->resolver = $resolver;
}
public function getNodeType(): string
{
return Node\Expr\FuncCall::class;
}
public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Node\Name) {
return [];
}
$funcCallName = $node->name->toString();
if ('oxnew' !== strtolower($funcCallName)) {
return [];
}
$firstArg = $node->getArgs()[0];
$formatArgType = $scope->getType($firstArg->value);
if (0 === count($formatArgType->getConstantStrings())) {
return [];
}
foreach ($formatArgType->getConstantStrings() as $constantString) {
$newFqdn = $this->resolver->getUnifiedClassName($constantString->getValue());
if ($constantString->getValue() === $newFqdn) {
return [];
}
return [
RuleErrorBuilder::message(
sprintf('oxNew() call with edition namespace for class %s. Use %s instead.', $constantString->getValue(), $newFqdn)
)
->identifier('oxid.rule.OxNewCalledWithEditionNamespaceRule')
->build(),
];
}
}
}