| Conditions | 10 |
| Paths | 10 |
| Total Lines | 32 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 27 |
| CRAP Score | 10 |
| 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 |
||
| 12 | 1 | public function map(Product $product, array $data): Product |
|
| 13 | { |
||
| 14 | 1 | foreach ($data as $key => $value) { |
|
| 15 | switch ($key) { |
||
| 16 | 1 | case 'id': |
|
| 17 | 1 | $product->setId((int)$value); |
|
| 18 | 1 | break; |
|
| 19 | 1 | case 'images': |
|
| 20 | 1 | $product->setImages($value); |
|
| 21 | 1 | break; |
|
| 22 | 1 | case 'variants': |
|
| 23 | 1 | $product->setVariants((new VariantsMapper())->map(new Variants(), $value)); |
|
| 24 | 1 | break; |
|
| 25 | 1 | case 'name': |
|
| 26 | 1 | $product->setName($value); |
|
| 27 | 1 | break; |
|
| 28 | 1 | case 'description': |
|
| 29 | 1 | $product->setDescription($value); |
|
| 30 | 1 | break; |
|
| 31 | 1 | case 'notes': |
|
| 32 | 1 | $product->setNotes($value); |
|
| 33 | 1 | break; |
|
| 34 | 1 | case 'creation_datetime': |
|
| 35 | 1 | $product->setCreationDatetime(new DateTime($value)); |
|
| 36 | 1 | break; |
|
| 37 | 1 | case 'modification_datetime': |
|
| 38 | 1 | $product->setModificationDatetime(new DateTime($value)); |
|
| 39 | 1 | break; |
|
| 40 | } |
||
| 41 | } |
||
| 42 | 1 | return $product; |
|
| 43 | } |
||
| 44 | } |
||
| 45 |