Skip to content

Include camel case attribute version #1676

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions config/ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,19 @@
Spatie\Macroable\Macroable::class,
],

/*
|--------------------------------------------------------------------------
| Include camel case attribute version
|--------------------------------------------------------------------------
|
| Generate camel camel case version for attributes
| When model attribute is defined as either one of these
| - getSomeValueAttribute()
| - someValue(): Attribute
| include both of these in the DocBlock:
| - $some_value
| - $someValue
|
*/
'model_include_camel_case_attribute_version' => false,
];
13 changes: 13 additions & 0 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ public function getPropertiesFromMethods($model)
$type = $this->getReturnTypeFromReflection($reflection);
$isAttribute = is_a($type, '\Illuminate\Database\Eloquent\Casts\Attribute', true);
$method = $reflection->getName();
$includeCamelCaseAttributeVersion = $this->laravel['config']->get('ide-helper.model_include_camel_case_attribute_version', false);
if (
Str::startsWith($method, 'get') && Str::endsWith($method, 'Attribute') && $method !== 'getAttribute'
) {
Expand All @@ -647,6 +648,9 @@ public function getPropertiesFromMethods($model)
$type = $this->getTypeInModel($model, $type);
$comment = $this->getCommentFromDocBlock($reflection);
$this->setProperty($name, $type, true, null, $comment);
if ($includeCamelCaseAttributeVersion) {
$this->setProperty(Str::camel($name), $type, true, null, $comment);
}
}
} elseif ($isAttribute) {
$types = $this->getAttributeTypes($model, $reflection);
Expand All @@ -658,6 +662,15 @@ public function getPropertiesFromMethods($model)
$types->has('set') ?: null,
$this->getCommentFromDocBlock($reflection)
);
if ($includeCamelCaseAttributeVersion && $types->has('get')) {
$this->setProperty(
Str::camel($method),
$type,
true,
null,
$this->getCommentFromDocBlock($reflection)
);
}
} elseif (
Str::startsWith($method, 'set') &&
Str::endsWith($method, 'Attribute') &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

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

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use stdClass;

class Simple extends Model
{
public function getIntValueAttribute(): int
{
return rand();
}

public function setIntValueAttribute(int $value): int
{
return $value;
}

public function stringValue(): Attribute
{
return new Attribute(
get: fn (): string => Str::random(),
set: fn (string $value): string => $value,
);
}
public function getBoolValueAttribute(): bool
{
return (bool) rand(0, 1);
}

public function arrayValue(): Attribute
{
return new Attribute(
get: fn (): array => [],
);
}

public function setStdClassValueAttribute(stdClass $stdClass): stdClass
{
return $stdClass;
}

public function voidValue(): Attribute
{
return new Attribute(
set: function (): void {
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocModelIncludeCamelCaseAttributeVersion;

use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;

class Test extends AbstractModelsCommand
{
protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app['config']->set('ide-helper.model_include_camel_case_attribute_version', true);
}

public function test(): void
{
$command = $this->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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

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

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use stdClass;

/**
*
*
* @property int $id
* @property-read array $array_value
* @property-read array $arrayValue
* @property-read bool $bool_value
* @property-read bool $boolValue
* @property int $int_value
* @property-read int $intValue
* @property-write mixed $std_class_value
* @property string $string_value
* @property-read string $stringValue
* @property-write mixed $void_value
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple query()
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple whereId($value)
* @mixin \Eloquent
*/
class Simple extends Model
{
public function getIntValueAttribute(): int
{
return rand();
}

public function setIntValueAttribute(int $value): int
{
return $value;
}

public function stringValue(): Attribute
{
return new Attribute(
get: fn (): string => Str::random(),
set: fn (string $value): string => $value,
);
}
public function getBoolValueAttribute(): bool
{
return (bool) rand(0, 1);
}

public function arrayValue(): Attribute
{
return new Attribute(
get: fn (): array => [],
);
}

public function setStdClassValueAttribute(stdClass $stdClass): stdClass
{
return $stdClass;
}

public function voidValue(): Attribute
{
return new Attribute(
set: function (): void {
}
);
}
}
Loading