From 2673bf48892470529a8ad0626a824757181bf1a5 Mon Sep 17 00:00:00 2001 From: BinotaLIU Date: Tue, 25 Jan 2022 04:39:17 +0800 Subject: [PATCH] return `mixed` if we failed detect castable type (#1306) fixed #1305 --- CHANGELOG.md | 2 ++ src/Console/ModelsCommand.php | 3 ++- .../Casts/CastableWithoutReturnType.php | 26 +++++++++++++++++++ .../LaravelCustomCasts/Models/CustomCast.php | 2 ++ .../__snapshots__/Test__test__1.php | 3 +++ 5 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CastableWithoutReturnType.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c6fe70..e2eb891 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. [Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.12.0...master) -------------- +### Fixed +- Properly handle `Castable`s without return type. [#1306 / binotaliu](https://github.com/barryvdh/laravel-ide-helper/pull/1306) 2022-01-23, 2.12.0 ------------------ diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index b498b4f..92f7ebd 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -1291,7 +1291,8 @@ class ModelsCommand extends Command $methodReflection = $castReflection->getMethod('get'); return $this->getReturnTypeFromReflection($methodReflection) ?? - $this->getReturnTypeFromDocBlock($methodReflection, $reflection); + $this->getReturnTypeFromDocBlock($methodReflection, $reflection) ?? + 'mixed'; } /** diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CastableWithoutReturnType.php b/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CastableWithoutReturnType.php new file mode 100644 index 0000000..c4d4991 --- /dev/null +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CastableWithoutReturnType.php @@ -0,0 +1,26 @@ + SelfCastingCasterWithThisDocblockReturn::class, 'casted_property_with_castable' => CastableReturnsCustomCaster::class, 'casted_property_with_anonymous_cast' => CastableReturnsAnonymousCaster::class, + 'casted_property_without_return_type' => CastableWithoutReturnType::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 2454d8f..fd28b72 100644 --- a/tests/Console/ModelsCommand/LaravelCustomCasts/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/__snapshots__/Test__test__1.php @@ -6,6 +6,7 @@ namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCas use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastableReturnsAnonymousCaster; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastableReturnsCustomCaster; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastableWithoutReturnType; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithDocblockReturn; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithDocblockReturnFqn; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithNullablePrimitiveReturn; @@ -38,6 +39,7 @@ use Illuminate\Database\Eloquent\Model; * @property SelfCastingCasterWithStaticDocblockReturn $casted_property_with_static_return_docblock_and_param * @property \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastedProperty $casted_property_with_castable * @property \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastedProperty $casted_property_with_anonymous_cast + * @property mixed $casted_property_without_return_type * @property \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CastedProperty $cast_without_property * @method static \Illuminate\Database\Eloquent\Builder|CustomCast newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|CustomCast newQuery() @@ -72,6 +74,7 @@ class CustomCast extends Model 'casted_property_with_this_return_docblock' => SelfCastingCasterWithThisDocblockReturn::class, 'casted_property_with_castable' => CastableReturnsCustomCaster::class, 'casted_property_with_anonymous_cast' => CastableReturnsAnonymousCaster::class, + 'casted_property_without_return_type' => CastableWithoutReturnType::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',