diff --git a/config/ide-helper.php b/config/ide-helper.php index 1500783aa..f67614828 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -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, ]; diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 395cf9d79..c19a081f2 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -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' ) { @@ -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); @@ -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') && diff --git a/tests/Console/ModelsCommand/GeneratePhpdocModelIncludeCamelCaseAttributeVersion/Models/Simple.php b/tests/Console/ModelsCommand/GeneratePhpdocModelIncludeCamelCaseAttributeVersion/Models/Simple.php new file mode 100644 index 000000000..31940010d --- /dev/null +++ b/tests/Console/ModelsCommand/GeneratePhpdocModelIncludeCamelCaseAttributeVersion/Models/Simple.php @@ -0,0 +1,55 @@ + 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 { + } + ); + } +} diff --git a/tests/Console/ModelsCommand/GeneratePhpdocModelIncludeCamelCaseAttributeVersion/Test.php b/tests/Console/ModelsCommand/GeneratePhpdocModelIncludeCamelCaseAttributeVersion/Test.php new file mode 100644 index 000000000..e15ed4ab1 --- /dev/null +++ b/tests/Console/ModelsCommand/GeneratePhpdocModelIncludeCamelCaseAttributeVersion/Test.php @@ -0,0 +1,31 @@ +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(); + } +} diff --git a/tests/Console/ModelsCommand/GeneratePhpdocModelIncludeCamelCaseAttributeVersion/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/GeneratePhpdocModelIncludeCamelCaseAttributeVersion/__snapshots__/Test__test__1.php new file mode 100644 index 000000000..35272a28a --- /dev/null +++ b/tests/Console/ModelsCommand/GeneratePhpdocModelIncludeCamelCaseAttributeVersion/__snapshots__/Test__test__1.php @@ -0,0 +1,75 @@ +|Simple newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|Simple query() + * @method static \Illuminate\Database\Eloquent\Builder|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 { + } + ); + } +}