| Total Lines | 101 |
| Code Lines | 28 |
| 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 |
||
| 28 | public static function castUsing(array $arguments) |
||
| 29 | { |
||
| 30 | return new class ($arguments) implements CastsAttributes { |
||
| 31 | /** |
||
| 32 | * @var array<class-string<TEnum>> |
||
| 33 | */ |
||
| 34 | protected $arguments; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param array<class-string<TEnum>> $arguments |
||
| 38 | * |
||
| 39 | * @SuppressWarnings("PHPMD.UndefinedVariable") |
||
| 40 | */ |
||
| 41 | public function __construct(array $arguments) |
||
| 42 | { |
||
| 43 | $this->arguments = $arguments; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param $model |
||
| 48 | * @param $key |
||
| 49 | * @param $value |
||
| 50 | * @param $attributes |
||
| 51 | * @return ArrayObject|void |
||
| 52 | * |
||
| 53 | * @SuppressWarnings("PHPMD.UndefinedVariable") |
||
| 54 | */ |
||
| 55 | public function get($model, $key, $value, $attributes) |
||
| 56 | { |
||
| 57 | if (! isset($attributes[$key])) { |
||
| 58 | return; |
||
| 59 | } |
||
| 60 | |||
| 61 | $data = $attributes[$key]; |
||
| 62 | if (is_object($data)) { |
||
| 63 | $data = (array) $data; |
||
| 64 | } |
||
| 65 | |||
| 66 | if (! is_array($data)) { |
||
| 67 | return; |
||
| 68 | } |
||
| 69 | |||
| 70 | $enumClass = $this->arguments[0]; |
||
| 71 | |||
| 72 | return new ArrayObject((new Collection($data))->map(function ($value) use ($enumClass) { |
||
| 73 | return is_subclass_of($enumClass, BackedEnum::class) |
||
| 74 | ? $enumClass::from($value) |
||
| 75 | : constant($enumClass . '::' . $value); |
||
| 76 | })->toArray()); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param Model $model |
||
| 81 | * @param string $key |
||
| 82 | * @param mixed $value |
||
| 83 | * @param mixed[] $attributes |
||
| 84 | * @return mixed[] |
||
| 85 | * |
||
| 86 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
||
| 87 | * @SuppressWarnings("PHPMD.UndefinedVariable") |
||
| 88 | */ |
||
| 89 | public function set($model, $key, $value, $attributes) |
||
| 90 | { |
||
| 91 | if ($value === null) { |
||
| 92 | return [$key => null]; |
||
| 93 | } |
||
| 94 | |||
| 95 | $storable = []; |
||
| 96 | |||
| 97 | foreach ($value as $enum) { |
||
| 98 | $storable[] = $this->getStorableEnumValue($enum); |
||
| 99 | } |
||
| 100 | |||
| 101 | return [$key => $storable]; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param Model $model |
||
| 106 | * @param string $key |
||
| 107 | * @param mixed $value |
||
| 108 | * @param mixed[] $attributes |
||
| 109 | * @return mixed[] |
||
| 110 | */ |
||
| 111 | public function serialize($model, string $key, $value, array $attributes) |
||
| 112 | { |
||
| 113 | return (new Collection($value->getArrayCopy()))->map(function ($enum) { |
||
| 114 | return $this->getStorableEnumValue($enum); |
||
| 115 | })->toArray(); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param mixed $enum |
||
| 120 | * @return int|string |
||
| 121 | */ |
||
| 122 | protected function getStorableEnumValue($enum) |
||
| 123 | { |
||
| 124 | if (is_string($enum) || is_int($enum)) { |
||
| 125 | return $enum; |
||
| 126 | } |
||
| 127 | |||
| 128 | return $enum instanceof BackedEnum ? $enum->value : $enum->name; |
||
| 129 | } |
||
| 146 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths