diff --git a/CHANGELOG.md b/CHANGELOG.md index 143651e..c95cf49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file. [Next release](https://github.com/barryvdh/laravel-ide-helper/compare/v2.10.0...master) -------------- +### Added +- Add support for cast types `decimal:*`, `encrypted:*`, `immutable_date`, `immutable_datetime`, `custom_datetime`, and `immutable_custom_datetime` [#1262 / miken32](https://github.com/barryvdh/laravel-ide-helper/pull/1262) + ### Fixed - Fix recursively searching for `HasFactory` and `Macroable` traits [\#1216 / daniel-de-wit](https://github.com/barryvdh/laravel-ide-helper/pull/1216) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index fd2997b..c3c08c4 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -355,11 +355,24 @@ class ModelsCommand extends Command { $casts = $model->getCasts(); foreach ($casts as $name => $type) { + if (Str::startsWith($type, 'decimal:')) { + $type = 'decimal'; + } elseif (Str::startsWith($type, 'custom_datetime:')) { + $type = 'date'; + } elseif (Str::startsWith($type, 'immutable_custom_datetime:')) { + $type = 'immutable_date'; + } elseif (Str::startsWith($type, 'encrypted:')) { + $type = Str::after($type, ':'); + } switch ($type) { + case 'encrypted': + $realType = 'mixed'; + break; case 'boolean': case 'bool': $realType = 'boolean'; break; + case 'decimal': case 'string': $realType = 'string'; break; @@ -384,6 +397,10 @@ class ModelsCommand extends Command case 'datetime': $realType = $this->dateClass; break; + case 'immutable_date': + case 'immutable_datetime': + $realType = '\Carbon\CarbonImmutable'; + break; case 'collection': $realType = '\Illuminate\Support\Collection'; break; diff --git a/tests/Console/ModelsCommand/SimpleCasts/Models/SimpleCast.php b/tests/Console/ModelsCommand/SimpleCasts/Models/SimpleCast.php new file mode 100644 index 0000000..5d8e3dc --- /dev/null +++ b/tests/Console/ModelsCommand/SimpleCasts/Models/SimpleCast.php @@ -0,0 +1,38 @@ + 'int', + 'cast_to_integer' => 'integer', + 'cast_to_real' => 'real', + 'cast_to_float' => 'float', + 'cast_to_double' => 'double', + 'cast_to_decimal' => 'decimal:4', + 'cast_to_string' => 'string', + 'cast_to_bool' => 'bool', + 'cast_to_boolean' => 'boolean', + 'cast_to_object' => 'object', + 'cast_to_array' => 'array', + 'cast_to_json' => 'json', + 'cast_to_collection' => 'collection', + 'cast_to_date' => 'date', + 'cast_to_datetime' => 'datetime', + 'cast_to_custom_datetime' => 'custom_datetime:Y-m-d H:i:s', + 'cast_to_immutable_date' => 'immutable_date', + 'cast_to_immutable_custom_datetime' => 'immutable_custom_datetime:Y-m-d H:i:s', + 'cast_to_immutable_datetime' => 'immutable_datetime', + 'cast_to_timestamp' => 'timestamp', + 'cast_to_encrypted' => 'encrypted', + 'cast_to_encrypted_array' => 'encrypted:array', + 'cast_to_encrypted_collection' => 'encrypted:collection', + 'cast_to_encrypted_json' => 'encrypted:json', + 'cast_to_encrypted_object' => 'encrypted:object', + ]; +} diff --git a/tests/Console/ModelsCommand/SimpleCasts/Test.php b/tests/Console/ModelsCommand/SimpleCasts/Test.php new file mode 100644 index 0000000..05cb826 --- /dev/null +++ b/tests/Console/ModelsCommand/SimpleCasts/Test.php @@ -0,0 +1,24 @@ +app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, [ + '--write' => true, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay()); + $this->assertMatchesMockedSnapshot(); + } +} diff --git a/tests/Console/ModelsCommand/SimpleCasts/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/SimpleCasts/__snapshots__/Test__test__1.php new file mode 100644 index 0000000..acb861f --- /dev/null +++ b/tests/Console/ModelsCommand/SimpleCasts/__snapshots__/Test__test__1.php @@ -0,0 +1,96 @@ + 'int', + 'cast_to_integer' => 'integer', + 'cast_to_real' => 'real', + 'cast_to_float' => 'float', + 'cast_to_double' => 'double', + 'cast_to_decimal' => 'decimal:4', + 'cast_to_string' => 'string', + 'cast_to_bool' => 'bool', + 'cast_to_boolean' => 'boolean', + 'cast_to_object' => 'object', + 'cast_to_array' => 'array', + 'cast_to_json' => 'json', + 'cast_to_collection' => 'collection', + 'cast_to_date' => 'date', + 'cast_to_datetime' => 'datetime', + 'cast_to_custom_datetime' => 'custom_datetime:Y-m-d H:i:s', + 'cast_to_immutable_date' => 'immutable_date', + 'cast_to_immutable_custom_datetime' => 'immutable_custom_datetime:Y-m-d H:i:s', + 'cast_to_immutable_datetime' => 'immutable_datetime', + 'cast_to_timestamp' => 'timestamp', + 'cast_to_encrypted' => 'encrypted', + 'cast_to_encrypted_array' => 'encrypted:array', + 'cast_to_encrypted_collection' => 'encrypted:collection', + 'cast_to_encrypted_json' => 'encrypted:json', + 'cast_to_encrypted_object' => 'encrypted:object', + ]; +} diff --git a/tests/Console/ModelsCommand/migrations/____simple_casts_table.php b/tests/Console/ModelsCommand/migrations/____simple_casts_table.php new file mode 100644 index 0000000..76b8f3a --- /dev/null +++ b/tests/Console/ModelsCommand/migrations/____simple_casts_table.php @@ -0,0 +1,41 @@ +string('cast_to_int'); + $table->string('cast_to_integer'); + $table->string('cast_to_real'); + $table->string('cast_to_float'); + $table->string('cast_to_double'); + $table->string('cast_to_decimal'); + $table->string('cast_to_string'); + $table->string('cast_to_bool'); + $table->string('cast_to_boolean'); + $table->string('cast_to_object'); + $table->string('cast_to_array'); + $table->string('cast_to_json'); + $table->string('cast_to_collection'); + $table->string('cast_to_date'); + $table->string('cast_to_datetime'); + $table->string('cast_to_custom_datetime'); + $table->string('cast_to_immutable_date'); + $table->string('cast_to_immutable_custom_datetime'); + $table->string('cast_to_immutable_datetime'); + $table->string('cast_to_timestamp'); + $table->string('cast_to_encrypted'); + $table->string('cast_to_encrypted_array'); + $table->string('cast_to_encrypted_collection'); + $table->string('cast_to_encrypted_json'); + $table->string('cast_to_encrypted_object'); + }); + } +}