From 2300647b90017f211c7caf89cb2dd65f89aabfcb Mon Sep 17 00:00:00 2001 From: belamov Date: Sat, 11 Apr 2020 08:20:36 +0500 Subject: [PATCH 1/4] added support for laravel 7 custom casts --- src/Console/ModelsCommand.php | 43 +++++++++ .../Casts/CastedProperty.php | 8 ++ .../Casts/CustomCasterWithDocblockReturn.php | 25 +++++ .../Casts/CustomCasterWithReturnType.php | 24 +++++ .../DocblockReturnTypeTest.php | 95 +++++++++++++++++++ .../CustomCastWithDocblockReturnType.php | 14 +++ .../CustomCastWithReturnType.php | 14 +++ .../LaravelCustomCasts/ReturnTypeTest.php | 95 +++++++++++++++++++ .../migrations/____custom_casts_table.php | 15 +++ 9 files changed, 333 insertions(+) create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CastedProperty.php create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CustomCasterWithDocblockReturn.php create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CustomCasterWithReturnType.php create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/DocblockReturnTypeTest.php create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithDocblockReturnType/CustomCastWithDocblockReturnType.php create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithReturnType/CustomCastWithReturnType.php create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/ReturnTypeTest.php create mode 100644 tests/Console/ModelsCommand/migrations/____custom_casts_table.php diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index ed9d2d6fa..fa2528896 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -12,6 +12,7 @@ use Composer\Autoload\ClassMapGenerator; use Illuminate\Console\Command; +use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Str; use Illuminate\Filesystem\Filesystem; @@ -314,6 +315,8 @@ protected function castPropertiesType($model) if (!isset($this->properties[$name])) { continue; } else { + $realType = $this->checkForCustomLaravelCasts($realType); + $this->properties[$name]['type'] = $this->getTypeOverride($realType); if (isset($this->nullableColumns[$name])) { @@ -866,4 +869,44 @@ private function getClassKeyword(ReflectionClass $reflection) return $keyword; } + + protected function checkForCustomLaravelCasts(string $type): ?string + { + if (!class_exists($type)) { + return $type; + } + + $reflection = new \ReflectionClass($type); + + if (!$reflection->implementsInterface(CastsAttributes::class)) { + return $type; + } + + $methodReflection = new \ReflectionMethod($type, 'get'); + + return $this->getReturnTypeFromReflection($methodReflection); + } + + protected function getReturnTypeFromReflection(\ReflectionMethod $reflection): ?string + { + $returnType = $reflection->getReturnType(); + + if (!$returnType) { + return $this->getReturnTypeFromDocBlock($reflection); + } + + $type = $returnType instanceof \ReflectionNamedType + ? $returnType->getName() + : (string)$returnType; + + if (!$returnType->isBuiltin()) { + $type = '\\' . $type; + } + + if ($returnType->allowsNull()) { + $type .= '|null'; + } + + return $type; + } } diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CastedProperty.php b/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CastedProperty.php new file mode 100644 index 000000000..e0dd9fb29 --- /dev/null +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CastedProperty.php @@ -0,0 +1,8 @@ +markTestSkipped('This test requires Laravel 7.0 or higher'); + return; + } + } + + protected function getEnvironmentSetUp($app) + { + parent::getEnvironmentSetUp($app); + + $app['config']->set('ide-helper', [ + 'model_locations' => [ + // This is calculated from the base_path() which points to + // vendor/orchestra/testbench-core/laravel + '/../../../../tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithDocblockReturnType', + ], + ]); + } + + public function test_it_parses_casted_object_with_docblock_return_type(): void + { + $actualContent = null; + $mockFilesystem = Mockery::mock(Filesystem::class); + $mockFilesystem + ->shouldReceive('get') + ->andReturn(file_get_contents(__DIR__.'/Models/CustomCastWithDocblockReturnType/CustomCastWithDocblockReturnType.php')) + ->once(); + $mockFilesystem + ->shouldReceive('put') + ->with( + Mockery::any(), + Mockery::capture($actualContent) + ) + ->andReturn(1) // Simulate we wrote _something_ to the file + ->once(); + + $this->instance(Filesystem::class, $mockFilesystem); + + $command = $this->app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, [ + '--write' => true, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertEmpty($tester->getDisplay()); +//dd($actualContent); + $expectedContent = <<<'PHP' + CustomCasterWithDocblockReturn::class + ]; +} + +PHP; + + $this->assertSame($expectedContent, $actualContent); + } +} diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithDocblockReturnType/CustomCastWithDocblockReturnType.php b/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithDocblockReturnType/CustomCastWithDocblockReturnType.php new file mode 100644 index 000000000..97d469e74 --- /dev/null +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithDocblockReturnType/CustomCastWithDocblockReturnType.php @@ -0,0 +1,14 @@ + CustomCasterWithDocblockReturn::class + ]; +} diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithReturnType/CustomCastWithReturnType.php b/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithReturnType/CustomCastWithReturnType.php new file mode 100644 index 000000000..a4588f82d --- /dev/null +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithReturnType/CustomCastWithReturnType.php @@ -0,0 +1,14 @@ + CustomCasterWithReturnType::class + ]; +} diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/ReturnTypeTest.php b/tests/Console/ModelsCommand/LaravelCustomCasts/ReturnTypeTest.php new file mode 100644 index 000000000..441224ce1 --- /dev/null +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/ReturnTypeTest.php @@ -0,0 +1,95 @@ +markTestSkipped('This test requires Laravel 7.0 or higher'); + return; + } + } + + protected function getEnvironmentSetUp($app) + { + parent::getEnvironmentSetUp($app); + + $app['config']->set('ide-helper', [ + 'model_locations' => [ + // This is calculated from the base_path() which points to + // vendor/orchestra/testbench-core/laravel + '/../../../../tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithReturnType', + ], + ]); + } + + public function test_it_parses_casted_object_with_return_type(): void + { + $actualContent = null; + $mockFilesystem = Mockery::mock(Filesystem::class); + $mockFilesystem + ->shouldReceive('get') + ->andReturn(file_get_contents(__DIR__.'/Models/CustomCastWithReturnType/CustomCastWithReturnType.php')) + ->once(); + $mockFilesystem + ->shouldReceive('put') + ->with( + Mockery::any(), + Mockery::capture($actualContent) + ) + ->andReturn(1) // Simulate we wrote _something_ to the file + ->once(); + + $this->instance(Filesystem::class, $mockFilesystem); + + $command = $this->app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, [ + '--write' => true, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertEmpty($tester->getDisplay()); + + $expectedContent = <<<'PHP' + CustomCasterWithReturnType::class + ]; +} + +PHP; + + $this->assertSame($expectedContent, $actualContent); + } +} diff --git a/tests/Console/ModelsCommand/migrations/____custom_casts_table.php b/tests/Console/ModelsCommand/migrations/____custom_casts_table.php new file mode 100644 index 000000000..14a6287a9 --- /dev/null +++ b/tests/Console/ModelsCommand/migrations/____custom_casts_table.php @@ -0,0 +1,15 @@ +string('casted_property'); + }); + } +} From cb0ce8bf742c93ed42a86f670507447eb65f1aab Mon Sep 17 00:00:00 2001 From: belamov Date: Sun, 12 Apr 2020 18:58:24 +0500 Subject: [PATCH 2/4] refactored tests; added test cases --- ...ustomCasterWithNullablePrimitiveReturn.php | 25 +++++ ...ustomCasterWithPrimitiveDocblockReturn.php | 25 +++++ .../Casts/CustomCasterWithPrimitiveReturn.php | 25 +++++ .../DocblockReturnTypeTest.php | 95 ------------------- .../LaravelCustomCasts/Models/CustomCast.php | 21 ++++ .../CustomCastWithDocblockReturnType.php | 14 --- .../CustomCastWithReturnType.php | 14 --- .../{ReturnTypeTest.php => Test.php} | 44 ++++++--- .../migrations/____custom_casts_table.php | 6 +- 9 files changed, 130 insertions(+), 139 deletions(-) create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CustomCasterWithNullablePrimitiveReturn.php create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CustomCasterWithPrimitiveDocblockReturn.php create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CustomCasterWithPrimitiveReturn.php delete mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/DocblockReturnTypeTest.php create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCast.php delete mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithDocblockReturnType/CustomCastWithDocblockReturnType.php delete mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithReturnType/CustomCastWithReturnType.php rename tests/Console/ModelsCommand/LaravelCustomCasts/{ReturnTypeTest.php => Test.php} (53%) diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CustomCasterWithNullablePrimitiveReturn.php b/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CustomCasterWithNullablePrimitiveReturn.php new file mode 100644 index 000000000..d960bfb7f --- /dev/null +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CustomCasterWithNullablePrimitiveReturn.php @@ -0,0 +1,25 @@ +markTestSkipped('This test requires Laravel 7.0 or higher'); - return; - } - } - - protected function getEnvironmentSetUp($app) - { - parent::getEnvironmentSetUp($app); - - $app['config']->set('ide-helper', [ - 'model_locations' => [ - // This is calculated from the base_path() which points to - // vendor/orchestra/testbench-core/laravel - '/../../../../tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithDocblockReturnType', - ], - ]); - } - - public function test_it_parses_casted_object_with_docblock_return_type(): void - { - $actualContent = null; - $mockFilesystem = Mockery::mock(Filesystem::class); - $mockFilesystem - ->shouldReceive('get') - ->andReturn(file_get_contents(__DIR__.'/Models/CustomCastWithDocblockReturnType/CustomCastWithDocblockReturnType.php')) - ->once(); - $mockFilesystem - ->shouldReceive('put') - ->with( - Mockery::any(), - Mockery::capture($actualContent) - ) - ->andReturn(1) // Simulate we wrote _something_ to the file - ->once(); - - $this->instance(Filesystem::class, $mockFilesystem); - - $command = $this->app->make(ModelsCommand::class); - - $tester = $this->runCommand($command, [ - '--write' => true, - ]); - - $this->assertSame(0, $tester->getStatusCode()); - $this->assertEmpty($tester->getDisplay()); -//dd($actualContent); - $expectedContent = <<<'PHP' - CustomCasterWithDocblockReturn::class - ]; -} - -PHP; - - $this->assertSame($expectedContent, $actualContent); - } -} diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCast.php b/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCast.php new file mode 100644 index 000000000..132711e94 --- /dev/null +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCast.php @@ -0,0 +1,21 @@ + CustomCasterWithReturnType::class, + 'casted_property_with_return_docblock' => CustomCasterWithDocblockReturn::class, + 'casted_property_with_return_primitive' => CustomCasterWithPrimitiveReturn::class, + 'casted_property_with_return_primitive_docblock' => CustomCasterWithPrimitiveDocblockReturn::class, + 'casted_property_with_return_nullable_primitive' => CustomCasterWithNullablePrimitiveReturn::class, + ]; +} diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithDocblockReturnType/CustomCastWithDocblockReturnType.php b/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithDocblockReturnType/CustomCastWithDocblockReturnType.php deleted file mode 100644 index 97d469e74..000000000 --- a/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithDocblockReturnType/CustomCastWithDocblockReturnType.php +++ /dev/null @@ -1,14 +0,0 @@ - CustomCasterWithDocblockReturn::class - ]; -} diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithReturnType/CustomCastWithReturnType.php b/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithReturnType/CustomCastWithReturnType.php deleted file mode 100644 index a4588f82d..000000000 --- a/tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithReturnType/CustomCastWithReturnType.php +++ /dev/null @@ -1,14 +0,0 @@ - CustomCasterWithReturnType::class - ]; -} diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/ReturnTypeTest.php b/tests/Console/ModelsCommand/LaravelCustomCasts/Test.php similarity index 53% rename from tests/Console/ModelsCommand/LaravelCustomCasts/ReturnTypeTest.php rename to tests/Console/ModelsCommand/LaravelCustomCasts/Test.php index 441224ce1..115be13bc 100644 --- a/tests/Console/ModelsCommand/LaravelCustomCasts/ReturnTypeTest.php +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/Test.php @@ -8,7 +8,7 @@ use Illuminate\Foundation\Application; use Mockery; -class ReturnTypeTest extends AbstractModelsCommand +class Test extends AbstractModelsCommand { protected function setUp(): void @@ -17,7 +17,6 @@ protected function setUp(): void if (version_compare(Application::VERSION, '7.0', '<')) { $this->markTestSkipped('This test requires Laravel 7.0 or higher'); - return; } } @@ -29,18 +28,18 @@ protected function getEnvironmentSetUp($app) 'model_locations' => [ // This is calculated from the base_path() which points to // vendor/orchestra/testbench-core/laravel - '/../../../../tests/Console/ModelsCommand/LaravelCustomCasts/Models/CustomCastWithReturnType', + '/../../../../tests/Console/ModelsCommand/LaravelCustomCasts/Models', ], ]); } - public function test_it_parses_casted_object_with_return_type(): void + public function test_it_parses_casted_properties_correctly(): void { $actualContent = null; $mockFilesystem = Mockery::mock(Filesystem::class); $mockFilesystem ->shouldReceive('get') - ->andReturn(file_get_contents(__DIR__.'/Models/CustomCastWithReturnType/CustomCastWithReturnType.php')) + ->andReturn(file_get_contents(__DIR__.'/Models/CustomCast.php')) ->once(); $mockFilesystem ->shouldReceive('put') @@ -65,26 +64,41 @@ public function test_it_parses_casted_object_with_return_type(): void $expectedContent = <<<'PHP' CustomCasterWithReturnType::class + 'casted_property_with_return_type' => CustomCasterWithReturnType::class, + 'casted_property_with_return_docblock' => CustomCasterWithDocblockReturn::class, + 'casted_property_with_return_primitive' => CustomCasterWithPrimitiveReturn::class, + 'casted_property_with_return_primitive_docblock' => CustomCasterWithPrimitiveDocblockReturn::class, + 'casted_property_with_return_nullable_primitive' => CustomCasterWithNullablePrimitiveReturn::class, ]; } diff --git a/tests/Console/ModelsCommand/migrations/____custom_casts_table.php b/tests/Console/ModelsCommand/migrations/____custom_casts_table.php index 14a6287a9..d1c902df9 100644 --- a/tests/Console/ModelsCommand/migrations/____custom_casts_table.php +++ b/tests/Console/ModelsCommand/migrations/____custom_casts_table.php @@ -9,7 +9,11 @@ class CustomCastsTable extends Migration public function up(): void { Schema::create('custom_casts', static function (Blueprint $table) { - $table->string('casted_property'); + $table->string('casted_property_with_return_type'); + $table->string('casted_property_with_return_docblock'); + $table->string('casted_property_with_return_primitive'); + $table->string('casted_property_with_return_primitive_docblock'); + $table->string('casted_property_with_return_nullable_primitive'); }); } } From ddef44cd9d7998fbb4f6d3d9535dba57f64f1024 Mon Sep 17 00:00:00 2001 From: belamov Date: Sun, 12 Apr 2020 19:11:39 +0500 Subject: [PATCH 3/4] added test case --- .../Casts/CustomCasterWithoutReturnType.php | 21 +++++++++++++++++++ .../LaravelCustomCasts/Models/CustomCast.php | 2 ++ .../ModelsCommand/LaravelCustomCasts/Test.php | 4 ++++ .../migrations/____custom_casts_table.php | 1 + 4 files changed, 28 insertions(+) create mode 100644 tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CustomCasterWithoutReturnType.php diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CustomCasterWithoutReturnType.php b/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CustomCasterWithoutReturnType.php new file mode 100644 index 000000000..5d8d22875 --- /dev/null +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/Casts/CustomCasterWithoutReturnType.php @@ -0,0 +1,21 @@ + CustomCasterWithPrimitiveReturn::class, 'casted_property_with_return_primitive_docblock' => CustomCasterWithPrimitiveDocblockReturn::class, 'casted_property_with_return_nullable_primitive' => CustomCasterWithNullablePrimitiveReturn::class, + 'casted_property_without_return' => CustomCasterWithoutReturnType::class, ]; } diff --git a/tests/Console/ModelsCommand/LaravelCustomCasts/Test.php b/tests/Console/ModelsCommand/LaravelCustomCasts/Test.php index 115be13bc..1ebacf917 100644 --- a/tests/Console/ModelsCommand/LaravelCustomCasts/Test.php +++ b/tests/Console/ModelsCommand/LaravelCustomCasts/Test.php @@ -68,6 +68,7 @@ public function test_it_parses_casted_properties_correctly(): void use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithDocblockReturn; use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithNullablePrimitiveReturn; +use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Casts\CustomCasterWithoutReturnType; 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; @@ -81,6 +82,7 @@ public function test_it_parses_casted_properties_correctly(): void * @property array $casted_property_with_return_primitive * @property array|null $casted_property_with_return_primitive_docblock * @property array|null $casted_property_with_return_nullable_primitive + * @property $casted_property_without_return * @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Models\CustomCast newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Models\CustomCast newQuery() * @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Models\CustomCast query() @@ -89,6 +91,7 @@ public function test_it_parses_casted_properties_correctly(): void * @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Models\CustomCast whereCastedPropertyWithReturnPrimitive($value) * @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Models\CustomCast whereCastedPropertyWithReturnPrimitiveDocblock($value) * @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Models\CustomCast whereCastedPropertyWithReturnType($value) + * @method static \Illuminate\Database\Eloquent\Builder|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\LaravelCustomCasts\Models\CustomCast whereCastedPropertyWithoutReturn($value) * @mixin \Eloquent */ class CustomCast extends Model @@ -99,6 +102,7 @@ class CustomCast extends Model 'casted_property_with_return_primitive' => CustomCasterWithPrimitiveReturn::class, 'casted_property_with_return_primitive_docblock' => CustomCasterWithPrimitiveDocblockReturn::class, 'casted_property_with_return_nullable_primitive' => CustomCasterWithNullablePrimitiveReturn::class, + 'casted_property_without_return' => CustomCasterWithoutReturnType::class, ]; } diff --git a/tests/Console/ModelsCommand/migrations/____custom_casts_table.php b/tests/Console/ModelsCommand/migrations/____custom_casts_table.php index d1c902df9..173b765d9 100644 --- a/tests/Console/ModelsCommand/migrations/____custom_casts_table.php +++ b/tests/Console/ModelsCommand/migrations/____custom_casts_table.php @@ -14,6 +14,7 @@ public function up(): void $table->string('casted_property_with_return_primitive'); $table->string('casted_property_with_return_primitive_docblock'); $table->string('casted_property_with_return_nullable_primitive'); + $table->string('casted_property_without_return'); }); } } From 08182bb3d45014378175a01580515df3fae98820 Mon Sep 17 00:00:00 2001 From: belamov Date: Wed, 22 Apr 2020 05:56:02 +0500 Subject: [PATCH 4/4] resolved method dublication --- src/Console/ModelsCommand.php | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 574526d21..7ebe99c8a 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -16,7 +16,6 @@ use Barryvdh\Reflection\DocBlock\Tag; use Composer\Autoload\ClassMapGenerator; use Illuminate\Console\Command; -use Illuminate\Database\Eloquent\Collection; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Filesystem\Filesystem; @@ -946,6 +945,11 @@ private function getClassKeyword(ReflectionClass $reflection) return $keyword; } + /** + * @param string $type + * @return string|null + * @throws \ReflectionException + */ protected function checkForCustomLaravelCasts(string $type): ?string { if (!class_exists($type)) { @@ -960,27 +964,10 @@ protected function checkForCustomLaravelCasts(string $type): ?string $methodReflection = new \ReflectionMethod($type, 'get'); - return $this->getReturnTypeFromReflection($methodReflection); - } - - protected function getReturnTypeFromReflection(\ReflectionMethod $reflection): ?string - { - $returnType = $reflection->getReturnType(); - - if (!$returnType) { - return $this->getReturnTypeFromDocBlock($reflection); - } - - $type = $returnType instanceof \ReflectionNamedType - ? $returnType->getName() - : (string)$returnType; - - if (!$returnType->isBuiltin()) { - $type = '\\' . $type; - } + $type = $this->getReturnTypeFromReflection($methodReflection); - if ($returnType->allowsNull()) { - $type .= '|null'; + if ($type === null) { + $type = $this->getReturnTypeFromDocBlock($methodReflection); } return $type;