-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathStringToClassRule.php
More file actions
58 lines (49 loc) · 1.4 KB
/
StringToClassRule.php
File metadata and controls
58 lines (49 loc) · 1.4 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
<?php
declare(strict_types=1);
namespace SlamPhpStan;
use PhpParser\Node;
use PhpParser\Node\Scalar\String_;
use PHPStan\Analyser\Scope;
use PHPStan\Broker\Broker;
use PHPStan\Rules\Rule;
/**
* @implements Rule<String_>
*/
final class StringToClassRule implements Rule
{
private Broker $broker;
private bool $strictCasing;
public function __construct(Broker $broker, bool $strictCasing)
{
$this->broker = $broker;
$this->strictCasing = $strictCasing;
}
public function getNodeType(): string
{
return String_::class;
}
/**
* @return string[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
$className = $node->value;
if (isset($className[0]) && '\\' === $className[0]) {
$className = \substr($className, 1);
}
$messages = [];
if (! \preg_match('/^\\w.+\\w$/u', $className)) {
return $messages;
}
if (! $this->broker->hasClass($className)) {
return $messages;
}
$classRef = $this->broker->getClass($className)->getNativeReflection();
if (($classRef->isInternal() || $this->strictCasing) && $classRef->getName() !== $className) {
return $messages;
}
return [
\sprintf('Class %s should be written with ::class notation, string found.', $className),
];
}
}