1 | <?php |
||
44 | class Addendum implements LoggerAwareInterface |
||
45 | { |
||
46 | |||
47 | const DefaultInstanceId = 'addendum'; |
||
48 | |||
49 | /** |
||
50 | * Runtime path |
||
51 | * @var string |
||
52 | */ |
||
53 | public $runtimePath = 'runtime'; |
||
54 | |||
55 | /** |
||
56 | * Namespaces to check for annotations. |
||
57 | * By default global and addendum namespace is included. |
||
58 | * @var string[] |
||
59 | */ |
||
60 | public $namespaces = [ |
||
61 | '\\', |
||
62 | TargetAnnotation::Ns |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * Translatable annotations |
||
67 | * TODO This should be moved to `maslosoft/addendum-i18n-extractor` |
||
68 | * @var string[] |
||
69 | */ |
||
70 | public $i18nAnnotations = [ |
||
71 | 'Label', |
||
72 | 'Description' |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * Plugins collection |
||
77 | * @var AddendumPlugins|mixed[] |
||
78 | */ |
||
79 | public $plugins = [ |
||
80 | 'matcher' => [ |
||
81 | ClassLiteralMatcher::class => [ |
||
82 | UseResolverDecorator::class |
||
83 | ], |
||
84 | StaticConstantMatcher::class => [ |
||
85 | UseResolverDecorator::class |
||
86 | ], |
||
87 | AnnotationsMatcher::class => [ |
||
88 | DefencerDecorator::class |
||
89 | ] |
||
90 | ] |
||
91 | ]; |
||
92 | |||
93 | /** |
||
94 | * Current instance id |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $instanceId = self::DefaultInstanceId; |
||
98 | |||
99 | /** |
||
100 | * DI |
||
101 | * @var EmbeDi |
||
102 | */ |
||
103 | protected $di = null; |
||
104 | |||
105 | /** |
||
106 | * Logger |
||
107 | * @var LoggerInterface |
||
108 | */ |
||
109 | private $_logger; |
||
110 | |||
111 | /** |
||
112 | * Cache for resolved annotations class names. |
||
113 | * Key is shor annotation name. |
||
114 | * @var string[] |
||
115 | */ |
||
116 | private static $_classnames = []; |
||
117 | |||
118 | /** |
||
119 | * This holds information about all declared classes implementing AnnotatedInterface. |
||
120 | * @see AnnotatedInterface |
||
121 | * @var string[] |
||
122 | */ |
||
123 | private static $_annotations = []; |
||
124 | |||
125 | /** |
||
126 | * Reflection annotated class cache |
||
127 | * @var ReflectionAnnotatedClass[] |
||
128 | */ |
||
129 | private static $_localCache = []; |
||
130 | |||
131 | /** |
||
132 | * Version holder |
||
133 | * @var string |
||
134 | */ |
||
135 | private static $_version = null; |
||
136 | |||
137 | /** |
||
138 | * Addendum instances |
||
139 | * @var Addendum[] |
||
140 | */ |
||
141 | private static $_a = []; |
||
142 | |||
143 | /** |
||
144 | * |
||
145 | * @param string $instanceId |
||
146 | */ |
||
147 | 43 | public function __construct($instanceId = self::DefaultInstanceId) |
|
155 | |||
156 | /** |
||
157 | * Get flyweight addendum instance of `Addendum`. |
||
158 | * Only one instance will be created for each `$instanceId` |
||
159 | * |
||
160 | * @param string $instanceId |
||
161 | * @return Addendum |
||
162 | */ |
||
163 | 30 | public static function fly($instanceId = self::DefaultInstanceId) |
|
171 | |||
172 | /** |
||
173 | * Get current addendum version. |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | public function getVersion() |
||
185 | |||
186 | /** |
||
187 | * Initialize addendum and store configuration. |
||
188 | * This should be called upon first intstance creation. |
||
189 | * |
||
190 | * @return Addendum |
||
191 | */ |
||
192 | 1 | public function init() |
|
201 | |||
202 | /** |
||
203 | * Chech if class could have annotations. |
||
204 | * **NOTE:** |
||
205 | * > This does not check check if class have annotations. It only checks if it implements `AnnotatedInterface` |
||
206 | * @param string|object $class |
||
207 | * @return bool |
||
208 | */ |
||
209 | 28 | public function hasAnnotations($class) |
|
213 | |||
214 | /** |
||
215 | * Use $class name or object to annotate class |
||
216 | * @param string|object $class |
||
217 | * @return ReflectionAnnotatedMethod|ReflectionAnnotatedProperty|ReflectionAnnotatedClass |
||
218 | */ |
||
219 | 28 | public function annotate($class) |
|
229 | |||
230 | /** |
||
231 | * Set logger |
||
232 | * |
||
233 | * @param LoggerInterface $logger |
||
234 | * @return Addendum |
||
235 | */ |
||
236 | 43 | public function setLogger(LoggerInterface $logger) |
|
241 | |||
242 | /** |
||
243 | * Get logger |
||
244 | * |
||
245 | * @return LoggerInterface logger |
||
246 | */ |
||
247 | public function getLogger() |
||
255 | |||
256 | /** |
||
257 | * Add annotations namespace. |
||
258 | * Every added namespace will be included in annotation name resolving for current instance. |
||
259 | * |
||
260 | * @param string $ns |
||
261 | * @renturn Addendum |
||
262 | */ |
||
263 | 3 | public function addNamespace($ns) |
|
281 | |||
282 | /** |
||
283 | * Add many annotaion namespaces. |
||
284 | * This is same as `addNamespace`, in that difference that many namespaces at once can be added. |
||
285 | * |
||
286 | * It accepts array of namespaces as param: |
||
287 | * ```php |
||
288 | * $nss = [ |
||
289 | * 'Maslosoft\Addendum\Annotations', |
||
290 | * 'Maslosoft\Mangan\Annotations' |
||
291 | * ]; |
||
292 | * ``` |
||
293 | * |
||
294 | * @param string[] $nss |
||
295 | * @return Addendum |
||
296 | */ |
||
297 | 28 | public function addNamespaces($nss) |
|
305 | |||
306 | /** |
||
307 | * Clear entire annotations cache. |
||
308 | */ |
||
309 | 3 | public static function cacheClear() |
|
318 | |||
319 | /** |
||
320 | * TODO This should not be static |
||
321 | * @param Reflector $reflection |
||
322 | * @return mixed[] |
||
323 | */ |
||
324 | 31 | public static function getDocComment(Reflector $reflection) |
|
341 | |||
342 | /** |
||
343 | * Set raw parsing mode |
||
344 | * @deprecated Since 4.0.4 this has no effect |
||
345 | * @param bool $enabled |
||
346 | */ |
||
347 | public static function setRawMode($enabled = true) |
||
351 | |||
352 | /** |
||
353 | * Resolve annotation class name to prefixed annotation class name |
||
354 | * |
||
355 | * @param string|bool $class |
||
356 | * @return string |
||
357 | */ |
||
358 | 35 | public static function resolveClassName($class) |
|
396 | |||
397 | 17 | private static function _getDeclaredAnnotations() |
|
412 | |||
413 | } |
||
414 |
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.