| Conditions | 6 |
| Paths | 5 |
| Total Lines | 51 |
| Code Lines | 27 |
| 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 |
||
| 41 | public function getTemplateData() : array |
||
| 42 | { |
||
| 43 | $blogPosts = []; |
||
| 44 | $parameters = $this->getRoutingParameters(); |
||
| 45 | $date = $parameters['uri_parameter'] ?? null; |
||
| 46 | |||
| 47 | if (!$date) { |
||
| 48 | throw new RuntimeException('Forbidden', 403); |
||
| 49 | } |
||
| 50 | |||
| 51 | $dateParts = explode('-', $date); |
||
| 52 | |||
| 53 | if (!preg_match('/^\d{4}\-\d{2}$/', $date) || !checkdate((int) ($dateParts[1] ?? 13), 1, (int) $dateParts[0])) { |
||
| 54 | throw new RuntimeException('Bad Request', 400); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** @var Entity\ApplicationEntity $applicationEntity */ |
||
| 58 | $applicationEntity = $this->getApplicationStorage() |
||
| 59 | ->getApplicationByName($this->environmentManager->getSelectedApplication()); |
||
| 60 | |||
| 61 | /** @var Entity\Filesystem\FilesystemEntity[] $publications */ |
||
| 62 | $publications = $this->getFilesystemStorage() |
||
| 63 | ->getPublishedDocuments( |
||
| 64 | $applicationEntity->getApplicationId(), |
||
| 65 | [ |
||
| 66 | 'YEAR(date_published) = ?' => (int)$dateParts[0], |
||
| 67 | 'MONTH(date_published) = ?' => (int)$dateParts[1] |
||
| 68 | ] |
||
| 69 | ); |
||
| 70 | |||
| 71 | if (!$publications) { |
||
|
|
|||
| 72 | throw new RuntimeException('Not Found', 404); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** @var DateTime $titleDate */ |
||
| 76 | $titleDate = $publications[0]->getDatePublished(); |
||
| 77 | |||
| 78 | /** @var Entity\Filesystem\FilesystemEntity $filesystemEntity */ |
||
| 79 | foreach ($publications as $filesystemEntity) { |
||
| 80 | $blogPosts[] = $this->getBlobPostData($applicationEntity, $filesystemEntity); |
||
| 81 | } |
||
| 82 | |||
| 83 | return [ |
||
| 84 | 'page' => [ |
||
| 85 | 'title' => $titleDate->format('Y4B'), |
||
| 86 | 'type' => 'Archive', |
||
| 87 | ], |
||
| 88 | 'activeMenu' => $date, |
||
| 89 | 'blogPosts' => $blogPosts, |
||
| 90 | ]; |
||
| 91 | } |
||
| 92 | } |
||
| 93 |
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.