-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLoadComponentExistsClassRule.php
More file actions
81 lines (70 loc) · 2.09 KB
/
LoadComponentExistsClassRule.php
File metadata and controls
81 lines (70 loc) · 2.09 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
<?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\Controller;
use Cake\Controller\ComponentRegistry;
use CakeDC\PHPStan\Rule\LoadObjectExistsCakeClassRule;
use CakeDC\PHPStan\Utility\CakeNameRegistry;
class LoadComponentExistsClassRule extends LoadObjectExistsCakeClassRule
{
/**
* @var string
*/
protected string $identifier = 'cake.loadComponent.existClass';
/**
* @var array<string>
*/
protected array $sourceMethods = [
'loadComponent',
];
/**
* @var array<string>
*/
protected array $componentRegistryMethods = [
'load',
];
private readonly CakeNameRegistry $cakeNameRegistry;
/**
* @param \CakeDC\PHPStan\Utility\CakeNameRegistry $cakeNameRegistry
*/
public function __construct(?CakeNameRegistry $cakeNameRegistry = null)
{
$this->cakeNameRegistry = $cakeNameRegistry ?? CakeNameRegistry::instance();
}
/**
* @inheritDoc
*/
protected function getTargetClassName(string $name): ?string
{
return $this->cakeNameRegistry->getComponentClassName($name);
}
/**
* @inheritDoc
*/
protected function getDetails(string $reference, array $args): ?array
{
if (str_ends_with($reference, 'Controller')) {
return [
'alias' => $args[0] ?? null,
'options' => $args[1] ?? null,
'sourceMethods' => $this->sourceMethods,
];
}
if ($reference === ComponentRegistry::class) {
return [
'alias' => $args[0] ?? null,
'options' => $args[1] ?? null,
'sourceMethods' => $this->componentRegistryMethods,
];
}
return null;
}
}