forked from CakeDC/cakephp-phpstan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadObjectExistsCakeClassRule.php
More file actions
146 lines (130 loc) · 4.15 KB
/
Copy pathLoadObjectExistsCakeClassRule.php
File metadata and controls
146 lines (130 loc) · 4.15 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
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
<?php
declare(strict_types=1);
/**
* Copyright 2024, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2024, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\PHPStan\Rule;
use CakeDC\PHPStan\Rule\Traits\ParseClassNameFromArgTrait;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
abstract class LoadObjectExistsCakeClassRule implements Rule
{
use ParseClassNameFromArgTrait;
/**
* @var string
*/
protected string $identifier;
/**
* @inheritDoc
*/
public function getNodeType(): string
{
return MethodCall::class;
}
/**
* @param \PhpParser\Node $node
* @param \PHPStan\Analyser\Scope $scope
* @return list<\PHPStan\Rules\IdentifierRuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
assert($node instanceof MethodCall);
$args = $node->getArgs();
if (!$node->name instanceof Node\Identifier) {
return [];
}
$reference = $scope->getType($node->var)->getReferencedClasses()[0] ?? null;
if ($reference === null) {
return [];
}
$details = $this->getDetails($reference, $args);
if (
$details === null
|| !in_array($node->name->name, $details['sourceMethods'], true)
|| !$details['alias'] instanceof Arg
|| !$details['alias']->value instanceof String_
) {
return [];
}
$inputClassName = $this->getInputClassNameFromNode($node);
if ($inputClassName === null) {
$inputClassName = $this->getInputClassName(
$details['alias']->value,
$details['options'],
);
}
if ($inputClassName === null || $this->getTargetClassName($inputClassName) !== null) {
return [];
}
return [
RuleErrorBuilder::message(sprintf(
'Call to %s::%s could not find the class for "%s"',
$reference,
$node->name->name,
$inputClassName,
))
->identifier($this->identifier)
->build(),
];
}
/**
* @param \PhpParser\Node\Scalar\String_ $nameArg
* @param \PhpParser\Node\Arg|null $options
* @return string|null
*/
protected function getInputClassName(String_ $nameArg, ?Arg $options): ?string
{
$className = $nameArg->value;
if (
$options === null
|| !$options->value instanceof Node\Expr\Array_
) {
return $className;
}
foreach ($options->value->items as $item) {
if (
!$item instanceof Node\Expr\ArrayItem
|| !$item->key instanceof String_
|| $item->key->value !== 'className'
) {
continue;
}
if ($item->value instanceof ConstFetch && $item->value->name->toString() === 'null') {
return $className;
}
return $this->parseClassNameFromExprTrait($item->value);
}
return $className;
}
/**
* @param string $name
* @return string|null
*/
abstract protected function getTargetClassName(string $name): ?string;
/**
* @param string $reference
* @param array<\PhpParser\Node\Arg> $args
* @return array{'alias': ?\PhpParser\Node\Arg, 'options': ?\PhpParser\Node\Arg, 'sourceMethods':array<string>}|null
*/
abstract protected function getDetails(string $reference, array $args): ?array;
/**
* @param \PhpParser\Node\Expr\MethodCall $node
* @return string|null
*/
protected function getInputClassNameFromNode(MethodCall $node): ?string
{
return null;
}
}