| Conditions | 11 |
| Paths | 213 |
| Total Lines | 58 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | 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 |
||
| 93 | private function fetchReference(Reference $reference): void { |
||
| 94 | try { |
||
| 95 | $user = $this->userSession->getUser(); |
||
| 96 | if ($user) { |
||
| 97 | $this->limiter->registerUserRequest('opengraph', 10, 120, $user); |
||
| 98 | } else { |
||
| 99 | $this->limiter->registerAnonRequest('opengraph', 10, 120, $this->request->getRemoteAddress()); |
||
| 100 | } |
||
| 101 | } catch (RateLimitExceededException $e) { |
||
| 102 | return; |
||
| 103 | } |
||
| 104 | |||
| 105 | $client = $this->clientService->newClient(); |
||
| 106 | try { |
||
| 107 | $response = $client->get($reference->getId(), [ 'timeout' => 10 ]); |
||
| 108 | } catch (\Exception $e) { |
||
| 109 | $this->logger->debug('Failed to fetch link for obtaining open graph data', ['exception' => $e]); |
||
| 110 | return; |
||
| 111 | } |
||
| 112 | |||
| 113 | $responseBody = (string)$response->getBody(); |
||
| 114 | |||
| 115 | // OpenGraph handling |
||
| 116 | $consumer = new Consumer(); |
||
| 117 | $consumer->useFallbackMode = true; |
||
| 118 | $object = $consumer->loadHtml($responseBody); |
||
| 119 | |||
| 120 | $reference->setUrl($reference->getId()); |
||
| 121 | |||
| 122 | if ($object->title) { |
||
| 123 | $reference->setTitle($object->title); |
||
| 124 | } |
||
| 125 | |||
| 126 | if ($object->description) { |
||
| 127 | $reference->setDescription($object->description); |
||
| 128 | } |
||
| 129 | |||
| 130 | if ($object->images) { |
||
|
|
|||
| 131 | try { |
||
| 132 | $appData = $this->appDataFactory->get('core'); |
||
| 133 | try { |
||
| 134 | $folder = $appData->getFolder('opengraph'); |
||
| 135 | } catch (NotFoundException $e) { |
||
| 136 | $folder = $appData->newFolder('opengraph'); |
||
| 137 | } |
||
| 138 | $response = $client->get($object->images[0]->url, [ 'timeout' => 10 ]); |
||
| 139 | $contentType = $response->getHeader('Content-Type'); |
||
| 140 | $contentLength = $response->getHeader('Content-Length'); |
||
| 141 | |||
| 142 | if (in_array($contentType, self::ALLOWED_CONTENT_TYPES, true) && $contentLength < self::MAX_PREVIEW_SIZE) { |
||
| 143 | $stream = Utils::streamFor($response->getBody()); |
||
| 144 | $bodyStream = new LimitStream($stream, self::MAX_PREVIEW_SIZE, 0); |
||
| 145 | $reference->setImageContentType($contentType); |
||
| 146 | $folder->newFile(md5($reference->getId()), $bodyStream->getContents()); |
||
| 147 | $reference->setImageUrl($this->urlGenerator->linkToRouteAbsolute('core.Reference.preview', ['referenceId' => md5($reference->getId())])); |
||
| 148 | } |
||
| 149 | } catch (\Throwable $e) { |
||
| 150 | $this->logger->error('Failed to fetch and store the open graph image for ' . $reference->getId(), ['exception' => $e]); |
||
| 151 | } |
||
| 163 |
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.