1 | <?php |
||
2 | |||
3 | namespace Spatie\Enum\Laravel; |
||
4 | |||
5 | use Illuminate\Contracts\Database\Eloquent\Castable; |
||
6 | use Illuminate\Contracts\Database\Eloquent\CastsAttributes; |
||
7 | use Illuminate\Contracts\Support\Jsonable; |
||
8 | use Spatie\Enum\Enum as BaseEnum; |
||
9 | use Spatie\Enum\Laravel\Casts\EnumCast; |
||
10 | use Spatie\Enum\Laravel\Casts\EnumCollectionCast; |
||
11 | |||
12 | abstract class Enum extends BaseEnum implements Jsonable, Castable |
||
13 | { |
||
14 | public static function castUsing(array $arguments): CastsAttributes |
||
15 | { |
||
16 | if (in_array('collection', $arguments)) { |
||
17 | return new EnumCollectionCast(static::class, ...$arguments); |
||
0 ignored issues
–
show
|
|||
18 | } |
||
19 | |||
20 | return new EnumCast(static::class, ...$arguments); |
||
0 ignored issues
–
show
The expression
return new Spatie\Enum\L...tic::class, $arguments) returns the type Spatie\Enum\Laravel\Casts\EnumCast which is incompatible with the return type mandated by Illuminate\Contracts\Dat...t\Castable::castUsing() of string .
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.
}
}
![]() |
|||
21 | } |
||
22 | |||
23 | public function toJson($options = 0): string |
||
24 | { |
||
25 | return json_encode($this->jsonSerialize(), $options); |
||
26 | } |
||
27 | } |
||
28 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: