| Conditions | 11 |
| Paths | 88 |
| Total Lines | 68 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 27 | protected function buildArray(LibraryInterface $library): array |
||
| 28 | { |
||
| 29 | $array = [ |
||
| 30 | 'languages' => [], |
||
| 31 | 'formats' => [ |
||
| 32 | [ |
||
| 33 | 'id' => '&f_unknown/unknown', |
||
| 34 | 'name' => 'Unknown', |
||
| 35 | ], |
||
| 36 | ], |
||
| 37 | 'categories' => [], |
||
| 38 | 'editors' => [], |
||
| 39 | 'authors' => [], |
||
| 40 | 'books' => [], |
||
| 41 | ]; |
||
| 42 | |||
| 43 | foreach ($library->getCategories() as $category) { |
||
| 44 | $array['categories'][] = [ |
||
| 45 | 'id' => '&c_'.$category->getId().'/'.$category->getId(), |
||
| 46 | 'name' => $category->getName(), |
||
| 47 | ]; |
||
| 48 | } |
||
| 49 | foreach ($library->getEditors() as $editor) { |
||
| 50 | $array['editors'][] = [ |
||
| 51 | 'id' => '&e_'.$editor->getId().'/'.$editor->getId(), |
||
| 52 | 'name' => $editor->getName(), |
||
| 53 | 'preferredLanguage' => $editor->getPreferredLanguage(), |
||
| 54 | ]; |
||
| 55 | } |
||
| 56 | |||
| 57 | foreach ($library->getBooks() as $book) { |
||
| 58 | $array['books'][] = $this->buildBookArray($book); |
||
| 59 | |||
| 60 | foreach ($book->getAuthors() as $author) { |
||
| 61 | $skip = false; |
||
| 62 | |||
| 63 | foreach ($array['authors'] as $authorArray) { |
||
| 64 | if (isset($authorArray['name']) && $authorArray['name'] === $author->getName()) { |
||
| 65 | $skip = true; |
||
| 66 | |||
| 67 | break; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($skip) { |
||
| 72 | continue; |
||
| 73 | } |
||
| 74 | |||
| 75 | $array['authors'][] = [ |
||
| 76 | 'id' => '&a_'.$author->getId().'/'.$author->getId(), |
||
| 77 | 'name' => $author->getName(), |
||
| 78 | ]; |
||
| 79 | } |
||
| 80 | |||
| 81 | foreach ($book->getReleases() as $release) { |
||
| 82 | if (isset($array['languages'][$release->getLanguage()->getId()])) { |
||
| 83 | continue; |
||
| 84 | } |
||
| 85 | $array['languages'][$release->getLanguage()->getId()] = [ |
||
| 86 | 'id' => '&l_'.$release->getLanguage()->getId().'/'.$release->getLanguage()->getId(), |
||
| 87 | 'name' => $release->getLanguage()->getName(), |
||
| 88 | ]; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | $array['languages'] = array_values($array['languages']); |
||
| 93 | |||
| 94 | return $array; |
||
| 95 | } |
||
| 141 |