diff --git a/CHANGELOG.md b/CHANGELOG.md index 70ed21894..17ce18c13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. ### Added - Add support for `immutable_date:*` and `immutable_datetime:*` casts. [#1380 / thekonz](https://github.com/barryvdh/laravel-ide-helper/pull/1380) +- Add support for attribute accessors without return type declared in closures. [#1439 / EmanueleCoppola](https://github.com/barryvdh/laravel-ide-helper/pull/1439) 2023-02-04, 2.13.0 ------------------ diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 9f9931f0c..5429747d7 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -1181,24 +1181,42 @@ protected function getAttributeReturnType(Model $model, \ReflectionMethod $refle $attribute = $reflectionMethod->invoke($model); return collect([ - 'get' => $attribute->get ? optional(new \ReflectionFunction($attribute->get))->getReturnType() : null, - 'set' => $attribute->set ? optional(new \ReflectionFunction($attribute->set))->getReturnType() : null, + 'get' => $attribute->get, + 'set' => $attribute->set, ]) + ->map(function ($function, $functionName) { + if (!$function) { + return null; + } + + $reflectionFunction = new \ReflectionFunction($function); + $functionReturnType = optional($reflectionFunction)->getReturnType(); + + if (!$functionReturnType) { + $functionReturnType = 'mixed'; + } + + return $functionReturnType; + }) ->filter() ->map(function ($type) { - if ($type instanceof \ReflectionUnionType) { - $types = collect($type->getTypes()) - /** @var ReflectionType $reflectionType */ - ->map(function ($reflectionType) { - return collect($this->extractReflectionTypes($reflectionType)); - }) - ->flatten(); + if ($type === 'mixed') { + $types = collect(['mixed']); } else { - $types = collect($this->extractReflectionTypes($type)); - } + if ($type instanceof \ReflectionUnionType) { + $types = collect($type->getTypes()) + /** @var ReflectionType $reflectionType */ + ->map(function ($reflectionType) { + return collect($this->extractReflectionTypes($reflectionType)); + }) + ->flatten(); + } else { + $types = collect($this->extractReflectionTypes($type)); + } - if ($type->allowsNull()) { - $types->push('null'); + if ($type->allowsNull()) { + $types->push('null'); + } } return $types->join('|'); diff --git a/tests/Console/ModelsCommand/Attributes/Models/Simple.php b/tests/Console/ModelsCommand/Attributes/Models/Simple.php index f64861b44..c9e60d924 100644 --- a/tests/Console/ModelsCommand/Attributes/Models/Simple.php +++ b/tests/Console/ModelsCommand/Attributes/Models/Simple.php @@ -21,6 +21,18 @@ function (?string $name): ?string { ); } + protected function attributeWithoutReturnTypes(): Attribute + { + return new Attribute( + function (?string $name) { + return $name; + }, + function (?string $name) { + return $name === null ? null : ucfirst($name); + } + ); + } + /** * ide-helper does not recognize this method being an Attribute * because the method has no actual return type; diff --git a/tests/Console/ModelsCommand/Attributes/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Attributes/__snapshots__/Test__test__1.php index df6863950..a2df4533f 100644 --- a/tests/Console/ModelsCommand/Attributes/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Attributes/__snapshots__/Test__test__1.php @@ -11,6 +11,7 @@ * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Attributes\Models\Simple * * @property integer $id + * @property mixed $attribute_without_return_types * @property string|null $name * @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery() @@ -32,6 +33,18 @@ function (?string $name): ?string { ); } + protected function attributeWithoutReturnTypes(): Attribute + { + return new Attribute( + function (?string $name) { + return $name; + }, + function (?string $name) { + return $name === null ? null : ucfirst($name); + } + ); + } + /** * ide-helper does not recognize this method being an Attribute * because the method has no actual return type;