Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented correct attribute handling #5

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 get<name>Attribute
$name = Str::snake(substr($method, 3, -9));
Expand All @@ -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 set<name>Attribute
$name = Str::snake(substr($method, 3, -9));
Expand Down
59 changes: 59 additions & 0 deletions tests/Console/ModelsCommand/Attributes/Models/BackedAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Attributes\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;

class BackedAttribute extends Model
{
protected function name(): Attribute
{
return new Attribute(
function (?string $name): ?string {
return $name;
},
function (?string $name): ?string {
return $name;
}
);
}

protected function nameRead(): Attribute
{
return new Attribute(
function (?string $name): ?string {
return $name;
},
);
}

protected function nameWrite(): Attribute
{
return new Attribute(
set: function (?string $name): ?string {
return $name;
},
);
}

protected function nonBackedSet(): Attribute
{
return new Attribute(
set: function (?string $name): ?string {
return $name;
},
);
}

protected function nonBackedGet(): Attribute
{
return new Attribute(
get: function (): ?string {
return 'test';
},
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,83 @@
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;

/**
*
*
* @property int $id
* @property string|null $name
* @property string|null $name_read
* @property string|null $name_write
* @property-read string|null $non_backed_get
* @property-write string|null $non_backed_set
* @method static \Illuminate\Database\Eloquent\Builder|BackedAttribute newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|BackedAttribute newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|BackedAttribute query()
* @method static \Illuminate\Database\Eloquent\Builder|BackedAttribute whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|BackedAttribute whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|BackedAttribute whereNameRead($value)
* @method static \Illuminate\Database\Eloquent\Builder|BackedAttribute whereNameWrite($value)
* @mixin \Eloquent
*/
class BackedAttribute extends Model
{
protected function name(): Attribute
{
return new Attribute(
function (?string $name): ?string {
return $name;
},
function (?string $name): ?string {
return $name;
}
);
}

protected function nameRead(): Attribute
{
return new Attribute(
function (?string $name): ?string {
return $name;
},
);
}

protected function nameWrite(): Attribute
{
return new Attribute(
set: function (?string $name): ?string {
return $name;
},
);
}

protected function nonBackedSet(): Attribute
{
return new Attribute(
set: function (?string $name): ?string {
return $name;
},
);
}

protected function nonBackedGet(): Attribute
{
return new Attribute(
get: function (): ?string {
return 'test';
},
);
}
}
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Attributes\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;

/**
*
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class BackedAttributeTable extends Migration
{
public function up(): void
{
Schema::create('backed_attributes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('name_read');
$table->string('name_write');
});
}
}