1 | <?php |
||
46 | class Addendum implements LoggerAwareInterface |
||
47 | { |
||
48 | |||
49 | const DefaultInstanceId = 'addendum'; |
||
50 | |||
51 | /** |
||
52 | * Runtime path |
||
53 | * @var string |
||
54 | */ |
||
55 | public $runtimePath = 'runtime'; |
||
56 | |||
57 | /** |
||
58 | * Whether to check modification time of file to invalidate cache. |
||
59 | * Set this to true for development, and false for production. |
||
60 | * @var bool |
||
61 | */ |
||
62 | public $checkMTime = false; |
||
63 | |||
64 | /** |
||
65 | * Namespaces to check for annotations. |
||
66 | * By default global and addendum namespace is included. |
||
67 | * @var string[] |
||
68 | */ |
||
69 | public $namespaces = [ |
||
70 | '\\', |
||
71 | TargetAnnotation::Ns |
||
72 | ]; |
||
73 | |||
74 | /** |
||
75 | * @var bool[] |
||
76 | * @internal Do not use, this for performance only |
||
77 | */ |
||
78 | public $nameKeys = []; |
||
79 | |||
80 | /** |
||
81 | * Translatable annotations |
||
82 | * TODO This should be moved to `maslosoft/addendum-i18n-extractor` |
||
83 | * @var string[] |
||
84 | */ |
||
85 | public $i18nAnnotations = [ |
||
86 | 'Label', |
||
87 | 'Description' |
||
88 | ]; |
||
89 | |||
90 | /** |
||
91 | * Plugins collection |
||
92 | * @var AddendumPlugins|mixed[] |
||
93 | */ |
||
94 | public $plugins = [ |
||
95 | 'matcher' => [ |
||
96 | ClassLiteralMatcher::class => [ |
||
97 | UseResolverDecorator::class, |
||
98 | [ |
||
99 | 'class' => ClassErrorSilencer::class, |
||
100 | 'classes' => [ |
||
101 | 'PHPMD' |
||
102 | ] |
||
103 | ] |
||
104 | ], |
||
105 | StaticConstantMatcher::class => [ |
||
106 | UseResolverDecorator::class |
||
107 | ], |
||
108 | AnnotationsMatcher::class => [ |
||
109 | DefencerDecorator::class |
||
110 | ] |
||
111 | ] |
||
112 | ]; |
||
113 | |||
114 | /** |
||
115 | * Current instance id |
||
116 | * @var string |
||
117 | */ |
||
118 | protected $instanceId = self::DefaultInstanceId; |
||
119 | |||
120 | /** |
||
121 | * DI |
||
122 | * @var EmbeDi |
||
123 | */ |
||
124 | protected $di = null; |
||
125 | |||
126 | /** |
||
127 | * Logger |
||
128 | * @var LoggerInterface |
||
129 | */ |
||
130 | private $loggerInstance; |
||
131 | |||
132 | /** |
||
133 | * Cache for resolved annotations class names. |
||
134 | * Key is shor annotation name. |
||
135 | * @var string[] |
||
136 | */ |
||
137 | private static $classNames = []; |
||
138 | |||
139 | /** |
||
140 | * This holds information about all declared classes implementing AnnotatedInterface. |
||
141 | * @see AnnotatedInterface |
||
142 | * @var string[] |
||
143 | */ |
||
144 | private static $annotations = []; |
||
145 | |||
146 | /** |
||
147 | * Version holder |
||
148 | * @var string |
||
149 | */ |
||
150 | private static $versionNumber = null; |
||
151 | |||
152 | /** |
||
153 | * Addendum instances |
||
154 | * @var Addendum[] |
||
155 | */ |
||
156 | private static $addendums = []; |
||
157 | |||
158 | /** |
||
159 | * |
||
160 | * @param string $instanceId |
||
161 | */ |
||
162 | 62 | public function __construct($instanceId = self::DefaultInstanceId) |
|
170 | |||
171 | /** |
||
172 | * Get flyweight addendum instance of `Addendum`. |
||
173 | * Only one instance will be created for each `$instanceId` |
||
174 | * |
||
175 | * @param string $instanceId |
||
176 | * @return Addendum |
||
177 | */ |
||
178 | 57 | public static function fly($instanceId = self::DefaultInstanceId) |
|
186 | |||
187 | 54 | public function getInstanceId() |
|
191 | |||
192 | /** |
||
193 | * Get current addendum version. |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public function getVersion() |
||
205 | |||
206 | /** |
||
207 | * Initialize addendum and store configuration. |
||
208 | * This should be called upon first intstance creation. |
||
209 | * |
||
210 | * @return Addendum |
||
211 | */ |
||
212 | 1 | public function init() |
|
221 | |||
222 | /** |
||
223 | * Check if class could have annotations. |
||
224 | * **NOTE:** |
||
225 | * > This does not check check if class have annotations. It only checks if it implements `AnnotatedInterface` |
||
226 | * @param string|object $class |
||
227 | * @return bool |
||
228 | */ |
||
229 | 33 | public function hasAnnotations($class) |
|
233 | |||
234 | /** |
||
235 | * Use $class name or object to annotate class |
||
236 | * @param string|object $class |
||
237 | * @return ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|ReflectionAnnotatedClass |
||
238 | */ |
||
239 | 33 | public function annotate($class) |
|
249 | |||
250 | /** |
||
251 | * Set logger |
||
252 | * |
||
253 | * @param LoggerInterface $logger |
||
254 | * @return Addendum |
||
255 | */ |
||
256 | public function setLogger(LoggerInterface $logger) |
||
261 | |||
262 | /** |
||
263 | * Get logger |
||
264 | * |
||
265 | * @return LoggerInterface logger |
||
266 | */ |
||
267 | 15 | public function getLogger() |
|
275 | |||
276 | /** |
||
277 | * Add annotations namespace. |
||
278 | * Every added namespace will be included in annotation name resolving for current instance. |
||
279 | * |
||
280 | * @param string $ns |
||
281 | * @renturn Addendum |
||
282 | */ |
||
283 | 4 | public function addNamespace($ns) |
|
309 | |||
310 | /** |
||
311 | * Add many annotation namespaces. |
||
312 | * This is same as `addNamespace`, in that difference that many namespaces at once can be added. |
||
313 | * |
||
314 | * It accepts array of namespaces as param: |
||
315 | * ```php |
||
316 | * $nss = [ |
||
317 | * 'Maslosoft\Addendum\Annotations', |
||
318 | * 'Maslosoft\Mangan\Annotations' |
||
319 | * ]; |
||
320 | * ``` |
||
321 | * |
||
322 | * @param string[] $nss |
||
323 | * @return Addendum |
||
324 | */ |
||
325 | 33 | public function addNamespaces($nss) |
|
333 | |||
334 | /** |
||
335 | * Clear entire annotations cache. |
||
336 | */ |
||
337 | 3 | public static function cacheClear() |
|
345 | |||
346 | /** |
||
347 | * TODO This should not be static |
||
348 | * @param Reflector $reflection |
||
349 | * @return mixed[] |
||
350 | */ |
||
351 | 47 | public static function getDocComment(Reflector $reflection) |
|
368 | |||
369 | /** |
||
370 | * This method has no effect |
||
371 | * @deprecated Since 4.0.4 this has no effect |
||
372 | * @param bool $enabled |
||
373 | */ |
||
374 | public static function setRawMode($enabled = true) |
||
378 | |||
379 | /** |
||
380 | * Resolve annotation class name to prefixed annotation class name |
||
381 | * |
||
382 | * @param string|bool $class |
||
383 | * @return string |
||
384 | */ |
||
385 | 54 | public static function resolveClassName($class) |
|
423 | |||
424 | 20 | private static function getDeclaredAnnotations() |
|
439 | |||
440 | } |
||
441 |
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.