| Conditions | 5 |
| Paths | 9 |
| Total Lines | 51 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 33 |
| CRAP Score | 5 |
| 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 |
||
| 23 | 2 | public function update( |
|
| 24 | ArtistInterface $artist |
||
| 25 | ): void { |
||
| 26 | 2 | $images = []; |
|
| 27 | 2 | $assetPath = $this->config->getAssetPath(); |
|
| 28 | |||
| 29 | 2 | $albumDestination = sprintf( |
|
| 30 | 2 | '%s/img/album', |
|
| 31 | 2 | $assetPath |
|
| 32 | 2 | ); |
|
| 33 | |||
| 34 | 2 | foreach ($artist->getAlbums() as $album) { |
|
| 35 | 2 | $albumImage = sprintf('%s/%s.jpg', $albumDestination, $album->getMbid()); |
|
| 36 | |||
| 37 | 2 | if (file_exists($albumImage)) { |
|
| 38 | 1 | $images[] = $albumImage; |
|
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | 2 | if ($images === []) { |
|
| 43 | 1 | return; |
|
| 44 | } |
||
| 45 | |||
| 46 | // MakeCollage only support up to 4 images, to take the first ones |
||
| 47 | 1 | if (count($images) > 4) { |
|
| 48 | 1 | $images = array_slice($images, 0, 4); |
|
| 49 | } |
||
| 50 | |||
| 51 | /** @var Image $image */ |
||
| 52 | 1 | $image = $this->collageMaker |
|
| 53 | 1 | ->make(600, 600) |
|
| 54 | 1 | ->padding(10) |
|
| 55 | 1 | ->background('#000') |
|
| 56 | 1 | ->from($images); |
|
| 57 | |||
| 58 | 1 | $artistDestination = sprintf( |
|
| 59 | 1 | '%s/img/artist', |
|
| 60 | 1 | $assetPath |
|
| 61 | 1 | ); |
|
| 62 | |||
| 63 | 1 | $image->save( |
|
| 64 | 1 | sprintf( |
|
| 65 | 1 | '%s/%s.jpg', |
|
| 66 | 1 | $artistDestination, |
|
| 67 | 1 | $artist->getMbid() |
|
| 68 | 1 | ) |
|
| 69 | 1 | ); |
|
| 70 | |||
| 71 | 1 | $artist->setLastModified(new DateTime()); |
|
| 72 | |||
| 73 | 1 | $this->artistRepository->save($artist); |
|
| 74 | } |
||
| 76 |