| Conditions | 7 | 
| Paths | 11 | 
| Total Lines | 55 | 
| Code Lines | 30 | 
| 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 | ||
| 53 | public function write(ReportInterface $report): string | ||
| 54 |     { | ||
| 55 | $key = $this->reportPathResolver->resolve($report); | ||
| 56 | // todo should we provide some kind of 'overwrite' parameter? | ||
| 57 |         if ($this->filesystem->has($key)) { | ||
| 58 | return $key; | ||
| 59 | } | ||
| 60 | |||
| 61 | $reportConfiguration = $report->getReportConfiguration(); | ||
| 62 |         if (null === $reportConfiguration) { | ||
| 63 |             throw new RuntimeException(sprintf('No report configuration associated with report %s', $report->getId())); | ||
| 64 | } | ||
| 65 | |||
| 66 | $template = $this->twig->load($reportConfiguration->getTemplate()); | ||
| 67 | |||
| 68 | $definedBlocks = $template->getBlockNames(); | ||
| 69 |         foreach (['extension', 'body'] as $block) { | ||
| 70 |             if (!in_array($block, $definedBlocks, true)) { | ||
| 71 | throw new InvalidArgumentException(sprintf( | ||
| 72 | 'The block "%s" is not present in the defined blocks ["%s"] of your template %s', | ||
| 73 | $block, | ||
| 74 |                     implode('", "', $definedBlocks), | ||
| 75 | $reportConfiguration->getTemplate() | ||
| 76 | )); // todo better exception | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | $path = $this->generateTempPath(); | ||
| 81 | |||
| 82 | $fp = fopen($path, 'w+b'); // needs to be w+ since we use the same stream later to read from | ||
| 83 | |||
| 84 |         ob_start(static function ($buffer) use ($fp) { | ||
| 85 | fwrite($fp, $buffer); | ||
| 86 | }, 1024); | ||
| 87 | |||
| 88 |         $template->displayBlock('body', [ | ||
| 89 | 'stockMovements' => $report->getStockMovements(), | ||
| 90 | ]); | ||
| 91 | |||
| 92 | ob_end_clean(); | ||
| 93 | |||
| 94 | $res = $this->filesystem->writeStream($key, $fp); | ||
| 95 | |||
| 96 |         try { | ||
| 97 | // tries to close the file pointer although it may already have been closed by flysystem | ||
| 98 | fclose($fp); | ||
| 99 |         } catch (FilesystemException $e) { | ||
|  | |||
| 100 | |||
| 101 | } | ||
| 102 | |||
| 103 |         if(false === $res) { | ||
| 104 |             throw new RuntimeException(sprintf('An error occurred when trying to write the report %s', $key)); | ||
| 105 | } | ||
| 106 | |||
| 107 | return $key; | ||
| 108 | } | ||
| 119 |