From 2cfd0013c8b0cde6ff7f257b8c516672cdd3d396 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Mon, 23 Oct 2023 08:31:27 +0200 Subject: [PATCH] Adjustements, add test --- src/Domain/Models/AttributeTypeCaster.php | 16 +++++++++++----- tests/Fixtures/User.php | 10 +--------- tests/expected/User.stub | 3 ++- tests/expected/_ide_models.stub | 2 +- tests/expected/_ide_modelsLarastan.stub | 2 +- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Domain/Models/AttributeTypeCaster.php b/src/Domain/Models/AttributeTypeCaster.php index a927b81..5a8e694 100644 --- a/src/Domain/Models/AttributeTypeCaster.php +++ b/src/Domain/Models/AttributeTypeCaster.php @@ -28,13 +28,13 @@ public function __construct(Model $model) public function resolve(Attribute $attribute): Attribute { switch (true) { - case $this->isTimestamps($attribute): - $type = $this->dateClass(); - - break; case $this->hasCast($attribute) && $this->isNotInboundCast($attribute): $type = $this->resolveFromCast($attribute); + break; + case $this->isTimestamps($attribute): + $type = $this->dateClass(); + break; default: $type = $this->resolveFromDatabaseType($attribute); @@ -105,9 +105,10 @@ private function resolveFromCast(Attribute $attribute): string case 'date': case 'datetime': case 'custom_datetime': + return $this->dateClass(); case 'immutable_date': case 'immutable_datetime': - return $this->dateClass(); + return $this->immutableDateClass(); } if ($this->isCustomCast($castType)) { @@ -176,6 +177,11 @@ private function dateClass(): string return '\\' . get_class(Date::now()); } + private function immutableDateClass(): string + { + return '\\' . get_class(Date::now()->toImmutable()); + } + private function isCustomCast(string $castType): bool { return class_exists($castType) diff --git a/tests/Fixtures/User.php b/tests/Fixtures/User.php index 35765c2..fe725f9 100644 --- a/tests/Fixtures/User.php +++ b/tests/Fixtures/User.php @@ -15,15 +15,6 @@ /** * This model is used for testing purposes. * @generated - * @property int $id - * @property string $email - * @property string $password - * @property \Soyhuce\NextIdeHelper\Tests\Fixtures\Address $address - * @property string|null $remember_token - * @property \Illuminate\Support\Carbon|null $created_at - * @property \Illuminate\Support\Carbon|null $updated_at - * @property-read \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection $laravelPosts - * @property-read \Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\PostCollection $posts */ class User extends Model { @@ -36,6 +27,7 @@ class User extends Model 'nullable_name' => Uppercase::class, 'role' => Role::class, 'password' => 'hashed', + 'updated_at' => 'immutable_datetime', ]; public function posts(): HasMany diff --git a/tests/expected/User.stub b/tests/expected/User.stub index e81a951..5f3e50a 100644 --- a/tests/expected/User.stub +++ b/tests/expected/User.stub @@ -24,7 +24,7 @@ use Soyhuce\NextIdeHelper\Tests\Fixtures\Blog\Post; * @property string|null $nullable_name * @property string|null $remember_token * @property \Illuminate\Support\Carbon $created_at - * @property \Illuminate\Support\Carbon $updated_at + * @property \Carbon\CarbonImmutable $updated_at * @property string $screamed_email * @property \Soyhuce\NextIdeHelper\Tests\Fixtures\Address|null $shipping_address * @property-read string $city @@ -45,6 +45,7 @@ class User extends Model 'nullable_name' => Uppercase::class, 'role' => Role::class, 'password' => 'hashed', + 'updated_at' => 'immutable_datetime', ]; public function posts(): HasMany diff --git a/tests/expected/_ide_models.stub b/tests/expected/_ide_models.stub index 6bb111b..5cc210c 100644 --- a/tests/expected/_ide_models.stub +++ b/tests/expected/_ide_models.stub @@ -40,7 +40,7 @@ namespace IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereNullableName(string|null $value) * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereRememberToken(string|null $value) * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereCreatedAt(\Illuminate\Support\Carbon|string $value) - * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereUpdatedAt(\Illuminate\Support\Carbon|string $value) + * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereUpdatedAt(\Carbon\CarbonImmutable|string $value) * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereEmailDomain(string $domain, ?string $area = null) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User create(array $attributes = []) * @method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\User|null find($id, array $columns = ['*']) diff --git a/tests/expected/_ide_modelsLarastan.stub b/tests/expected/_ide_modelsLarastan.stub index ef765f7..d2d4176 100644 --- a/tests/expected/_ide_modelsLarastan.stub +++ b/tests/expected/_ide_modelsLarastan.stub @@ -42,7 +42,7 @@ namespace IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereNullableName(string|null $value) * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereRememberToken(string|null $value) * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereCreatedAt(\Illuminate\Support\Carbon|string $value) - * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereUpdatedAt(\Illuminate\Support\Carbon|string $value) + * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereUpdatedAt(\Carbon\CarbonImmutable|string $value) * @method \IdeHelper\Soyhuce\NextIdeHelper\Tests\Fixtures\__UserQuery whereEmailDomain(string $domain, ?string $area = null) * @method \Soyhuce\NextIdeHelper\Tests\Fixtures\User create(array $attributes = []) * @method \Illuminate\Database\Eloquent\Collection|\Soyhuce\NextIdeHelper\Tests\Fixtures\User|null find($id, array $columns = ['*'])