| Total Complexity | 50 |
| Total Lines | 325 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Linker 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 Linker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | final class Linker implements LinkerInterface |
||
| 38 | { |
||
| 39 | /** |
||
| 40 | * memory cache for linker |
||
| 41 | * |
||
| 42 | * @var Query |
||
|
|
|||
| 43 | */ |
||
| 44 | private array $cache = []; |
||
| 45 | |||
| 46 | /** @var array<class-string<BatchResolverInterface>, BatchResolverInterface> */ |
||
| 47 | private array $batchResolverCache = []; |
||
| 48 | |||
| 49 | public function __construct( |
||
| 50 | private readonly InvokerInterface $invoker, |
||
| 51 | private readonly FactoryInterface $factory, |
||
| 52 | private readonly BatchResolverFactoryInterface|null $batchResolverFactory = null, |
||
| 53 | ) { |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritDoc} |
||
| 58 | */ |
||
| 59 | #[Override] |
||
| 60 | public function invoke(AbstractRequest $request) |
||
| 61 | { |
||
| 62 | $this->cache = []; |
||
| 63 | |||
| 64 | return $this->invokeRecursive($request); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @throws LinkQueryException |
||
| 69 | * @throws LinkRelException |
||
| 70 | */ |
||
| 71 | private function invokeRecursive(AbstractRequest $request): ResourceObject |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param Body $nextResource |
||
| 90 | * |
||
| 91 | * @return ResourceObject |
||
| 92 | */ |
||
| 93 | private function nextLink(LinkType $link, ResourceObject $ro, array $nextResource): ResourceObject |
||
| 94 | { |
||
| 95 | /** @psalm-suppress MixedAssignment */ |
||
| 96 | $nextBody = $nextResource; |
||
| 97 | |||
| 98 | if ($link->type === LinkType::SELF_LINK) { |
||
| 99 | $ro->body = $nextBody; |
||
| 100 | |||
| 101 | return $ro; |
||
| 102 | } |
||
| 103 | |||
| 104 | if ($link->type === LinkType::NEW_LINK) { |
||
| 105 | assert(is_array($ro->body) || $ro->body === null); |
||
| 106 | $ro->body[$link->key] = $nextBody; |
||
| 107 | |||
| 108 | return $ro; |
||
| 109 | } |
||
| 110 | |||
| 111 | // crawl |
||
| 112 | return $ro; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Annotation link |
||
| 117 | * |
||
| 118 | * @throws MethodException |
||
| 119 | * @throws LinkRelException |
||
| 120 | * @throws Exception\LinkQueryException |
||
| 121 | */ |
||
| 122 | private function annotationLink(LinkType $link, ResourceObject $current, AbstractRequest $request): ResourceObject |
||
| 123 | { |
||
| 124 | if (! is_array($current->body)) { |
||
| 125 | throw new Exception\LinkQueryException('Only array is allowed for link in ' . $current::class, 500); |
||
| 126 | } |
||
| 127 | |||
| 128 | $classMethod = 'on' . ucfirst($request->method); |
||
| 129 | /** @var list<Link> $annotations */ |
||
| 130 | $annotations = (new ReflectionMethod($current::class, $classMethod))->getAnnotations(); |
||
| 131 | if ($link->type === LinkType::CRAWL_LINK) { |
||
| 132 | return $this->annotationCrawl($annotations, $link, $current); |
||
| 133 | } |
||
| 134 | |||
| 135 | /* @noinspection ExceptionsAnnotatingAndHandlingInspection */ |
||
| 136 | return $this->annotationRel($annotations, $link, $current); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Annotation link (new, self) |
||
| 141 | * |
||
| 142 | * @param Link[] $annotations |
||
| 143 | * |
||
| 144 | * @throws UriException |
||
| 145 | * @throws MethodException |
||
| 146 | * @throws Exception\LinkQueryException |
||
| 147 | * @throws Exception\LinkRelException |
||
| 148 | */ |
||
| 149 | private function annotationRel(array $annotations, LinkType $link, ResourceObject $current): ResourceObject |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Link annotation crawl |
||
| 171 | * |
||
| 172 | * @param ObjectList $annotations |
||
| 173 | * |
||
| 174 | * @throws MethodException |
||
| 175 | */ |
||
| 176 | private function annotationCrawl(array $annotations, LinkType $link, ResourceObject $current): ResourceObject |
||
| 177 | { |
||
| 178 | $isList = $this->isList($current->body); |
||
| 179 | /** @var QueryList $bodyList */ |
||
| 180 | $bodyList = $isList ? (array) $current->body : [$current->body]; |
||
| 181 | |||
| 182 | // Process batch links first |
||
| 183 | $this->processBatchLinks($annotations, $link, $bodyList); |
||
| 184 | |||
| 185 | // Process non-batch links |
||
| 186 | foreach ($bodyList as &$body) { |
||
| 187 | $this->crawl($annotations, $link, $body); |
||
| 188 | } |
||
| 189 | |||
| 190 | unset($body); |
||
| 191 | /** @psalm-suppress PossiblyUndefinedArrayOffset */ |
||
| 192 | $current->body = $isList ? $bodyList : $bodyList[0]; |
||
| 193 | |||
| 194 | return $current; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Process batch-enabled links |
||
| 199 | * |
||
| 200 | * @param ObjectList $annotations |
||
| 201 | * @param QueryList $bodyList |
||
| 202 | * |
||
| 203 | * @param-out QueryList $bodyList |
||
| 204 | */ |
||
| 205 | private function processBatchLinks(array $annotations, LinkType $link, array &$bodyList): void |
||
| 206 | { |
||
| 207 | if ($this->batchResolverFactory === null) { |
||
| 208 | return; |
||
| 209 | } |
||
| 210 | |||
| 211 | // Group URIs by batch resolver class |
||
| 212 | /** @var array<class-string<BatchResolverInterface>, array{annotation: Link, uris: array<int, string>}> $batchGroups */ |
||
| 213 | $batchGroups = []; |
||
| 214 | |||
| 215 | foreach ($annotations as $annotation) { |
||
| 216 | if (! $annotation instanceof Link || $annotation->crawl !== $link->key) { |
||
| 217 | continue; |
||
| 218 | } |
||
| 219 | |||
| 220 | if ($annotation->batch === null) { |
||
| 221 | continue; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** @var class-string<BatchResolverInterface> $batchClass */ |
||
| 225 | $batchClass = $annotation->batch; |
||
| 226 | $batchGroups[$batchClass] = ['annotation' => $annotation, 'uris' => []]; |
||
| 227 | |||
| 228 | foreach ($bodyList as $index => $body) { |
||
| 229 | $uri = uri_template($annotation->href, $body); |
||
| 230 | $batchGroups[$batchClass]['uris'][$index] = $uri; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | // Execute batch resolvers and distribute results |
||
| 235 | foreach ($batchGroups as $batchClass => $group) { |
||
| 236 | $resolver = $this->getBatchResolver($batchClass); |
||
| 237 | $requests = new Requests(array_values($group['uris'])); |
||
| 238 | $results = $resolver($requests); |
||
| 239 | |||
| 240 | foreach ($group['uris'] as $index => $uri) { |
||
| 241 | $bodyList[$index][$group['annotation']->rel] = $results->get($uri); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get or create a batch resolver instance |
||
| 248 | * |
||
| 249 | * @param class-string<BatchResolverInterface> $class |
||
| 250 | */ |
||
| 251 | private function getBatchResolver(string $class): BatchResolverInterface |
||
| 252 | { |
||
| 253 | if (! isset($this->batchResolverCache[$class])) { |
||
| 254 | assert($this->batchResolverFactory !== null); |
||
| 255 | $this->batchResolverCache[$class] = $this->batchResolverFactory->create($class); |
||
| 256 | } |
||
| 257 | |||
| 258 | return $this->batchResolverCache[$class]; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param ObjectList $annotations |
||
| 263 | * @param Body $body |
||
| 264 | * |
||
| 265 | * @throws LinkQueryException |
||
| 266 | * @throws MethodException |
||
| 267 | * @throws LinkRelException |
||
| 268 | * @throws UriException |
||
| 269 | * |
||
| 270 | * @param-out Body $body |
||
| 271 | */ |
||
| 272 | private function crawl(array $annotations, LinkType $link, array &$body): void |
||
| 273 | { |
||
| 274 | foreach ($annotations as $annotation) { |
||
| 275 | if (! $annotation instanceof Link || $annotation->crawl !== $link->key) { |
||
| 276 | continue; |
||
| 277 | } |
||
| 278 | |||
| 279 | // Skip batch-enabled links (already processed by processBatchLinks) |
||
| 280 | if ($annotation->batch !== null && $this->batchResolverFactory !== null) { |
||
| 281 | continue; |
||
| 282 | } |
||
| 283 | |||
| 284 | $uri = uri_template($annotation->href, $body); |
||
| 285 | $rel = $this->factory->newInstance($uri); |
||
| 286 | /* @noinspection UnnecessaryParenthesesInspection */ |
||
| 287 | $query = (new Uri($uri))->query; |
||
| 288 | $request = new Request($this->invoker, $rel, Request::GET, $query, [$link], $this); |
||
| 289 | $hash = $request->hash(); |
||
| 290 | if (array_key_exists($hash, $this->cache)) { |
||
| 291 | /** @var Body $cachedResponse */ |
||
| 292 | $cachedResponse = $this->cache[$hash]; |
||
| 293 | $body[$annotation->rel] = $cachedResponse; |
||
| 294 | continue; |
||
| 295 | } |
||
| 296 | |||
| 297 | $this->cache[$hash] = $body[$annotation->rel] = $this->getResponseBody($request); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | /** @return Body|null */ |
||
| 302 | private function getResponseBody(Request $request): array|null |
||
| 308 | } |
||
| 309 | |||
| 310 | private function isList(mixed $value): bool |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param list<array-key> $keys |
||
| 328 | * @param BodyOrStringList $list |
||
| 329 | */ |
||
| 330 | private function isMultiColumnMultiRowList(array $keys, array $list): bool |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param Body $value |
||
| 347 | * @psalm-param Query|scalar $firstRow |
||
| 348 | */ |
||
| 349 | private function isMultiColumnList(array $value, mixed $firstRow): bool |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param Body $value |
||
| 356 | * @param list<array-key> $keys |
||
| 357 | * @param Body $list |
||
| 358 | */ |
||
| 359 | private function isSingleColumnList(array $value, array $keys, array $list): bool |
||
| 362 | } |
||
| 363 | } |
||
| 364 |
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