Skip to content

Commit bcbd6a5

Browse files
committed
include camel case attribute version
1 parent 8d441ec commit bcbd6a5

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed

config/ide-helper.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,19 @@
349349
Spatie\Macroable\Macroable::class,
350350
],
351351

352+
/*
353+
|--------------------------------------------------------------------------
354+
| Include camel case attribute version
355+
|--------------------------------------------------------------------------
356+
|
357+
| Generate camel camel case version for attributes
358+
| When model attribute is defined as either one of these
359+
| - getSomeValueAttribute()
360+
| - someValue(): Attribute
361+
| include both of these in the DocBlock:
362+
| - $some_value
363+
| - $someValue
364+
|
365+
*/
366+
'model_include_camel_case_attribute_version' => false,
352367
];

src/Console/ModelsCommand.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ public function getPropertiesFromMethods($model)
637637
$type = $this->getReturnTypeFromReflection($reflection);
638638
$isAttribute = is_a($type, '\Illuminate\Database\Eloquent\Casts\Attribute', true);
639639
$method = $reflection->getName();
640+
$includeCamelCaseAttributeVersion = $this->laravel['config']->get('ide-helper.model_include_camel_case_attribute_version', false);
640641
if (
641642
Str::startsWith($method, 'get') && Str::endsWith($method, 'Attribute') && $method !== 'getAttribute'
642643
) {
@@ -647,6 +648,9 @@ public function getPropertiesFromMethods($model)
647648
$type = $this->getTypeInModel($model, $type);
648649
$comment = $this->getCommentFromDocBlock($reflection);
649650
$this->setProperty($name, $type, true, null, $comment);
651+
if ($includeCamelCaseAttributeVersion) {
652+
$this->setProperty(Str::camel($name), $type, true, null, $comment);
653+
}
650654
}
651655
} elseif ($isAttribute) {
652656
$types = $this->getAttributeTypes($model, $reflection);
@@ -658,6 +662,15 @@ public function getPropertiesFromMethods($model)
658662
$types->has('set') ?: null,
659663
$this->getCommentFromDocBlock($reflection)
660664
);
665+
if ($includeCamelCaseAttributeVersion && $types->has('get')) {
666+
$this->setProperty(
667+
Str::camel($method),
668+
$type,
669+
true,
670+
null,
671+
$this->getCommentFromDocBlock($reflection)
672+
);
673+
}
661674
} elseif (
662675
Str::startsWith($method, 'set') &&
663676
Str::endsWith($method, 'Attribute') &&
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocModelIncludeCamelCaseAttributeVersion\Models;
6+
7+
use Illuminate\Database\Eloquent\Casts\Attribute;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Support\Str;
10+
use stdClass;
11+
12+
class Simple extends Model
13+
{
14+
public function getIntValueAttribute(): int
15+
{
16+
return rand();
17+
}
18+
19+
public function setIntValueAttribute(int $value): int
20+
{
21+
return $value;
22+
}
23+
24+
public function stringValue(): Attribute
25+
{
26+
return new Attribute(
27+
get: fn (): string => Str::random(),
28+
set: fn (string $value): string => $value,
29+
);
30+
}
31+
public function getBoolValueAttribute(): bool
32+
{
33+
return (bool) rand(0,1);
34+
}
35+
36+
public function arrayValue(): Attribute
37+
{
38+
return new Attribute(
39+
get: fn (): array => [],
40+
);
41+
}
42+
43+
public function setStdClassValueAttribute(stdClass $stdClass): stdClass
44+
{
45+
return $stdClass;
46+
}
47+
48+
public function voidValue(): Attribute
49+
{
50+
return new Attribute(
51+
set: function (): void {}
52+
);
53+
}
54+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocModelIncludeCamelCaseAttributeVersion;
6+
7+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
8+
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AbstractModelsCommand;
9+
10+
class Test extends AbstractModelsCommand
11+
{
12+
protected function getEnvironmentSetUp($app)
13+
{
14+
parent::getEnvironmentSetUp($app);
15+
16+
$app['config']->set('ide-helper.model_include_camel_case_attribute_version', true);
17+
}
18+
19+
public function test(): void
20+
{
21+
$command = $this->app->make(ModelsCommand::class);
22+
23+
$tester = $this->runCommand($command, [
24+
'--write' => true,
25+
]);
26+
27+
$this->assertSame(0, $tester->getStatusCode());
28+
$this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay());
29+
$this->assertMatchesMockedSnapshot();
30+
}
31+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\GeneratePhpdocModelIncludeCamelCaseAttributeVersion\Models;
6+
7+
use Illuminate\Database\Eloquent\Casts\Attribute;
8+
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Support\Str;
10+
use stdClass;
11+
12+
/**
13+
*
14+
*
15+
* @property int $id
16+
* @property-read array $array_value
17+
* @property-read array $arrayValue
18+
* @property-read bool $bool_value
19+
* @property-read bool $boolValue
20+
* @property int $int_value
21+
* @property-read int $intValue
22+
* @property-write mixed $std_class_value
23+
* @property string $string_value
24+
* @property-read string $stringValue
25+
* @property-write mixed $void_value
26+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newModelQuery()
27+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple newQuery()
28+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple query()
29+
* @method static \Illuminate\Database\Eloquent\Builder<static>|Simple whereId($value)
30+
* @mixin \Eloquent
31+
*/
32+
class Simple extends Model
33+
{
34+
public function getIntValueAttribute(): int
35+
{
36+
return rand();
37+
}
38+
39+
public function setIntValueAttribute(int $value): int
40+
{
41+
return $value;
42+
}
43+
44+
public function stringValue(): Attribute
45+
{
46+
return new Attribute(
47+
get: fn (): string => Str::random(),
48+
set: fn (string $value): string => $value,
49+
);
50+
}
51+
public function getBoolValueAttribute(): bool
52+
{
53+
return (bool) rand(0,1);
54+
}
55+
56+
public function arrayValue(): Attribute
57+
{
58+
return new Attribute(
59+
get: fn (): array => [],
60+
);
61+
}
62+
63+
public function setStdClassValueAttribute(stdClass $stdClass): stdClass
64+
{
65+
return $stdClass;
66+
}
67+
68+
public function voidValue(): Attribute
69+
{
70+
return new Attribute(
71+
set: function (): void {}
72+
);
73+
}
74+
}

0 commit comments

Comments
 (0)