LaravelFreelancerNL /
laravel-arangodb
| 1 | <?php |
||||||||
| 2 | |||||||||
| 3 | declare(strict_types=1); |
||||||||
| 4 | |||||||||
| 5 | namespace LaravelFreelancerNL\Aranguent\Eloquent\Casts; |
||||||||
| 6 | |||||||||
| 7 | use BackedEnum; |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 8 | use Illuminate\Contracts\Database\Eloquent\CastsAttributes; |
||||||||
| 9 | use Illuminate\Database\Eloquent\Casts\AsEnumCollection as IlluminateAsEnumCollection; |
||||||||
| 10 | use Illuminate\Support\Collection; |
||||||||
| 11 | use LaravelFreelancerNL\Aranguent\Eloquent\Model; |
||||||||
| 12 | |||||||||
| 13 | /** |
||||||||
| 14 | * @SuppressWarnings("PHPMD.UndefinedVariable") |
||||||||
| 15 | * @SuppressWarnings("PHPMD.UnusedFormalParameter") |
||||||||
| 16 | * @SuppressWarnings("PHPMD.ShortMethodName") |
||||||||
| 17 | */ |
||||||||
| 18 | class AsEnumCollection extends IlluminateAsEnumCollection |
||||||||
| 19 | { |
||||||||
| 20 | /** |
||||||||
| 21 | * Get the caster class to use when casting from / to this cast target. |
||||||||
| 22 | * |
||||||||
| 23 | * @template TEnum of \UnitEnum|\BackedEnum |
||||||||
| 24 | * |
||||||||
| 25 | * @param array{class-string<TEnum>} $arguments |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 26 | * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Support\Collection<array-key, TEnum>, iterable<TEnum>> |
||||||||
| 27 | */ |
||||||||
| 28 | 1 | public static function castUsing(array $arguments) |
|||||||
| 29 | { |
||||||||
| 30 | 1 | return new class ($arguments) implements CastsAttributes { |
|||||||
| 31 | /** |
||||||||
| 32 | * @var array<class-string<TEnum>> |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 33 | */ |
||||||||
| 34 | protected $arguments; |
||||||||
| 35 | |||||||||
| 36 | /** |
||||||||
| 37 | * @param array<class-string<TEnum>> $arguments |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 38 | */ |
||||||||
| 39 | public function __construct(array $arguments) |
||||||||
| 40 | { |
||||||||
| 41 | 1 | $this->arguments = $arguments; |
|||||||
| 42 | } |
||||||||
| 43 | |||||||||
| 44 | public function get($model, $key, $value, $attributes) |
||||||||
| 45 | { |
||||||||
| 46 | 1 | if (! isset($attributes[$key])) { |
|||||||
| 47 | return; |
||||||||
| 48 | } |
||||||||
| 49 | |||||||||
| 50 | 1 | $data = $attributes[$key]; |
|||||||
| 51 | 1 | if (is_object($data)) { |
|||||||
| 52 | $data = (array) $data; |
||||||||
| 53 | } |
||||||||
| 54 | |||||||||
| 55 | 1 | if (! is_array($data)) { |
|||||||
| 56 | return; |
||||||||
| 57 | } |
||||||||
| 58 | |||||||||
| 59 | 1 | $enumClass = $this->arguments[0]; |
|||||||
| 60 | |||||||||
| 61 | 1 | return (new Collection($data))->map(function ($value) use ($enumClass) { |
|||||||
|
0 ignored issues
–
show
$data of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The expression
return new Illuminate\Su...ion(...) { /* ... */ }) returns the type Illuminate\Support\Collection which is incompatible with the return type mandated by Illuminate\Contracts\Dat...\CastsAttributes::get() of Illuminate\Contracts\Database\Eloquent\TGet|null.
In the issue above, the returned value is violating the contract defined by the mentioned interface. Let's take a look at an example: interface HasName {
/** @return string */
public function getName();
}
class Name {
public $name;
}
class User implements HasName {
/** @return string|Name */
public function getName() {
return new Name('foo'); // This is a violation of the ``HasName`` interface
// which only allows a string value to be returned.
}
}
Loading history...
|
|||||||||
| 62 | 1 | return is_subclass_of($enumClass, BackedEnum::class) |
|||||||
| 63 | ? $enumClass::from($value) |
||||||||
| 64 | 1 | : constant($enumClass . '::' . $value); |
|||||||
| 65 | 1 | }); |
|||||||
| 66 | } |
||||||||
| 67 | |||||||||
| 68 | public function set($model, $key, $value, $attributes) |
||||||||
| 69 | { |
||||||||
| 70 | 1 | $value = $value !== null |
|||||||
| 71 | 1 | ? (new Collection($value))->map(function ($enum) { |
|||||||
| 72 | 1 | return $this->getStorableEnumValue($enum); |
|||||||
| 73 | 1 | })->jsonSerialize() |
|||||||
| 74 | : null; |
||||||||
| 75 | |||||||||
| 76 | 1 | return [$key => $value]; |
|||||||
| 77 | } |
||||||||
| 78 | |||||||||
| 79 | /** |
||||||||
| 80 | * @param Model $model |
||||||||
| 81 | * @param string $key |
||||||||
| 82 | * @param mixed $value |
||||||||
| 83 | * @param mixed[] $attributes |
||||||||
| 84 | * @return mixed[] |
||||||||
| 85 | */ |
||||||||
| 86 | public function serialize($model, string $key, $value, array $attributes) |
||||||||
|
0 ignored issues
–
show
The parameter
$key is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$attributes is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$model is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||||
| 87 | { |
||||||||
| 88 | return (new Collection($value))->map(function ($enum) { |
||||||||
| 89 | return $this->getStorableEnumValue($enum); |
||||||||
| 90 | })->toArray(); |
||||||||
| 91 | } |
||||||||
| 92 | |||||||||
| 93 | /** |
||||||||
| 94 | * @param mixed $enum |
||||||||
| 95 | * @return int|string |
||||||||
| 96 | */ |
||||||||
| 97 | protected function getStorableEnumValue($enum) |
||||||||
| 98 | { |
||||||||
| 99 | 1 | if (is_string($enum) || is_int($enum)) { |
|||||||
| 100 | return $enum; |
||||||||
| 101 | } |
||||||||
| 102 | |||||||||
| 103 | 1 | return $enum instanceof BackedEnum ? $enum->value : $enum->name; |
|||||||
| 104 | } |
||||||||
| 105 | 1 | }; |
|||||||
| 106 | } |
||||||||
| 107 | |||||||||
| 108 | /** |
||||||||
| 109 | * Specify the Enum for the cast. |
||||||||
| 110 | * |
||||||||
| 111 | * @param class-string $class |
||||||||
|
0 ignored issues
–
show
|
|||||||||
| 112 | * @return string |
||||||||
| 113 | */ |
||||||||
| 114 | 18 | public static function of($class) |
|||||||
| 115 | { |
||||||||
| 116 | 18 | return static::class . ':' . $class; |
|||||||
| 117 | } |
||||||||
| 118 | } |
||||||||
| 119 |
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