From b3a79fe6c19c8ae64aba2a590ba86f379a2ee5d2 Mon Sep 17 00:00:00 2001 From: Matthew Hailwood Date: Fri, 4 Dec 2020 19:33:45 +1300 Subject: [PATCH] Allow casts with a return type of static or this to reference themselves (#1103) * Allow casts with a return type of static or this to reference themselves Supports ```php /** * @param $model * @param string $key * @param $value * @param array $attributes * @return static */ public function get($model, string $key, $value, array $attributes) { return new static($value); } ``` or ```php /** * @param $model * @param string $key * @param $value * @param array $attributes * @return $this */ public function get($model, string $key, $value, array $attributes) { return new static($value); } ``` * Update CHANGELOG.md --- CHANGELOG.md | 1 + src/Console/ModelsCommand.php | 12 ++++++--- ...fCastingCasterWithStaticDocblockReturn.php | 12 +++++++++ ...elfCastingCasterWithThisDocblockReturn.php | 12 +++++++++ ...fCastingCasterWithStaticDocblockReturn.php | 26 +++++++++++++++++++ ...elfCastingCasterWithThisDocblockReturn.php | 26 +++++++++++++++++++ .../LaravelCustomCasts/Models/CustomCast.php | 9 +++++++ .../__snapshots__/Test__test__1.php | 19 ++++++++++++++ .../migrations/____custom_casts_table.php | 5 ++++ 9 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Casts/ExtendedSelfCastingCasterWithStaticDocblockReturn.php create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Casts/ExtendedSelfCastingCasterWithThisDocblockReturn.php create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Casts/SelfCastingCasterWithStaticDocblockReturn.php create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Casts/SelfCastingCasterWithThisDocblockReturn.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c3e4da6..718b169 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file. - Compatibility with Lumen [\#1043 / mfn](https://github.com/barryvdh/laravel-ide-helper/pull/1043) - Allow model_locations to have glob patterns [\#1059 / saackearl](https://github.com/barryvdh/laravel-ide-helper/pull/1059) - Error when generating helper for macroable classes which are not facades and contain a "fake" method [\#1066 / domkrm] (https://github.com/barryvdh/laravel-ide-helper/pull/1066) +- Casts with a return type of `static` or `$this` now resolve to an instance of the cast [\#1103 / riesjart](https://github.com/barryvdh/laravel-ide-helper/pull/1103) 2020-09-07, 2.8.1 ----------------- diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index a17a5de..4dc2215 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -1089,13 +1089,17 @@ class ModelsCommand extends Command $methodReflection = new \ReflectionMethod($type, 'get'); - $type = $this->getReturnTypeFromReflection($methodReflection); + $reflectionType = $this->getReturnTypeFromReflection($methodReflection); - if ($type === null) { - $type = $this->getReturnTypeFromDocBlock($methodReflection); + if ($reflectionType === null) { + $reflectionType = $this->getReturnTypeFromDocBlock($methodReflection); + } + + if($reflectionType === 'static' || $reflectionType === '$this') { + $reflectionType = $type; } - return $type; + return $reflectionType; } protected function getTypeInModel(object $model, ?string $type): ?string diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/ExtendedSelfCastingCasterWithStaticDocblockReturn.php b/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/ExtendedSelfCastingCasterWithStaticDocblockReturn.php new file mode 100644 index 0000000..1196566 --- /dev/null +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/ExtendedSelfCastingCasterWithStaticDocblockReturn.php @@ -0,0 +1,12 @@ + CustomCasterWithNullablePrimitiveReturn::class, 'casted_property_without_return' => CustomCasterWithoutReturnType::class, 'casted_property_with_param' => CustomCasterWithParam::class . ':param', + 'casted_property_with_static_return_docblock' => SelfCastingCasterWithStaticDocblockReturn::class, + 'casted_property_with_this_return_docblock' => SelfCastingCasterWithThisDocblockReturn::class, + 'extended_casted_property_with_static_return_docblock' => ExtendedSelfCastingCasterWithStaticDocblockReturn::class, + 'extended_casted_property_with_this_return_docblock' => ExtendedSelfCastingCasterWithThisDocblockReturn::class, + 'casted_property_with_static_return_docblock_and_param' => SelfCastingCasterWithStaticDocblockReturn::class .':param', ]; } diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/LaravelCustomCasts/__snapshots__/Test__test__1.php index 8b55303..f29ffad 100644 --- a/tests/Console/ModelsCommand/LaravelCustomCasts/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/__snapshots__/Test__test__1.php @@ -12,6 +12,10 @@ use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Cas use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithPrimitiveDocblockReturn; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithPrimitiveReturn; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithReturnType; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\ExtendedSelfCastingCasterWithStaticDocblockReturn; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\ExtendedSelfCastingCasterWithThisDocblockReturn; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\SelfCastingCasterWithStaticDocblockReturn; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\SelfCastingCasterWithThisDocblockReturn; use Illuminate\Database\Eloquent\Model; /** @@ -25,6 +29,11 @@ use Illuminate\Database\Eloquent\Model; * @property array|null $casted_property_with_return_nullable_primitive * @property $casted_property_without_return * @property \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastedProperty $casted_property_with_param + * @property SelfCastingCasterWithStaticDocblockReturn $casted_property_with_static_return_docblock + * @property SelfCastingCasterWithThisDocblockReturn $casted_property_with_this_return_docblock + * @property ExtendedSelfCastingCasterWithStaticDocblockReturn $extended_casted_property_with_static_return_docblock + * @property ExtendedSelfCastingCasterWithThisDocblockReturn $extended_casted_property_with_this_return_docblock + * @property SelfCastingCasterWithStaticDocblockReturn $casted_property_with_static_return_docblock_and_param * @method static \Illuminate\Database\Eloquent\Builder|CustomCast newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|CustomCast newQuery() * @method static \Illuminate\Database\Eloquent\Builder|CustomCast query() @@ -35,7 +44,12 @@ use Illuminate\Database\Eloquent\Model; * @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithReturnPrimitive($value) * @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithReturnPrimitiveDocblock($value) * @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithReturnType($value) + * @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithStaticReturnDocblock($value) + * @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithStaticReturnDocblockAndParam($value) + * @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithThisReturnDocblock($value) * @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereCastedPropertyWithoutReturn($value) + * @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereExtendedCastedPropertyWithStaticReturnDocblock($value) + * @method static \Illuminate\Database\Eloquent\Builder|CustomCast whereExtendedCastedPropertyWithThisReturnDocblock($value) * @mixin \Eloquent */ class CustomCast extends Model @@ -49,5 +63,10 @@ class CustomCast extends Model 'casted_property_with_return_nullable_primitive' => CustomCasterWithNullablePrimitiveReturn::class, 'casted_property_without_return' => CustomCasterWithoutReturnType::class, 'casted_property_with_param' => CustomCasterWithParam::class . ':param', + 'casted_property_with_static_return_docblock' => SelfCastingCasterWithStaticDocblockReturn::class, + 'casted_property_with_this_return_docblock' => SelfCastingCasterWithThisDocblockReturn::class, + 'extended_casted_property_with_static_return_docblock' => ExtendedSelfCastingCasterWithStaticDocblockReturn::class, + 'extended_casted_property_with_this_return_docblock' => ExtendedSelfCastingCasterWithThisDocblockReturn::class, + 'casted_property_with_static_return_docblock_and_param' => SelfCastingCasterWithStaticDocblockReturn::class .':param', ]; } diff --git a/tests/Console/ModelsCommand/migrations/____custom_casts_table.php b/tests/Console/ModelsCommand/migrations/____custom_casts_table.php index ccf3e54..845d9b9 100644 --- a/tests/Console/ModelsCommand/migrations/____custom_casts_table.php +++ b/tests/Console/ModelsCommand/migrations/____custom_casts_table.php @@ -19,6 +19,11 @@ class CustomCastsTable extends Migration $table->string('casted_property_with_return_nullable_primitive'); $table->string('casted_property_without_return'); $table->string('casted_property_with_param'); + $table->string('casted_property_with_static_return_docblock'); + $table->string('casted_property_with_this_return_docblock'); + $table->string('extended_casted_property_with_static_return_docblock'); + $table->string('extended_casted_property_with_this_return_docblock'); + $table->string('casted_property_with_static_return_docblock_and_param'); }); } }