| Conditions | 7 |
| Paths | 4 |
| Total Lines | 84 |
| Code Lines | 47 |
| 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 |
||
| 117 | protected function getBooksDependencies(SimpleXMLElement $data, SimpleXMLElement $book): array |
||
| 118 | { |
||
| 119 | // Book attributes |
||
| 120 | $bookAttributes = $this->getSimpleXmlElementAttributesAsArray($book); |
||
| 121 | |||
| 122 | // Series attributes |
||
| 123 | $series = $book->{'series'}[0]; |
||
| 124 | $seriesAttributes = $this->getSimpleXmlElementAttributesAsArray($series); |
||
| 125 | |||
| 126 | // Publish attributes |
||
| 127 | $publish = $book->{'publish'}[0]; |
||
| 128 | $publishAttributes = $this->getSimpleXmlElementAttributesAsArray($publish); |
||
| 129 | |||
| 130 | // Editor attributes |
||
| 131 | $releaseEditor = $book->{'editor'}[0]; |
||
| 132 | $releaseEditorAttributes = $this->getSimpleXmlElementAttributesAsArray($releaseEditor); |
||
| 133 | |||
| 134 | // Categories data |
||
| 135 | $categories = $data->xpath('//information/types/type[@code="'.$bookAttributes['type'].'"]'); |
||
| 136 | Assert::isIterable($categories); |
||
| 137 | $category = reset($categories); |
||
| 138 | Assert::isInstanceOf($category, SimpleXMLElement::class); |
||
| 139 | $categoryAttributes = $this->getSimpleXmlElementAttributesAsArray($category); |
||
| 140 | |||
| 141 | // Releases data |
||
| 142 | $releases = []; |
||
| 143 | foreach (array_keys($releaseEditorAttributes) as $release) { |
||
| 144 | $releaseNode = $book->{$release}; |
||
| 145 | |||
| 146 | // Categories data |
||
| 147 | $editors = $data->xpath('//information/editors/editor[@code="'.$releaseEditorAttributes[$release].'"]'); |
||
| 148 | Assert::isIterable($editors); |
||
| 149 | $editor = reset($editors); |
||
| 150 | Assert::isInstanceOf($editor, SimpleXMLElement::class); |
||
| 151 | $editorAttributes = $this->getSimpleXmlElementAttributesAsArray($editor); |
||
| 152 | |||
| 153 | $releases[] = new $this->classes['release']( |
||
| 154 | [ |
||
| 155 | 'title' => (string) $book->{'title'}, |
||
| 156 | 'editor' => new $this->classes['editor']( |
||
| 157 | $this->renameArrayKeys($editorAttributes, ['code' => 'id', 'lang' => 'preferredLanguage']) |
||
| 158 | ), |
||
| 159 | 'publicationDate' => isset($publishAttributes[$release]) |
||
| 160 | ? $this->transformToDateTime($publishAttributes[$release]) |
||
| 161 | : null, |
||
| 162 | 'language' => new $this->classes['language']( |
||
| 163 | [ |
||
| 164 | 'id' => $editorAttributes['lang'], |
||
| 165 | 'name' => $editorAttributes['lang'], |
||
| 166 | ] |
||
| 167 | ), |
||
| 168 | 'owner' => new $this->classes['owner']( |
||
| 169 | [ |
||
| 170 | 'nbCopies' => isset($releaseNode['copies']) ? (int) $releaseNode['copies'] : null, |
||
| 171 | 'nbReadings' => isset($releaseNode['readings']) ? (int) $releaseNode['readings'] : null, |
||
| 172 | ] |
||
| 173 | ), |
||
| 174 | 'format' => new $this->classes['format'](), |
||
| 175 | 'series' => new $this->classes['series']( |
||
| 176 | null !== $series |
||
| 177 | ? [ |
||
| 178 | 'title' => (string) $series, |
||
| 179 | 'bookId' => $seriesAttributes['number'] ?? null, |
||
| 180 | ] |
||
| 181 | : [] |
||
| 182 | ), |
||
| 183 | ] |
||
| 184 | ); |
||
| 185 | } |
||
| 186 | |||
| 187 | $authors = []; |
||
| 188 | foreach (explode(',', (string) $book->{'author'}) as $author) { |
||
| 189 | $authors[] = new $this->classes['author'](['name' => trim($author)]); |
||
| 190 | } |
||
| 191 | |||
| 192 | return [ |
||
| 193 | 'category' => new $this->classes['category']( |
||
| 194 | [ |
||
| 195 | 'id' => $bookAttributes['type'], |
||
| 196 | 'name' => $categoryAttributes['name'], |
||
| 197 | ] |
||
| 198 | ), |
||
| 199 | 'authors' => $authors, |
||
| 200 | 'releases' => $releases, |
||
| 201 | ]; |
||
| 212 |