| Total Complexity | 50 |
| Total Lines | 334 |
| 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 | if (!array_key_exists($key, $this->extendReaderMap)) { |
||
| 122 | continue; |
||
| 123 | } |
||
| 124 | $readerClasspath = $this->extendReaderMap[$key]; |
||
| 125 | $refClass = new \ReflectionClass($readerClasspath); |
||
| 126 | $reader = $refClass->newInstance(); |
||
| 127 | $reader->read($annotationInfo); |
||
| 128 | $this->annotationInfoExtendList[$key] = $reader->getAnnotationInfo(); |
||
| 129 | } |
||
| 130 | |||
| 131 | return $this->annotationInfoExtendList; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * 発生した例外を返却する |
||
| 136 | * @param ExceptionDelegator 発生した例外 |
||
| 137 | */ |
||
| 138 | public function getException() |
||
| 139 | { |
||
| 140 | return $this->exception; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * アクションメソッドを設定する |
||
| 145 | * @param string アクションメソッド |
||
| 146 | */ |
||
| 147 | public function setActionMethod(string $actionMethod) |
||
| 148 | { |
||
| 149 | $this->actionMethod = $actionMethod; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * 読み込み可能アノテーション情報を設定する |
||
| 154 | * @param string アノテーションクラスパス |
||
| 155 | * @param Container アノテーションクラス依存コンテナ |
||
| 156 | */ |
||
| 157 | public function readable(string $classpath, Container $container = null) |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * 拡張アノテーションリーダーを設定する |
||
| 164 | * @param string アノテーションクラスパス |
||
| 165 | * @param string 拡張アノテーションリーダークラスパス |
||
| 166 | */ |
||
| 167 | public function useExtendReader(string $annotationClasspath, string $readerClasspath) |
||
| 168 | { |
||
| 169 | $this->extendReaderMap[$annotationClasspath] = $readerClasspath; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * アノテーション情報を読み込む |
||
| 174 | */ |
||
| 175 | public function read() |
||
| 176 | { |
||
| 177 | try { |
||
| 178 | $this->readClass(); |
||
| 179 | $this->readMethod(); |
||
| 180 | $this->readProperty(); |
||
| 181 | } catch (DoctrineAnnotationException $e) { |
||
| 182 | $this->initialize(); |
||
| 183 | throw new AnnotationException($e); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * クラス情報を読み込む |
||
| 189 | */ |
||
| 190 | public function readClass() |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * メソッド情報を読み込む |
||
| 238 | */ |
||
| 239 | public function readMethod() |
||
| 240 | { |
||
| 241 | $reader = new DoctrineAnnotationReader(); |
||
| 242 | $refClass = new \ReflectionClass($this->instance); |
||
| 243 | |||
| 244 | while ($refClass !== false) { |
||
| 245 | foreach ($refClass->getMethods() as $refMethod) { |
||
| 246 | if ($refClass->getName() !== $refMethod->class) { |
||
| 247 | continue; |
||
| 248 | } |
||
| 249 | |||
| 250 | $annotations = $reader->getMethodAnnotations($refMethod); |
||
| 251 | if (empty($annotations)) { |
||
| 252 | continue; |
||
| 253 | } |
||
| 254 | |||
| 255 | for ($i = 0, $count = count($annotations); $i < $count; $i++) { |
||
| 256 | $annotation = $annotations[$i]; |
||
| 257 | |||
| 258 | if (!$annotation instanceof IMethod && !$annotation instanceof IMethods) { |
||
| 259 | continue; |
||
| 260 | } |
||
| 261 | |||
| 262 | // IMethodを実装している場合、アクションメソッドのアノテーション以外は読み込まない |
||
| 263 | // PHPのメソッドは大文字小文字を区別しないため、そのまま比較するとルーティング解決結果と実際のメソッド名が合わないケースがある |
||
| 264 | // PHPの仕様に合わせてメソッド名の文字列比較は小文字に変換してから行う |
||
| 265 | if ($annotation instanceof IMethod && strtolower($this->actionMethod) !== strtolower($refMethod->name)) { |
||
| 266 | continue; |
||
| 267 | } |
||
| 268 | |||
| 269 | // 読み込み可能なアノテーション以外は読み込まない |
||
| 270 | $key = get_class($annotation); |
||
| 271 | if (!array_key_exists($key, $this->readableMap)) { |
||
| 272 | continue; |
||
| 273 | } |
||
| 274 | |||
| 275 | $container = $this->readableMap[$key]; |
||
| 276 | |||
| 277 | try { |
||
| 278 | $annotation->onMethodInject($this->instance, $refMethod, $container); |
||
| 279 | } catch (\Exception $e) { |
||
| 280 | if ($this->exception === null) { |
||
| 281 | $this->exception = new ExceptionDelegator($this->instance, $e, $this->actionMethod); |
||
| 282 | } |
||
| 283 | continue; |
||
| 284 | } |
||
| 285 | |||
| 286 | // IReadを実装している場合、任意のデータを返却する |
||
| 287 | if ($annotation instanceof IRead) { |
||
| 288 | if (!array_key_exists($key, $this->annotationInfoList)) { |
||
| 289 | $this->annotationInfoList[$key] = []; |
||
| 290 | } |
||
| 291 | $this->annotationInfoList[$key][] = $annotation->getAnnotationInfo(); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | $refClass = $refClass->getParentClass(); |
||
| 297 | } |
||
| 298 | |||
| 299 | // 拡張リーダー処理結果をクリアする |
||
| 300 | $this->annotationInfoExtendList = []; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * プロパティ情報を読み込む |
||
| 305 | */ |
||
| 306 | private function readProperty() |
||
| 359 | } |
||
| 360 | } |
||
| 361 | } |
||
| 362 |
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