1 | <?php |
||||||
2 | |||||||
3 | namespace ByTIC\Models\SmartProperties\Definitions\Definition; |
||||||
4 | |||||||
5 | use Nip\Utility\Str; |
||||||
6 | |||||||
7 | /** |
||||||
8 | * Trait HasPropertiesNamespaces |
||||||
9 | * @package ByTIC\Models\SmartProperties\Definitions\Definition |
||||||
10 | */ |
||||||
11 | trait HasPropertiesNamespaces |
||||||
12 | { |
||||||
13 | protected $propertiesNamespaces = null; |
||||||
14 | |||||||
15 | /** |
||||||
16 | * @return null |
||||||
17 | */ |
||||||
18 | public function getPropertiesNamespaces() |
||||||
19 | { |
||||||
20 | if ($this->propertiesNamespaces === null) { |
||||||
21 | $this->initPropertiesNamespaces(); |
||||||
22 | } |
||||||
23 | |||||||
24 | return $this->propertiesNamespaces; |
||||||
25 | } |
||||||
26 | |||||||
27 | protected function initPropertiesNamespaces() |
||||||
28 | { |
||||||
29 | $this->propertiesNamespaces = $this->generatePropertiesNamespaces(); |
||||||
30 | } |
||||||
31 | |||||||
32 | protected function generatePropertiesNamespaces() |
||||||
33 | { |
||||||
34 | $directories = (array)$this->generateItemsDirectory(); |
||||||
35 | $rootNamespace = $this->getPropertyItemsRootNamespace(); |
||||||
36 | |||||||
37 | $propertiesNamespaces = []; |
||||||
38 | foreach ($directories as $namespace => $directory) { |
||||||
39 | $namespace = Str::contains($namespace, '\\') ? $namespace : $rootNamespace; |
||||||
40 | $namespace = trim($namespace, '\\'); |
||||||
41 | $propertiesNamespaces[$namespace . '\\'] = $directory; |
||||||
42 | } |
||||||
43 | return $propertiesNamespaces; |
||||||
44 | } |
||||||
45 | |||||||
46 | /** |
||||||
47 | * @return string |
||||||
48 | */ |
||||||
49 | protected function generateItemsDirectory() |
||||||
50 | { |
||||||
51 | $name = $this->getName(); |
||||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
52 | if ($name) { |
||||||
53 | $methodName = 'get' . $name . 'ItemsDirectory'; |
||||||
54 | if (method_exists($this->manager, $methodName)) { |
||||||
55 | return $this->manager->$methodName(); |
||||||
56 | } |
||||||
57 | |||||||
58 | $methodName = 'get' . Str::plural($name) . 'Directory'; |
||||||
59 | if (method_exists($this->manager, $methodName)) { |
||||||
60 | return $this->manager->$methodName(); |
||||||
61 | } |
||||||
62 | } |
||||||
63 | |||||||
64 | return $this->generateManagerDirectory() . DIRECTORY_SEPARATOR . $this->generatePropertyDirectory(); |
||||||
65 | } |
||||||
66 | |||||||
67 | /** |
||||||
68 | * @return string |
||||||
69 | */ |
||||||
70 | protected function generateManagerDirectory() |
||||||
71 | { |
||||||
72 | $reflector = new \ReflectionObject($this->manager); |
||||||
73 | |||||||
74 | return dirname($reflector->getFileName()); |
||||||
75 | } |
||||||
76 | |||||||
77 | /** |
||||||
78 | * @return string |
||||||
79 | */ |
||||||
80 | protected function generatePropertyDirectory() |
||||||
81 | { |
||||||
82 | return $this->getLabel(); |
||||||
0 ignored issues
–
show
It seems like
getLabel() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
83 | } |
||||||
84 | |||||||
85 | /** |
||||||
86 | * @return string |
||||||
87 | */ |
||||||
88 | protected function getPropertyItemsRootNamespace(): string |
||||||
89 | { |
||||||
90 | $manager = $this->getManager(); |
||||||
0 ignored issues
–
show
It seems like
getManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
91 | $method = 'get' . $this->getName() . 'ItemsRootNamespace'; |
||||||
92 | if (method_exists($manager, $method)) { |
||||||
93 | return $manager->{$method}(); |
||||||
94 | } |
||||||
95 | |||||||
96 | $method = 'get' . $this->getName() . 'Namespace'; |
||||||
97 | if (method_exists($manager, $method)) { |
||||||
98 | return $manager->{$method}(); |
||||||
99 | } |
||||||
100 | |||||||
101 | return $manager->getModelNamespace() . $this->getLabel() . '\\'; |
||||||
102 | } |
||||||
103 | } |
||||||
104 |