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