Total Complexity | 52 |
Total Lines | 325 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AnnotationReader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AnnotationReader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class AnnotationReader |
||
27 | { |
||
28 | use Injector; |
||
29 | |||
30 | /** |
||
31 | * @var IAnnotatable インスタンス |
||
32 | */ |
||
33 | private $instance; |
||
34 | |||
35 | /** |
||
36 | * @var array<string> 読み込み可能アノテーション情報 |
||
37 | */ |
||
38 | private $readableMap; |
||
39 | |||
40 | /** |
||
41 | * @var array<ExtendReader> 拡張アノテーションリーダー |
||
42 | */ |
||
43 | private $extendReaderMap; |
||
44 | |||
45 | /** |
||
46 | * @var array<string> アノテーション情報リスト |
||
47 | */ |
||
48 | private $annotationInfoList; |
||
49 | |||
50 | /** |
||
51 | * @var array<string> アノテーション情報リスト(拡張リーダー処理済み) |
||
52 | */ |
||
53 | private $annotationInfoExtendList; |
||
54 | |||
55 | /** |
||
56 | * @var callable 読み込み時の例外 |
||
57 | */ |
||
58 | private $exception; |
||
59 | |||
60 | /** |
||
61 | * @var string アクションメソッド |
||
62 | */ |
||
63 | private $actionMethod; |
||
64 | |||
65 | /** |
||
66 | * @var Container デフォルト依存コンテナ |
||
67 | */ |
||
68 | private $defaultContainer; |
||
69 | |||
70 | /** |
||
71 | * constructor |
||
72 | * @param IAnnotatable ターゲットインスタンス |
||
73 | */ |
||
74 | public function __construct(IAnnotatable $instance) |
||
75 | { |
||
76 | $this->initialize(); |
||
77 | $this->instance = $instance; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * 初期化処理 |
||
82 | */ |
||
83 | private function initialize() |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * アノテーション情報リストを返却する |
||
93 | * @param array<mixed> アノテーション情報リスト |
||
94 | */ |
||
95 | public function getAnnotationInfoList(): array |
||
96 | { |
||
97 | if (empty($this->extendReaderMap)) { |
||
98 | return $this->annotationInfoList; |
||
99 | } |
||
100 | |||
101 | foreach ($this->annotationInfoList as $key => $annotationInfo) { |
||
102 | if (!array_key_exists($key, $this->extendReaderMap)) { |
||
103 | continue; |
||
104 | } |
||
105 | $readerClasspath = $this->extendReaderMap[$key]; |
||
106 | $refClass = new \ReflectionClass($readerClasspath); |
||
107 | $reader = $refClass->newInstance(); |
||
108 | $reader->read($annotationInfo); |
||
109 | $this->annotationInfoList[$key] = $reader->getAnnotationInfo(); |
||
110 | } |
||
111 | |||
112 | return $this->annotationInfoList; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * 発生した例外を返却する |
||
117 | * @param ExceptionDelegator 発生した例外 |
||
118 | */ |
||
119 | public function getException() |
||
120 | { |
||
121 | return $this->exception; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * アクションメソッドを設定する |
||
126 | * @param string アクションメソッド |
||
127 | */ |
||
128 | public function setActionMethod(string $actionMethod) |
||
129 | { |
||
130 | $this->actionMethod = $actionMethod; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * 読み込み可能アノテーション情報を設定する |
||
135 | * @param string アノテーションクラスパス |
||
136 | * @param Container アノテーションクラス依存コンテナ |
||
137 | */ |
||
138 | public function readable(string $classpath, Container $container = null) |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * 拡張アノテーションリーダーを設定する |
||
145 | * @param string アノテーションクラスパス |
||
146 | * @param string 拡張アノテーションリーダークラスパス |
||
147 | */ |
||
148 | public function useExtendReader(string $annotationClasspath, string $readerClasspath) |
||
149 | { |
||
150 | $this->extendReaderMap[$annotationClasspath] = $readerClasspath; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * アノテーション情報を読み込む |
||
155 | */ |
||
156 | public function read() |
||
157 | { |
||
158 | try { |
||
159 | $this->readClass(); |
||
160 | $this->readMethod(); |
||
161 | $this->readProperty(); |
||
162 | } catch (DoctrineAnnotationException $e) { |
||
163 | $this->initialize(); |
||
164 | throw new AnnotationException($e); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * クラス情報を読み込む |
||
170 | */ |
||
171 | public function readClass() |
||
219 | } |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * メソッド情報を読み込む |
||
224 | */ |
||
225 | public function readMethod() |
||
226 | { |
||
227 | $reader = new DoctrineAnnotationReader(); |
||
228 | $refClass = new \ReflectionClass($this->instance); |
||
229 | |||
230 | while ($refClass !== false) { |
||
231 | foreach ($refClass->getMethods() as $refMethod) { |
||
232 | if ($refClass->getName() !== $refMethod->class) { |
||
233 | continue; |
||
234 | } |
||
235 | |||
236 | $annotations = $reader->getMethodAnnotations($refMethod); |
||
237 | if (empty($annotations)) { |
||
238 | continue; |
||
239 | } |
||
240 | |||
241 | for ($i = 0, $count = count($annotations); $i < $count; $i++) { |
||
242 | $annotation = $annotations[$i]; |
||
243 | |||
244 | if (!$annotation instanceof IMethod && !$annotation instanceof IMethods) { |
||
245 | continue; |
||
246 | } |
||
247 | |||
248 | // IMethodを実装している場合、アクションメソッドのアノテーション以外は読み込まない |
||
249 | // PHPのメソッドは大文字小文字を区別しないため、そのまま比較するとルーティング解決結果と実際のメソッド名が合わないケースがある |
||
250 | // PHPの仕様に合わせてメソッド名の文字列比較は小文字に変換してから行う |
||
251 | if ($annotation instanceof IMethod && strtolower($this->actionMethod) !== strtolower($refMethod->name)) { |
||
252 | continue; |
||
253 | } |
||
254 | |||
255 | // 読み込み可能なアノテーション以外は読み込まない |
||
256 | $key = get_class($annotation); |
||
257 | $container = null; |
||
258 | if (!array_key_exists($key, $this->readableMap)) { |
||
259 | if ($annotation instanceof IExtension) { |
||
260 | $container = $this->defaultContainer; |
||
261 | } else { |
||
262 | continue; |
||
263 | } |
||
264 | } else { |
||
265 | $container = $this->readableMap[$key]; |
||
266 | } |
||
267 | |||
268 | try { |
||
269 | $annotation->onMethodInject($this->instance, $refMethod, $container); |
||
270 | } catch (\Exception $e) { |
||
271 | if ($this->exception === null) { |
||
272 | $this->exception = new ExceptionDelegator($this->instance, $e, $this->actionMethod); |
||
273 | } |
||
274 | continue; |
||
275 | } |
||
276 | |||
277 | // IReadを実装している場合、任意のデータを返却する |
||
278 | if ($annotation instanceof IRead) { |
||
279 | if (!array_key_exists($key, $this->annotationInfoList)) { |
||
280 | $this->annotationInfoList[$key] = []; |
||
281 | } |
||
282 | $this->annotationInfoList[$key][] = $annotation->getAnnotationInfo(); |
||
283 | } |
||
284 | } |
||
285 | } |
||
286 | |||
287 | $refClass = $refClass->getParentClass(); |
||
288 | } |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * プロパティ情報を読み込む |
||
293 | */ |
||
294 | private function readProperty() |
||
351 | } |
||
352 | } |
||
353 | } |
||
354 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths