laravel-ide-helper icon indicating copy to clipboard operation
laravel-ide-helper copied to clipboard

Union return type with macro fails with "Call to undefined method ReflectionUnionType::getName()"

Open carestad opened this issue 2 years ago • 4 comments

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.

carestad avatar Aug 09 '21 12:08 carestad

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 avatar Aug 09 '21 20:08 mfn

@mfn Yep, it seems to work well with dev-master :+1:

carestad avatar Aug 10 '21 10:08 carestad

Have the same issue and can confirm using dev-master fixed my issue as well!

Ilyes512 avatar Aug 17 '21 11:08 Ilyes512

Setting a dep on dev-master is far from ideal. @barryvdh Can we please get a new release/tag?

Ilyes512 avatar Dec 02 '21 18:12 Ilyes512

New releases where made -> closing

mfn avatar Feb 16 '24 22:02 mfn