From c32fe5804c08992abe06ff7144de68707217dd01 Mon Sep 17 00:00:00 2001 From: Luuk de Weijer Date: Thu, 2 May 2024 17:29:55 +0200 Subject: [PATCH] Implemented correct attribute handling --- src/Console/ModelsCommand.php | 16 ++-- .../Attributes/Models/BackedAttribute.php | 59 ++++++++++++++ .../__snapshots__/Test__test__1.php | 77 +++++++++++++++++++ .../migrations/____backed_attribute_table.php | 20 +++++ 4 files changed, 162 insertions(+), 10 deletions(-) create mode 100644 tests/Console/ModelsCommand/Attributes/Models/BackedAttribute.php create mode 100644 tests/Console/ModelsCommand/migrations/____backed_attribute_table.php diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 4ec323a62..51d3c1131 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -580,10 +580,7 @@ public function getPropertiesFromMethods($model) $isAttribute = is_a($type, '\Illuminate\Database\Eloquent\Casts\Attribute', true); $method = $reflection->getName(); if ( - Str::startsWith($method, 'get') && Str::endsWith( - $method, - 'Attribute' - ) && $method !== 'getAttribute' + Str::startsWith($method, 'get') && Str::endsWith($method, 'Attribute') && $method !== 'getAttribute' ) { //Magic getAttribute $name = Str::snake(substr($method, 3, -9)); @@ -599,15 +596,14 @@ public function getPropertiesFromMethods($model) $this->setProperty( Str::snake($method), $type, - $types->has('get'), - $types->has('set'), + $types->has('get') ?: null, + $types->has('set') ?: null, $this->getCommentFromDocBlock($reflection) ); } elseif ( - Str::startsWith($method, 'set') && Str::endsWith( - $method, - 'Attribute' - ) && $method !== 'setAttribute' + Str::startsWith($method, 'set') && + Str::endsWith($method, 'Attribute') && + $method !== 'setAttribute' ) { //Magic setAttribute $name = Str::snake(substr($method, 3, -9)); diff --git a/tests/Console/ModelsCommand/Attributes/Models/BackedAttribute.php b/tests/Console/ModelsCommand/Attributes/Models/BackedAttribute.php new file mode 100644 index 000000000..c6c8bf2b2 --- /dev/null +++ b/tests/Console/ModelsCommand/Attributes/Models/BackedAttribute.php @@ -0,0 +1,59 @@ +bigIncrements('id'); + $table->string('name'); + $table->string('name_read'); + $table->string('name_write'); + }); + } +}