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

Add support for attribute accessors without return type declared in closures #1439

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------
Expand Down
44 changes: 31 additions & 13 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('|');
Expand Down
12 changes: 12 additions & 0 deletions tests/Console/ModelsCommand/Attributes/Models/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Attributes\Models\Simple
*
* @property integer $id
* @property mixed $attribute_without_return_types
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was returned before?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before it didn't even show the attribute

* @property string|null $name
* @method static \Illuminate\Database\Eloquent\Builder|Simple newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Simple newQuery()
Expand All @@ -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;
Expand Down