| Conditions | 17 |
| Paths | 387 |
| Total Lines | 80 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 95 | private function fetchReference(Reference $reference): void { |
||
| 96 | try { |
||
| 97 | $user = $this->userSession->getUser(); |
||
| 98 | if ($user) { |
||
| 99 | $this->limiter->registerUserRequest('opengraph', 10, 120, $user); |
||
| 100 | } else { |
||
| 101 | $this->limiter->registerAnonRequest('opengraph', 10, 120, $this->request->getRemoteAddress()); |
||
| 102 | } |
||
| 103 | } catch (RateLimitExceededException $e) { |
||
| 104 | return; |
||
| 105 | } |
||
| 106 | |||
| 107 | $client = $this->clientService->newClient(); |
||
| 108 | try { |
||
| 109 | $headResponse = $client->head($reference->getId(), [ 'timeout' => 10 ]); |
||
| 110 | } catch (\Exception $e) { |
||
| 111 | $this->logger->debug('Failed to perform HEAD request to get target metadata', ['exception' => $e]); |
||
| 112 | return; |
||
| 113 | } |
||
| 114 | $linkContentLength = $headResponse->getHeader('Content-Length'); |
||
| 115 | if (is_numeric($linkContentLength) && (int) $linkContentLength > 5 * 1024 * 1024) { |
||
| 116 | $this->logger->debug('Skip resolving links pointing to content length > 5 MB'); |
||
| 117 | return; |
||
| 118 | } |
||
| 119 | $linkContentType = $headResponse->getHeader('Content-Type'); |
||
| 120 | $expectedContentType = 'text/html'; |
||
| 121 | $suffixedExpectedContentType = $expectedContentType . ';'; |
||
| 122 | $startsWithSuffixed = substr($linkContentType, 0, strlen($suffixedExpectedContentType)) === $suffixedExpectedContentType; |
||
| 123 | // check the header begins with the expected content type |
||
| 124 | if ($linkContentType !== $expectedContentType && !$startsWithSuffixed) { |
||
| 125 | $this->logger->debug('Skip resolving links pointing to content type that is not "text/html"'); |
||
| 126 | return; |
||
| 127 | } |
||
| 128 | try { |
||
| 129 | $response = $client->get($reference->getId(), [ 'timeout' => 10 ]); |
||
| 130 | } catch (\Exception $e) { |
||
| 131 | $this->logger->debug('Failed to fetch link for obtaining open graph data', ['exception' => $e]); |
||
| 132 | return; |
||
| 133 | } |
||
| 134 | |||
| 135 | $responseBody = (string)$response->getBody(); |
||
| 136 | |||
| 137 | // OpenGraph handling |
||
| 138 | $consumer = new Consumer(); |
||
| 139 | $consumer->useFallbackMode = true; |
||
| 140 | $object = $consumer->loadHtml($responseBody); |
||
| 141 | |||
| 142 | $reference->setUrl($reference->getId()); |
||
| 143 | |||
| 144 | if ($object->title) { |
||
| 145 | $reference->setTitle($object->title); |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($object->description) { |
||
| 149 | $reference->setDescription($object->description); |
||
| 150 | } |
||
| 151 | |||
| 152 | if ($object->images) { |
||
|
|
|||
| 153 | try { |
||
| 154 | $appData = $this->appDataFactory->get('core'); |
||
| 155 | try { |
||
| 156 | $folder = $appData->getFolder('opengraph'); |
||
| 157 | } catch (NotFoundException $e) { |
||
| 158 | $folder = $appData->newFolder('opengraph'); |
||
| 159 | } |
||
| 160 | $response = $client->get($object->images[0]->url, [ 'timeout' => 10 ]); |
||
| 161 | $contentType = $response->getHeader('Content-Type'); |
||
| 162 | $contentLength = $response->getHeader('Content-Length'); |
||
| 163 | |||
| 164 | if (in_array($contentType, self::ALLOWED_CONTENT_TYPES, true) && $contentLength < self::MAX_PREVIEW_SIZE) { |
||
| 165 | $stream = Utils::streamFor($response->getBody()); |
||
| 166 | $bodyStream = new LimitStream($stream, self::MAX_PREVIEW_SIZE, 0); |
||
| 167 | $reference->setImageContentType($contentType); |
||
| 168 | $folder->newFile(md5($reference->getId()), $bodyStream->getContents()); |
||
| 169 | $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Reference.preview', ['referenceId' => md5($reference->getId())])); |
||
| 170 | } |
||
| 171 | } catch (GuzzleException $e) { |
||
| 172 | $this->logger->info('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]); |
||
| 173 | } catch (\Throwable $e) { |
||
| 174 | $this->logger->error('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]); |
||
| 175 | } |
||
| 187 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.