From d8402c33976a4905ffede63f3966eada05a5293e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Sat, 11 Sep 2021 13:35:17 +0200 Subject: [PATCH] Fixes from code analyzer --- src/BaseEnumRoutable.php | 2 +- src/EnumServiceProvider.php | 2 +- src/Rules/EnumIsValidRule.php | 7 +++++-- src/Traits/CastsEnums.php | 23 ++++++++++++++++++++--- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/BaseEnumRoutable.php b/src/BaseEnumRoutable.php index 4ffa946..d8ecedc 100644 --- a/src/BaseEnumRoutable.php +++ b/src/BaseEnumRoutable.php @@ -39,7 +39,7 @@ abstract class BaseEnumRoutable extends BaseEnum implements UrlRoutable * * @param mixed $value * @param string|null $field - * @return \Illuminate\Database\Eloquent\Model|null + * @return \Kwaadpepper\Enum\BaseEnum|null */ // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassAfterLastUsed public function resolveRouteBinding($value, $field = null) diff --git a/src/EnumServiceProvider.php b/src/EnumServiceProvider.php index 33602cd..3ef9010 100644 --- a/src/EnumServiceProvider.php +++ b/src/EnumServiceProvider.php @@ -13,7 +13,7 @@ class EnumServiceProvider extends ServiceProvider */ public function boot() { - $this->loadJsonTranslationsFrom(__DIR__ . '/../resources/lang', 'enum'); + $this->loadJsonTranslationsFrom(__DIR__ . '/../resources/lang'); $this->publishes([ __DIR__ . '/../resources/lang' => resource_path('lang/vendor/enum'), diff --git a/src/Rules/EnumIsValidRule.php b/src/Rules/EnumIsValidRule.php index c3f36b3..ab87494 100644 --- a/src/Rules/EnumIsValidRule.php +++ b/src/Rules/EnumIsValidRule.php @@ -40,13 +40,16 @@ class EnumIsValidRule extends BaseEnumRule */ public function message(): string { + $message = null; switch ($this->messageType) { case 1: - return trans('enum::enum.isNotValid'); + $message = trans('enum::enum.isNotValid'); + break; case 2: - return trans('enum::enum.notInList', [ + $message = trans('enum::enum.notInList', [ 'values' => implode(',', forward_static_call([$this->enumClass, 'toValues'])) ]); } + return \is_string($message) ? $message : 'Invalid Enum'; } } diff --git a/src/Traits/CastsEnums.php b/src/Traits/CastsEnums.php index ac72907..e0552ef 100644 --- a/src/Traits/CastsEnums.php +++ b/src/Traits/CastsEnums.php @@ -6,9 +6,26 @@ use Kwaadpepper\Enum\BaseEnum; /** * @property array $enumCasts Map attribute names to enum classes. + * @method bool hasCast(string $key, array|string|null $types = null) + * @method mixed castAttribute(string $key, mixed $value) */ trait CastsEnums { + + /** + * The model's attributes. + * + * @var array + */ + protected $attributes = []; + + /** + * Propertis that should be casted to enum + * + * @var array + */ + protected $enumCasts = []; + /** * Get a plain attribute (not a relationship). * @@ -36,7 +53,7 @@ trait CastsEnums public function setAttribute($key, $value) { if ($value !== null && $this->hasEnumCast($key)) { - /** @var \App\Enums\BaseEnum $enum */ + /** @var \Kwaadpepper\Enum\BaseEnum $enum */ $enum = $this->enumCasts[$key]; if ($value instanceof $enum) { @@ -80,11 +97,11 @@ trait CastsEnums * * @param string $key * @param mixed $value - * @return \App\Enums\BaseEnum|null + * @return \Kwaadpepper\Enums\BaseEnum|null */ protected function castToEnum($key, $value): ?BaseEnum { - /** @var \App\Enums\BaseEnum $enum */ + /** @var \Kwaadpepper\Enum\BaseEnum $enum */ $enum = $this->enumCasts[$key]; if ($value === null || $value instanceof BaseEnum) {