1 | <?php |
||
48 | class Addendum implements LoggerAwareInterface |
||
49 | { |
||
50 | |||
51 | const DefaultInstanceId = 'addendum'; |
||
52 | |||
53 | /** |
||
54 | * Runtime path |
||
55 | * @var string |
||
56 | */ |
||
57 | public $runtimePath = 'runtime'; |
||
58 | |||
59 | /** |
||
60 | * Whether to check modification time of file to invalidate cache. |
||
61 | * Set this to true for development, and false for production. |
||
62 | * @var bool |
||
63 | */ |
||
64 | public $checkMTime = false; |
||
65 | |||
66 | /** |
||
67 | * Namespaces to check for annotations. |
||
68 | * By default global and addendum namespace is included. |
||
69 | * @var string[] |
||
70 | */ |
||
71 | public $namespaces = [ |
||
72 | '\\', |
||
73 | TargetAnnotation::Ns |
||
74 | ]; |
||
75 | |||
76 | /** |
||
77 | * @var bool[] |
||
78 | * @internal Do not use, this for performance only |
||
79 | */ |
||
80 | public $nameKeys = []; |
||
81 | |||
82 | /** |
||
83 | * Translatable annotations |
||
84 | * TODO This should be moved to `maslosoft/addendum-i18n-extractor` |
||
85 | * @var string[] |
||
86 | */ |
||
87 | public $i18nAnnotations = [ |
||
88 | 'Label', |
||
89 | 'Description' |
||
90 | ]; |
||
91 | |||
92 | /** |
||
93 | * Plugins collection |
||
94 | * @var AddendumPlugins|mixed[] |
||
95 | */ |
||
96 | public $plugins = [ |
||
97 | 'matcher' => [ |
||
98 | ClassLiteralMatcher::class => [ |
||
99 | SelfKeywordDecorator::class, |
||
100 | UseResolverDecorator::class, |
||
101 | [ |
||
102 | 'class' => ClassErrorSilencer::class, |
||
103 | 'classes' => [ |
||
104 | 'PHPMD' |
||
105 | ] |
||
106 | ] |
||
107 | ], |
||
108 | StaticConstantMatcher::class => [ |
||
109 | UseResolverDecorator::class, |
||
110 | MagicConstantDecorator::class |
||
111 | ], |
||
112 | AnnotationsMatcher::class => [ |
||
113 | DefencerDecorator::class |
||
114 | ] |
||
115 | ] |
||
116 | ]; |
||
117 | |||
118 | /** |
||
119 | * Current instance id |
||
120 | * @var string |
||
121 | */ |
||
122 | protected $instanceId = self::DefaultInstanceId; |
||
123 | |||
124 | /** |
||
125 | * DI |
||
126 | * @var EmbeDi |
||
127 | */ |
||
128 | protected $di = null; |
||
129 | |||
130 | /** |
||
131 | * Logger |
||
132 | * @var LoggerInterface |
||
133 | */ |
||
134 | private $loggerInstance; |
||
135 | |||
136 | /** |
||
137 | * Cache for resolved annotations class names. |
||
138 | * Key is short annotation name. |
||
139 | * @var string[] |
||
140 | */ |
||
141 | private static $classNames = []; |
||
142 | |||
143 | /** |
||
144 | * This holds information about all declared classes implementing AnnotatedInterface. |
||
145 | * @see AnnotatedInterface |
||
146 | * @var string[] |
||
147 | */ |
||
148 | private static $annotations = []; |
||
149 | |||
150 | /** |
||
151 | * Version holder |
||
152 | * @var string |
||
153 | */ |
||
154 | private static $versionNumber = null; |
||
155 | |||
156 | /** |
||
157 | * Addendum instances |
||
158 | * @var Addendum[] |
||
159 | */ |
||
160 | private static $addendums = []; |
||
161 | |||
162 | /** |
||
163 | * |
||
164 | * @param string $instanceId |
||
165 | */ |
||
166 | 68 | public function __construct($instanceId = self::DefaultInstanceId) |
|
174 | |||
175 | /** |
||
176 | * Get flyweight addendum instance of `Addendum`. |
||
177 | * Only one instance will be created for each `$instanceId` |
||
178 | * |
||
179 | * @param string $instanceId |
||
180 | * @return Addendum |
||
181 | */ |
||
182 | 61 | public static function fly($instanceId = self::DefaultInstanceId) |
|
183 | { |
||
184 | 61 | if (empty(self::$addendums[$instanceId])) |
|
185 | { |
||
186 | self::$addendums[$instanceId] = (new Addendum($instanceId))->init(); |
||
187 | } |
||
188 | 61 | return self::$addendums[$instanceId]; |
|
189 | } |
||
190 | |||
191 | 58 | public function getInstanceId() |
|
195 | |||
196 | /** |
||
197 | * Get current addendum version. |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | public function getVersion() |
||
209 | |||
210 | /** |
||
211 | * Initialize addendum and store configuration. |
||
212 | * This should be called upon first instance creation. |
||
213 | * |
||
214 | * @return Addendum |
||
215 | */ |
||
216 | public function init() |
||
217 | { |
||
218 | if (!$this->di->isStored($this)) |
||
219 | { |
||
220 | (new Signal())->emit(new NamespacesSignal($this)); |
||
221 | } |
||
222 | $this->di->store($this); |
||
223 | return $this; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Check if class could have annotations. |
||
228 | * **NOTE:** |
||
229 | * > This does not check check if class have annotations. It only checks if it implements `AnnotatedInterface` |
||
230 | * @param string|object $class |
||
231 | * @return bool |
||
232 | */ |
||
233 | 37 | public function hasAnnotations($class) |
|
237 | |||
238 | /** |
||
239 | * Use $class name or object to annotate class |
||
240 | * @param string|object $class |
||
241 | * @return ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|ReflectionAnnotatedClass |
||
242 | */ |
||
243 | 37 | public function annotate($class) |
|
253 | |||
254 | /** |
||
255 | * Set logger |
||
256 | * |
||
257 | * @param LoggerInterface $logger |
||
258 | * @return Addendum |
||
259 | */ |
||
260 | public function setLogger(LoggerInterface $logger) |
||
265 | |||
266 | /** |
||
267 | * Get logger |
||
268 | * |
||
269 | * @return LoggerInterface logger |
||
270 | */ |
||
271 | 52 | public function getLogger() |
|
279 | |||
280 | /** |
||
281 | * Add annotations namespace. |
||
282 | * Every added namespace will be included in annotation name resolving for current instance. |
||
283 | * |
||
284 | * @param string $ns |
||
285 | * @return Addendum |
||
286 | */ |
||
287 | 4 | public function addNamespace($ns) |
|
288 | { |
||
289 | 4 | NameNormalizer::normalize($ns, false); |
|
290 | 4 | if (!in_array($ns, $this->namespaces)) |
|
291 | { |
||
292 | $before = count($this->namespaces); |
||
293 | $this->namespaces[] = $ns; |
||
294 | $this->namespaces = array_unique($this->namespaces); |
||
295 | $after = count($this->namespaces); |
||
296 | if ($after !== $before) |
||
297 | { |
||
298 | $this->nameKeys = array_flip($this->namespaces); |
||
299 | Cache\NsCache::$addedNs = true; |
||
300 | } |
||
301 | |||
302 | $this->di->store($this, [], true); |
||
303 | |||
304 | // Reconfigure flyweight instances if present |
||
305 | if (!empty(self::$addendums[$this->instanceId])) |
||
306 | { |
||
307 | self::$addendums[$this->instanceId]->di->configure(self::$addendums[$this->instanceId]); |
||
308 | } |
||
309 | } |
||
310 | 4 | Meta::$addNs = true; |
|
311 | 4 | return $this; |
|
312 | } |
||
313 | |||
314 | /** |
||
315 | * Add many annotation namespaces. |
||
316 | * This is same as `addNamespace`, in that difference that many namespaces at once can be added. |
||
317 | * |
||
318 | * It accepts array of namespaces as param: |
||
319 | * ```php |
||
320 | * $nss = [ |
||
321 | * 'Maslosoft\Addendum\Annotations', |
||
322 | * 'Maslosoft\Mangan\Annotations' |
||
323 | * ]; |
||
324 | * ``` |
||
325 | * |
||
326 | * @param string[] $nss |
||
327 | * @return Addendum |
||
328 | */ |
||
329 | 37 | public function addNamespaces($nss) |
|
337 | |||
338 | /** |
||
339 | * Clear entire annotations cache. |
||
340 | */ |
||
341 | 3 | public static function cacheClear() |
|
349 | |||
350 | /** |
||
351 | * @param Reflector $reflection |
||
352 | * @return mixed[] |
||
353 | */ |
||
354 | 51 | public static function getDocComment(Reflector $reflection) |
|
371 | |||
372 | /** |
||
373 | * This method has no effect |
||
374 | * @deprecated Since 4.0.4 this has no effect |
||
375 | * @param bool $enabled |
||
376 | */ |
||
377 | public static function setRawMode($enabled = true) |
||
381 | |||
382 | /** |
||
383 | * Resolve annotation class name to prefixed annotation class name |
||
384 | * |
||
385 | * @param string|bool $class |
||
386 | * @return string |
||
387 | */ |
||
388 | 58 | public static function resolveClassName($class) |
|
426 | |||
427 | 55 | private static function getDeclaredAnnotations() |
|
442 | |||
443 | } |
||
444 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.