| Conditions | 9 |
| Paths | 8 |
| Total Lines | 66 |
| Code Lines | 45 |
| 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 |
||
| 121 | public function deleteTracks($fileIds, $userId = null){ |
||
| 122 | if(!is_array($fileIds)){ |
||
| 123 | $fileIds = [$fileIds]; |
||
| 124 | } |
||
| 125 | |||
| 126 | $tracks = ($userId !== null) |
||
| 127 | ? $this->mapper->findByFileIds($fileIds, $userId) |
||
| 128 | : $this->mapper->findAllByFileIds($fileIds); |
||
| 129 | |||
| 130 | if(count($tracks) === 0){ |
||
| 131 | $result = false; |
||
| 132 | } |
||
| 133 | else{ |
||
| 134 | // delete all the matching tracks |
||
| 135 | $trackIds = array_map(function($t) { return $t->getId(); }, $tracks); |
||
| 136 | $this->deleteById($trackIds); |
||
| 137 | |||
| 138 | // find all distinct albums, artists, and users of the deleted tracks |
||
| 139 | $artists = []; |
||
| 140 | $albums = []; |
||
| 141 | $users = []; |
||
| 142 | foreach($tracks as $track){ |
||
| 143 | $artists[$track->getArtistId()] = 1; |
||
| 144 | $albums[$track->getAlbumId()] = 1; |
||
| 145 | $users[$track->getUserId()] = 1; |
||
| 146 | } |
||
| 147 | $artists = array_keys($artists); |
||
| 148 | $albums = array_keys($albums); |
||
| 149 | $users = array_keys($users); |
||
| 150 | |||
| 151 | // categorize each artist as 'remaining' or 'obsolete' |
||
| 152 | $remainingArtists = []; |
||
| 153 | $obsoleteArtists = []; |
||
| 154 | foreach($artists as $artistId){ |
||
| 155 | $result = $this->mapper->countByArtist($artistId); |
||
| 156 | if($result === '0'){ |
||
| 157 | $obsoleteArtists[] = $artistId; |
||
| 158 | }else{ |
||
| 159 | $remainingArtists[] = $artistId; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | // categorize each album as 'remaining' or 'obsolete' |
||
| 164 | $remainingAlbums = []; |
||
| 165 | $obsoleteAlbums = []; |
||
| 166 | foreach($albums as $albumId){ |
||
| 167 | $result = $this->mapper->countByAlbum($albumId); |
||
| 168 | if($result === '0'){ |
||
| 169 | $obsoleteAlbums[] = $albumId; |
||
| 170 | }else{ |
||
| 171 | $remainingAlbums[] = $albumId; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | $result = [ |
||
| 176 | 'deletedTracks' => $trackIds, |
||
| 177 | 'remainingAlbums' => $remainingAlbums, |
||
| 178 | 'remainingArtists' => $remainingArtists, |
||
| 179 | 'obsoleteAlbums' => $obsoleteAlbums, |
||
| 180 | 'obsoleteArtists' => $obsoleteArtists, |
||
| 181 | 'affectedUsers' => $users |
||
| 182 | ]; |
||
| 183 | } |
||
| 184 | |||
| 185 | return $result; |
||
| 186 | } |
||
| 187 | |||
| 198 |