Conditions | 9 |
Paths | 128 |
Total Lines | 67 |
Code Lines | 46 |
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 declare(strict_types=1); |
||
34 | public function transform($item) |
||
35 | { |
||
36 | /* ?><pre><?= json_encode($item, \JSON_PRETTY_PRINT) ?></pre><?php */ |
||
37 | $included = $item['included']; |
||
38 | $animeId = $item['relationships']['media']['data']['id']; |
||
39 | $anime = $included['anime'][$animeId]; |
||
40 | |||
41 | $genres = array_column($anime['relationships']['genres'], 'name') ?? []; |
||
42 | sort($genres); |
||
43 | |||
44 | $rating = (int) 2 * $item['attributes']['rating']; |
||
45 | |||
46 | $total_episodes = array_key_exists('episodeCount', $anime) && (int) $anime['episodeCount'] !== 0 |
||
47 | ? (int) $anime['episodeCount'] |
||
48 | : '-'; |
||
49 | |||
50 | $MALid = NULL; |
||
51 | |||
52 | if (array_key_exists('mappings', $anime['relationships'])) |
||
53 | { |
||
54 | foreach ($anime['relationships']['mappings'] as $mapping) |
||
55 | { |
||
56 | if ($mapping['externalSite'] === 'myanimelist/anime') |
||
57 | { |
||
58 | $MALid = $mapping['externalId']; |
||
59 | break; |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | |||
64 | $streamingLinks = (array_key_exists('streamingLinks', $anime['relationships'])) |
||
65 | ? Kitsu::parseListItemStreamingLinks($included, $animeId) |
||
66 | : []; |
||
67 | |||
68 | return [ |
||
69 | 'id' => $item['id'], |
||
70 | 'mal_id' => $MALid, |
||
71 | 'episodes' => [ |
||
72 | 'watched' => (int) $item['attributes']['progress'] !== '0' |
||
|
|||
73 | ? (int) $item['attributes']['progress'] |
||
74 | : '-', |
||
75 | 'total' => $total_episodes, |
||
76 | 'length' => $anime['episodeLength'], |
||
77 | ], |
||
78 | 'airing' => [ |
||
79 | 'status' => Kitsu::getAiringStatus($anime['startDate'], $anime['endDate']), |
||
80 | 'started' => $anime['startDate'], |
||
81 | 'ended' => $anime['endDate'] |
||
82 | ], |
||
83 | 'anime' => [ |
||
84 | 'age_rating' => $anime['ageRating'], |
||
85 | 'title' => $anime['canonicalTitle'], |
||
86 | 'titles' => Kitsu::filterTitles($anime), |
||
87 | 'slug' => $anime['slug'], |
||
88 | 'type' => $this->string($anime['showType'])->upperCaseFirst()->__toString(), |
||
89 | 'image' => $anime['posterImage']['small'], |
||
90 | 'genres' => $genres, |
||
91 | 'streaming_links' => $streamingLinks, |
||
92 | ], |
||
93 | 'watching_status' => $item['attributes']['status'], |
||
94 | 'notes' => $item['attributes']['notes'], |
||
95 | 'rewatching' => (bool) $item['attributes']['reconsuming'], |
||
96 | 'rewatched' => (int) $item['attributes']['reconsumeCount'], |
||
97 | 'user_rating' => ($rating === 0) ? '-' : (int) $rating, |
||
98 | 'private' => (bool) $item['attributes']['private'] ?? false, |
||
99 | ]; |
||
100 | } |
||
101 | |||
135 | // End of AnimeListTransformer.php |