1 | <?php |
||
49 | class Addendum implements LoggerAwareInterface |
||
50 | { |
||
51 | |||
52 | const DefaultInstanceId = 'addendum'; |
||
53 | |||
54 | /** |
||
55 | * Runtime path |
||
56 | * @var string |
||
57 | */ |
||
58 | public $runtimePath = 'runtime'; |
||
59 | |||
60 | /** |
||
61 | * Whether to check modification time of file to invalidate cache. |
||
62 | * Set this to true for development, and false for production. |
||
63 | * @var bool |
||
64 | */ |
||
65 | public $checkMTime = false; |
||
66 | |||
67 | /** |
||
68 | * Namespaces to check for annotations. |
||
69 | * By default global and addendum namespace is included. |
||
70 | * @var string[] |
||
71 | */ |
||
72 | public $namespaces = [ |
||
73 | '\\', |
||
74 | TargetAnnotation::Ns |
||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * @var bool[] |
||
79 | * @internal Do not use, this for performance only |
||
80 | */ |
||
81 | public $nameKeys = []; |
||
82 | |||
83 | /** |
||
84 | * Translatable annotations |
||
85 | * TODO This should be moved to `maslosoft/addendum-i18n-extractor` |
||
86 | * @var string[] |
||
87 | */ |
||
88 | public $i18nAnnotations = [ |
||
89 | 'Label', |
||
90 | 'Description' |
||
91 | ]; |
||
92 | |||
93 | /** |
||
94 | * Plugins collection |
||
95 | * @var AddendumPlugins|mixed[] |
||
96 | */ |
||
97 | public $plugins = [ |
||
98 | 'matcher' => [ |
||
99 | ClassLiteralMatcher::class => [ |
||
100 | SelfKeywordDecorator::class, |
||
101 | UseResolverDecorator::class, |
||
102 | [ |
||
103 | 'class' => ClassErrorSilencer::class, |
||
104 | 'classes' => [ |
||
105 | 'PHPMD' |
||
106 | ] |
||
107 | ] |
||
108 | ], |
||
109 | StaticConstantMatcher::class => [ |
||
110 | UseResolverDecorator::class, |
||
111 | MagicConstantDecorator::class |
||
112 | ], |
||
113 | AnnotationsMatcher::class => [ |
||
114 | DefencerDecorator::class |
||
115 | ] |
||
116 | ] |
||
117 | ]; |
||
118 | |||
119 | /** |
||
120 | * Current instance id |
||
121 | * @var string |
||
122 | */ |
||
123 | protected $instanceId = self::DefaultInstanceId; |
||
124 | |||
125 | /** |
||
126 | * DI |
||
127 | * @var EmbeDi |
||
128 | */ |
||
129 | protected $di = null; |
||
130 | |||
131 | /** |
||
132 | * Logger |
||
133 | * @var LoggerInterface |
||
134 | */ |
||
135 | private $loggerInstance; |
||
136 | |||
137 | /** |
||
138 | * Cache for resolved annotations class names. |
||
139 | * Key is shor annotation name. |
||
140 | * @var string[] |
||
141 | */ |
||
142 | private static $classNames = []; |
||
143 | |||
144 | /** |
||
145 | * This holds information about all declared classes implementing AnnotatedInterface. |
||
146 | * @see AnnotatedInterface |
||
147 | * @var string[] |
||
148 | */ |
||
149 | private static $annotations = []; |
||
150 | |||
151 | /** |
||
152 | * Version holder |
||
153 | * @var string |
||
154 | */ |
||
155 | private static $versionNumber = null; |
||
156 | |||
157 | /** |
||
158 | * Addendum instances |
||
159 | * @var Addendum[] |
||
160 | */ |
||
161 | private static $addendums = []; |
||
162 | |||
163 | /** |
||
164 | * |
||
165 | * @param string $instanceId |
||
166 | */ |
||
167 | 69 | public function __construct($instanceId = self::DefaultInstanceId) |
|
175 | |||
176 | /** |
||
177 | * Get flyweight addendum instance of `Addendum`. |
||
178 | * Only one instance will be created for each `$instanceId` |
||
179 | * |
||
180 | * @param string $instanceId |
||
181 | * @return Addendum |
||
182 | */ |
||
183 | 62 | public static function fly($instanceId = self::DefaultInstanceId) |
|
184 | { |
||
185 | 62 | if (empty(self::$addendums[$instanceId])) |
|
186 | { |
||
187 | self::$addendums[$instanceId] = (new Addendum($instanceId))->init(); |
||
188 | } |
||
189 | 62 | return self::$addendums[$instanceId]; |
|
190 | } |
||
191 | |||
192 | 59 | public function getInstanceId() |
|
196 | |||
197 | /** |
||
198 | * Get current addendum version. |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getVersion() |
||
210 | |||
211 | /** |
||
212 | * Initialize addendum and store configuration. |
||
213 | * This should be called upon first intstance creation. |
||
214 | * |
||
215 | * @return Addendum |
||
216 | */ |
||
217 | public function init() |
||
218 | { |
||
219 | if (!$this->di->isStored($this)) |
||
220 | { |
||
221 | (new Signal())->emit(new NamespacesSignal($this)); |
||
222 | } |
||
223 | $this->di->store($this); |
||
224 | return $this; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Check if class could have annotations. |
||
229 | * **NOTE:** |
||
230 | * > This does not check check if class have annotations. It only checks if it implements `AnnotatedInterface` |
||
231 | * @param string|object $class |
||
232 | * @return bool |
||
233 | */ |
||
234 | 38 | public function hasAnnotations($class) |
|
238 | |||
239 | /** |
||
240 | * Use $class name or object to annotate class |
||
241 | * @param string|object $class |
||
242 | * @return ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|ReflectionAnnotatedClass |
||
243 | */ |
||
244 | 38 | public function annotate($class) |
|
254 | |||
255 | /** |
||
256 | * Set logger |
||
257 | * |
||
258 | * @param LoggerInterface $logger |
||
259 | * @return Addendum |
||
260 | */ |
||
261 | public function setLogger(LoggerInterface $logger) |
||
266 | |||
267 | /** |
||
268 | * Get logger |
||
269 | * |
||
270 | * @return LoggerInterface logger |
||
271 | */ |
||
272 | 53 | public function getLogger() |
|
280 | |||
281 | /** |
||
282 | * Add annotations namespace. |
||
283 | * Every added namespace will be included in annotation name resolving for current instance. |
||
284 | * |
||
285 | * @param string $ns |
||
286 | * @renturn Addendum |
||
287 | */ |
||
288 | 5 | public function addNamespace($ns) |
|
314 | |||
315 | /** |
||
316 | * Add many annotation namespaces. |
||
317 | * This is same as `addNamespace`, in that difference that many namespaces at once can be added. |
||
318 | * |
||
319 | * It accepts array of namespaces as param: |
||
320 | * ```php |
||
321 | * $nss = [ |
||
322 | * 'Maslosoft\Addendum\Annotations', |
||
323 | * 'Maslosoft\Mangan\Annotations' |
||
324 | * ]; |
||
325 | * ``` |
||
326 | * |
||
327 | * @param string[] $nss |
||
328 | * @return Addendum |
||
329 | */ |
||
330 | 38 | public function addNamespaces($nss) |
|
338 | |||
339 | /** |
||
340 | * Clear entire annotations cache. |
||
341 | */ |
||
342 | 3 | public static function cacheClear() |
|
350 | |||
351 | /** |
||
352 | * TODO This should not be static |
||
353 | * @param Reflector $reflection |
||
354 | * @return mixed[] |
||
355 | */ |
||
356 | 52 | public static function getDocComment(Reflector $reflection) |
|
373 | |||
374 | /** |
||
375 | * This method has no effect |
||
376 | * @deprecated Since 4.0.4 this has no effect |
||
377 | * @param bool $enabled |
||
378 | */ |
||
379 | public static function setRawMode($enabled = true) |
||
383 | |||
384 | /** |
||
385 | * Resolve annotation class name to prefixed annotation class name |
||
386 | * |
||
387 | * @param string|bool $class |
||
388 | * @return string |
||
389 | */ |
||
390 | 59 | public static function resolveClassName($class) |
|
428 | |||
429 | 56 | private static function getDeclaredAnnotations() |
|
444 | |||
445 | } |
||
446 |
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.