laravel-ide-helper
laravel-ide-helper copied to clipboard
Union return type with macro fails with "Call to undefined method ReflectionUnionType::getName()"
Versions:
- ide-helper Version: 2.10.0
- Laravel Version: 8.53.1
- PHP Version: 8.0.9
Description:
In my AppServiceProvider.php
I have added a macro for the Stringable
class in Laravel:
Stringable::macro('fromJson', fn(bool $assoc = false): array|object => json_decode($this->value, $assoc));
THe array|object
part there causes php artisan ide-helper:generate
to fail with the following error:
Call to undefined method ReflectionUnionType::getName()
at vendor/barryvdh/laravel-ide-helper/src/Macro.php:60
56▕ if ($method->hasReturnType() && !$this->phpdoc->hasTag('return')) {
57▕ $builder = EloquentBuilder::class;
58▕ $return = $method->getReturnType();
59▕
➜ 60▕ $type = $return->getName();
61▕ $type .= $this->root === "\\{$builder}" && $return->getName() === $builder ? '|static' : '';
62▕ $type .= $return->allowsNull() ? '|null' : '';
63▕
64▕ $this->phpdoc->appendTag(Tag::createInstance("@return {$type}"));
+29 vendor frames
30 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
I have also tested to change fn()
to function()
like so:
Stringable::macro('fromJson', function(bool $assoc = false): array|object {
return json_decode($this->value, $assoc);
});
But to no avail I'm afraid. If the return type is only array
or object
it works.
I think this was fixed with https://github.com/barryvdh/laravel-ide-helper/pull/1148 but there's no new release yet; can you check with 2.9-dev
if it works for you?
@mfn Yep, it seems to work well with dev-master
:+1:
Have the same issue and can confirm using dev-master fixed my issue as well!
Setting a dep on dev-master
is far from ideal. @barryvdh Can we please get a new release/tag?
New releases where made -> closing