| Conditions | 7 | 
| Paths | 137 | 
| Total Lines | 51 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 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  | 
            ||
| 71 | public function execute(AMQPMessage $message): int  | 
            ||
| 72 |     { | 
            ||
| 73 | sleep(1); // wait for data to be flushed (really) in database  | 
            ||
| 74 | |||
| 75 |         try { | 
            ||
| 76 | ['image' => $image, 'tenantId' => $tenantId] = unserialize($message->body, [false]);  | 
            ||
| 77 |             if (($tenant = $this->entityManager->find(Tenant::class, $tenantId)) instanceof TenantInterface) { | 
            ||
| 78 | $this->tenantContext->setTenant($tenant);  | 
            ||
| 79 | }  | 
            ||
| 80 | |||
| 81 |             if (null === $image) { | 
            ||
| 82 |                 throw new InvalidArgumentException('Missing image data'); | 
            ||
| 83 | }  | 
            ||
| 84 |         } catch (RuntimeException $e) { | 
            ||
| 85 |             $this->logger->error('Message REJECTED: '.$e->getMessage(), ['exception' => $e->getTraceAsString()]); | 
            ||
| 86 | |||
| 87 | return ConsumerInterface::MSG_REJECT;  | 
            ||
| 88 | }  | 
            ||
| 89 | |||
| 90 | /** @var ImageInterface $image */  | 
            ||
| 91 | $image = $this->entityManager->merge($image);  | 
            ||
| 92 | $mediaId = $image->getAssetId();  | 
            ||
| 93 | $tempLocation = rtrim(sys_get_temp_dir(), '/').DIRECTORY_SEPARATOR.sha1($mediaId);  | 
            ||
| 94 | |||
| 95 |         try { | 
            ||
| 96 |             if (!function_exists('imagewebp')) { | 
            ||
| 97 |                 throw new BadFunctionCallException('"imagewebp" function is missing. Looks like GD was compiled without webp support'); | 
            ||
| 98 | }  | 
            ||
| 99 | imagewebp($this->getImageAsResource($image), $tempLocation);  | 
            ||
| 100 | $uploadedFile = new UploadedFile($tempLocation, $mediaId, 'image/webp', strlen($tempLocation), null, true);  | 
            ||
| 101 | $this->mediaManager->saveFile($uploadedFile, $mediaId);  | 
            ||
| 102 | |||
| 103 |             $this->logger->info(sprintf('File "%s" converted successfully to WEBP', $mediaId)); | 
            ||
| 104 | |||
| 105 | $image->addVariant(ImageInterface::VARIANT_WEBP);  | 
            ||
| 106 | $this->markArticlesMediaAsUpdated($image);  | 
            ||
| 107 | |||
| 108 | $this->entityManager->flush();  | 
            ||
| 109 |         } catch (Exception $e) { | 
            ||
| 110 |             $this->logger->error('File NOT converted '.$e->getMessage(), ['exception' => $e->getTraceAsString()]); | 
            ||
| 111 | |||
| 112 | return ConsumerInterface::MSG_REJECT;  | 
            ||
| 113 |         } finally { | 
            ||
| 114 | $filesystem = new Filesystem();  | 
            ||
| 115 |             if ($filesystem->exists($tempLocation)) { | 
            ||
| 116 | $filesystem->remove($tempLocation);  | 
            ||
| 117 | }  | 
            ||
| 118 | }  | 
            ||
| 119 | |||
| 120 | return ConsumerInterface::MSG_ACK;  | 
            ||
| 121 | }  | 
            ||
| 122 | |||
| 156 | 
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.