| Total Complexity | 49 |
| Total Lines | 321 |
| 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 $actionMethod; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * constructor |
||
| 81 | * @param IAnnotatable ターゲットインスタンス |
||
| 82 | * @param Container 依存コンテナ |
||
| 83 | */ |
||
| 84 | public function __construct(IAnnotatable $instance) |
||
| 85 | { |
||
| 86 | $this->initialize(); |
||
| 87 | $this->instance = $instance; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * 初期化処理 |
||
| 92 | */ |
||
| 93 | private function initialize() |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * アノテーション情報リストを返却する |
||
| 102 | * @param array<mixed> アノテーション情報リスト |
||
| 103 | */ |
||
| 104 | public function getAnnotationInfoList(): array |
||
| 105 | { |
||
| 106 | if (empty($this->extendReaderMap)) { |
||
| 107 | return $this->annotationInfoList; |
||
| 108 | } |
||
| 109 | |||
| 110 | foreach ($this->annotationInfoList as $key => $annotationInfo) { |
||
| 111 | if (!array_key_exists($key, $this->extendReaderMap)) { |
||
| 112 | continue; |
||
| 113 | } |
||
| 114 | $readerClasspath = $this->extendReaderMap[$key]; |
||
| 115 | $refClass = new \ReflectionClass($readerClasspath); |
||
| 116 | $reader = $refClass->newInstance(); |
||
| 117 | $reader->read($annotationInfo); |
||
| 118 | $this->annotationInfoList[$key] = $reader->getAnnotationInfo(); |
||
| 119 | } |
||
| 120 | |||
| 121 | return $this->annotationInfoList; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * 発生した例外を返却する |
||
| 126 | * @param ExceptionDelegator 発生した例外 |
||
| 127 | */ |
||
| 128 | public function getException() |
||
| 129 | { |
||
| 130 | return $this->exception; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * アクションメソッドを設定する |
||
| 135 | * @param string アクションメソッド |
||
| 136 | */ |
||
| 137 | public function setActionMethod(string $actionMethod) |
||
| 138 | { |
||
| 139 | $this->actionMethod = $actionMethod; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * 読み込み可能アノテーション情報を設定する |
||
| 144 | * @param string アノテーションクラスパス |
||
| 145 | * @param Container アノテーションクラス依存コンテナ |
||
| 146 | */ |
||
| 147 | public function readable(string $classpath, Container $container = null) |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * 拡張アノテーションリーダーを設定する |
||
| 154 | * @param string アノテーションクラスパス |
||
| 155 | * @param string 拡張アノテーションリーダークラスパス |
||
| 156 | */ |
||
| 157 | public function useExtendReader(string $annotationClasspath, string $readerClasspath) |
||
| 158 | { |
||
| 159 | $this->extendReaderMap[$annotationClasspath] = $readerClasspath; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * アノテーション情報を読み込む |
||
| 164 | */ |
||
| 165 | public function read() |
||
| 166 | { |
||
| 167 | try { |
||
| 168 | $this->readClass(); |
||
| 169 | $this->readMethod(); |
||
| 170 | $this->readProperty(); |
||
| 171 | } catch (DoctrineAnnotationException $e) { |
||
| 172 | $this->initialize(); |
||
| 173 | throw new AnnotationException($e); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * クラス情報を読み込む |
||
| 179 | */ |
||
| 180 | public function readClass() |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * メソッド情報を読み込む |
||
| 228 | */ |
||
| 229 | public function readMethod() |
||
| 230 | { |
||
| 231 | $reader = new DoctrineAnnotationReader(); |
||
| 232 | $refClass = new \ReflectionClass($this->instance); |
||
| 233 | |||
| 234 | while ($refClass !== false) { |
||
| 235 | foreach ($refClass->getMethods() as $refMethod) { |
||
| 236 | if ($refClass->getName() !== $refMethod->class) { |
||
| 237 | continue; |
||
| 238 | } |
||
| 239 | |||
| 240 | $annotations = $reader->getMethodAnnotations($refMethod); |
||
| 241 | if (empty($annotations)) { |
||
| 242 | continue; |
||
| 243 | } |
||
| 244 | |||
| 245 | for ($i = 0, $count = count($annotations); $i < $count; $i++) { |
||
| 246 | $annotation = $annotations[$i]; |
||
| 247 | |||
| 248 | if (!$annotation instanceof IMethod && !$annotation instanceof IMethods) { |
||
| 249 | continue; |
||
| 250 | } |
||
| 251 | |||
| 252 | // IMethodを実装している場合、アクションメソッドのアノテーション以外は読み込まない |
||
| 253 | // PHPのメソッドは大文字小文字を区別しないため、そのまま比較するとルーティング解決結果と実際のメソッド名が合わないケースがある |
||
| 254 | // PHPの仕様に合わせてメソッド名の文字列比較は小文字に変換してから行う |
||
| 255 | if ($annotation instanceof IMethod && strtolower($this->actionMethod) !== strtolower($refMethod->name)) { |
||
| 256 | continue; |
||
| 257 | } |
||
| 258 | |||
| 259 | // 読み込み可能なアノテーション以外は読み込まない |
||
| 260 | $key = get_class($annotation); |
||
| 261 | if (!array_key_exists($key, $this->readableMap)) { |
||
| 262 | continue; |
||
| 263 | } |
||
| 264 | |||
| 265 | $container = $this->readableMap[$key]; |
||
| 266 | |||
| 267 | try { |
||
| 268 | $annotation->onMethodInject($this->instance, $refMethod, $container); |
||
| 269 | } catch (\Exception $e) { |
||
| 270 | if ($this->exception === null) { |
||
| 271 | $this->exception = new ExceptionDelegator($this->instance, $e, $this->actionMethod); |
||
| 272 | } |
||
| 273 | continue; |
||
| 274 | } |
||
| 275 | |||
| 276 | // IReadを実装している場合、任意のデータを返却する |
||
| 277 | if ($annotation instanceof IRead) { |
||
| 278 | if (!array_key_exists($key, $this->annotationInfoList)) { |
||
| 279 | $this->annotationInfoList[$key] = []; |
||
| 280 | } |
||
| 281 | $this->annotationInfoList[$key][] = $annotation->getAnnotationInfo(); |
||
| 282 | } |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | $refClass = $refClass->getParentClass(); |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * プロパティ情報を読み込む |
||
| 292 | */ |
||
| 293 | private function readProperty() |
||
| 346 | } |
||
| 347 | } |
||
| 348 | } |
||
| 349 |
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