| Total Lines | 51 |
| Code Lines | 11 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 25 | 1 | public static function castUsing(array $arguments) |
|
| 26 | { |
||
| 27 | 1 | return new class ($arguments) implements CastsAttributes { |
|
| 28 | /** |
||
| 29 | * @param array<array-key, mixed> $arguments |
||
| 30 | */ |
||
| 31 | 1 | public function __construct(protected array $arguments) {} |
|
| 32 | |||
| 33 | /** |
||
| 34 | * @param $model |
||
| 35 | * @param $key |
||
| 36 | * @param $value |
||
| 37 | * @param $attributes |
||
| 38 | * @return Collection|mixed|void|null |
||
| 39 | * |
||
| 40 | * @SuppressWarnings("PHPMD.UndefinedVariable") |
||
| 41 | */ |
||
| 42 | public function get($model, $key, $value, $attributes) |
||
| 43 | { |
||
| 44 | 1 | if (! isset($attributes[$key])) { |
|
| 45 | return; |
||
| 46 | } |
||
| 47 | |||
| 48 | 1 | $data = $attributes[$key]; |
|
| 49 | |||
| 50 | 1 | if (is_object($data)) { |
|
| 51 | $data = (array) $data; |
||
| 52 | } |
||
| 53 | |||
| 54 | |||
| 55 | 1 | $collectionClass = $this->arguments[0] ?? Collection::class; |
|
| 56 | |||
| 57 | 1 | if (! is_a($collectionClass, Collection::class, true)) { |
|
| 58 | throw new InvalidArgumentException('The provided class must extend [' . Collection::class . '].'); |
||
| 59 | } |
||
| 60 | |||
| 61 | 1 | return is_array($data) ? new $collectionClass($data) : null; |
|
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param Model $model |
||
| 66 | * @param string $key |
||
| 67 | * @param mixed $value |
||
| 68 | * @param mixed[] $attributes |
||
| 69 | * @return mixed[] |
||
| 70 | * |
||
| 71 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
||
| 72 | */ |
||
| 73 | public function set($model, $key, $value, $attributes) |
||
| 74 | { |
||
| 75 | 1 | return [$key => $value]; |
|
| 76 | } |
||
| 91 |