Total Complexity | 49 |
Total Lines | 330 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
25 | class AnnotationReader |
||
26 | { |
||
27 | use Injector; |
||
28 | |||
29 | /** |
||
30 | * @var \ReflectionClass リフレクションクラスオブジェクト |
||
31 | */ |
||
32 | // private $refClass; |
||
33 | |||
34 | /** |
||
35 | * @var IAnnotatable インスタンス |
||
36 | */ |
||
37 | private $instance; |
||
38 | |||
39 | /** |
||
40 | * @var Logger ロガー |
||
41 | */ |
||
42 | // private $logger; |
||
43 | |||
44 | /** |
||
45 | * @var Container コンテナ |
||
46 | */ |
||
47 | // private $container; |
||
48 | |||
49 | /** |
||
50 | * @var array<string> 読み込み可能アノテーション情報 |
||
51 | */ |
||
52 | private $readableMap; |
||
53 | |||
54 | /** |
||
55 | * @var array<ExtendReader> 拡張アノテーションリーダー |
||
56 | */ |
||
57 | private $extendReaderMap; |
||
58 | |||
59 | /** |
||
60 | * @var array<string> アノテーション情報リスト |
||
61 | */ |
||
62 | private $annotationInfoList; |
||
63 | |||
64 | /** |
||
65 | * @var array<string> アノテーション情報リスト(拡張リーダー処理済み) |
||
66 | */ |
||
67 | private $annotationInfoExtendList; |
||
68 | |||
69 | /** |
||
70 | * @var callable 読み込み時の例外 |
||
71 | */ |
||
72 | private $exception; |
||
73 | |||
74 | /** |
||
75 | * @var string 読み込み対象アノテーションクラスパス |
||
76 | */ |
||
77 | // private $annotationClasspath; |
||
78 | |||
79 | /** |
||
80 | * @var string アクションメソッド |
||
81 | */ |
||
82 | private $actionMethod; |
||
83 | |||
84 | /** |
||
85 | * constructor |
||
86 | * @param IAnnotatable ターゲットインスタンス |
||
87 | * @param Container 依存コンテナ |
||
88 | */ |
||
89 | public function __construct(IAnnotatable $instance) |
||
90 | { |
||
91 | $this->initialize(); |
||
92 | $this->instance = $instance; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * 初期化処理 |
||
97 | */ |
||
98 | private function initialize() |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * アノテーション情報リストを返却する |
||
108 | * @param array<mixed> アノテーション情報リスト |
||
109 | */ |
||
110 | public function getAnnotationInfoList(): array |
||
111 | { |
||
112 | if (empty($this->extendReaderMap)) { |
||
113 | return $this->annotationInfoList; |
||
114 | } |
||
115 | |||
116 | if (!empty($this->annotationInfoExtendList)) { |
||
117 | return $this->annotationInfoExtendList; |
||
118 | } |
||
119 | |||
120 | foreach ($this->annotationInfoList as $key => $annotationInfo) { |
||
121 | $readerClasspath = $this->extendReaderMap[$key]; |
||
122 | $reader = new $readerClasspath(); |
||
123 | $reader->read($annotationInfo); |
||
124 | $this->annotationInfoExtendList[$key] = $reader->getAnnotationInfo(); |
||
125 | } |
||
126 | |||
127 | return $this->annotationInfoExtendList; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * 発生した例外を返却する |
||
132 | * @param ExceptionDelegator 発生した例外 |
||
133 | */ |
||
134 | public function getException() |
||
135 | { |
||
136 | return $this->exception; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * アクションメソッドを設定する |
||
141 | * @param string アクションメソッド |
||
142 | */ |
||
143 | public function setActionMethod(string $actionMethod) |
||
144 | { |
||
145 | $this->actionMethod = $actionMethod; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * 読み込み可能アノテーション情報を設定する |
||
150 | * @param string アノテーションクラスパス |
||
151 | * @param Container アノテーションクラス依存コンテナ |
||
152 | */ |
||
153 | public function readable(string $classpath, Container $container = null) |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * 拡張アノテーションリーダーを設定する |
||
160 | * @param string アノテーションクラスパス |
||
161 | * @param string 拡張アノテーションリーダークラスパス |
||
162 | */ |
||
163 | public function useExtendReader(string $annotationClasspath, string $readerClasspath) |
||
164 | { |
||
165 | $this->extendReaderMap[$annotationClasspath] = $readerClasspath; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * アノテーション情報を読み込む |
||
170 | */ |
||
171 | public function read() |
||
172 | { |
||
173 | try { |
||
174 | $this->readClass(); |
||
175 | $this->readMethod(); |
||
176 | $this->readProperty(); |
||
177 | } catch (DoctrineAnnotationException $e) { |
||
178 | $this->initialize(); |
||
179 | throw new AnnotationException($e); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * クラス情報を読み込む |
||
185 | */ |
||
186 | public function readClass() |
||
229 | } |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * メソッド情報を読み込む |
||
234 | */ |
||
235 | public function readMethod() |
||
236 | { |
||
237 | $reader = new DoctrineAnnotationReader(); |
||
238 | $refClass = new \ReflectionClass($this->instance); |
||
239 | |||
240 | while ($refClass !== false) { |
||
241 | foreach ($refClass->getMethods() as $refMethod) { |
||
242 | if ($refClass->getName() !== $refMethod->class) { |
||
243 | continue; |
||
244 | } |
||
245 | |||
246 | $annotations = $reader->getMethodAnnotations($refMethod); |
||
247 | if (empty($annotations)) { |
||
248 | continue; |
||
249 | } |
||
250 | |||
251 | for ($i = 0, $count = count($annotations); $i < $count; $i++) { |
||
252 | $annotation = $annotations[$i]; |
||
253 | |||
254 | if (!$annotation instanceof IMethod && !$annotation instanceof IMethods) { |
||
255 | continue; |
||
256 | } |
||
257 | |||
258 | // IMethodを実装している場合、アクションメソッドのアノテーション以外は読み込まない |
||
259 | // PHPのメソッドは大文字小文字を区別しないため、そのまま比較するとルーティング解決結果と実際のメソッド名が合わないケースがある |
||
260 | // PHPの仕様に合わせてメソッド名の文字列比較は小文字に変換してから行う |
||
261 | if ($annotation instanceof IMethod && strtolower($this->actionMethod) !== strtolower($refMethod->name)) { |
||
262 | continue; |
||
263 | } |
||
264 | |||
265 | // 読み込み可能なアノテーション以外は読み込まない |
||
266 | $key = get_class($annotation); |
||
267 | if (!array_key_exists($key, $this->readableMap)) { |
||
268 | continue; |
||
269 | } |
||
270 | |||
271 | $container = $this->readableMap[$key]; |
||
272 | |||
273 | try { |
||
274 | $annotation->onMethodInject($this->instance, $refMethod, $container); |
||
275 | } catch (\Exception $e) { |
||
276 | if ($this->exception === null) { |
||
277 | $this->exception = new ExceptionDelegator($this->instance, $e, $this->actionMethod); |
||
278 | } |
||
279 | continue; |
||
280 | } |
||
281 | |||
282 | // IReadを実装している場合、任意のデータを返却する |
||
283 | if ($annotation instanceof IRead) { |
||
284 | if (!array_key_exists($key, $this->annotationInfoList)) { |
||
285 | $this->annotationInfoList[$key] = []; |
||
286 | } |
||
287 | $this->annotationInfoList[$key][] = $annotation->getAnnotationInfo(); |
||
288 | } |
||
289 | } |
||
290 | } |
||
291 | |||
292 | $refClass = $refClass->getParentClass(); |
||
293 | } |
||
294 | |||
295 | // 拡張リーダー処理結果をクリアする |
||
296 | $this->annotationInfoExtendList = []; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * プロパティ情報を読み込む |
||
301 | */ |
||
302 | private function readProperty() |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 |
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