Laravel-ide-helper: Call to a member function getMethods() on null

Created on 24 Oct 2020  ยท  4Comments  ยท  Source: barryvdh/laravel-ide-helper

Versions:

  • ide-helper Version: dev-master (2.8.1)
  • Laravel Version: 8.9.0
  • PHP Version: 7.4.8

Description:

I recently try to run the command below and I got this error:

$ artisan ide-helper:generate                                             

   Error 

  Call to a member function getMethods() on null

  at vendor/barryvdh/laravel-ide-helper/src/Macro.php:64
     60โ–•     {
     61โ–•         $enclosingClass = $this->method->getClosureScopeClass();
     62โ–• 
     63โ–•         /** @var \ReflectionMethod $enclosingMethod */
  โžœ  64โ–•         $enclosingMethod = Collection::make($enclosingClass->getMethods())
     65โ–•             ->first(function (\ReflectionMethod $method) {
     66โ–•                 return $method->getStartLine() <= $this->method->getStartLine()
     67โ–•                     && $method->getEndLine() >= $this->method->getEndLine();
     68โ–•             });

      +32 vendor frames 
  33  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

Steps To Reproduce:

bug

Most helpful comment

This seems to be caused by invokable classes. This how to reproduce this issue:

Versions:

  • laravel/framework 8.18.1
  • barryvdh/laravel-ide-helper 2.8.2
  • PHP 7.4.10

Step 1.

Create class App\Foo:

<?php

namespace App;

class Foo
{
    public function __invoke($var)
    {
        return $var;
    }
}

Step 2.

Register the macro by passing a new instance of App\Foo:

<?php

namespace App\Providers;

use App\Foo;
// ...

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        UrlGenerator::macro('foo', new Foo());
    }

    // ...
}

Step 3.

Run php artisan ide-helper:generate.

Result

   Error

  Call to a member function getMethods() on null

  at vendor/barryvdh/laravel-ide-helper/src/Macro.php:64
     60โ–•     {
     61โ–•         $enclosingClass = $this->method->getClosureScopeClass();
     62โ–•
     63โ–•         /** @var \ReflectionMethod $enclosingMethod */
  โžœ  64โ–•         $enclosingMethod = Collection::make($enclosingClass->getMethods())
     65โ–•             ->first(function (\ReflectionMethod $method) {
     66โ–•                 return $method->getStartLine() <= $this->method->getStartLine()
     67โ–•                     && $method->getEndLine() >= $this->method->getEndLine();
     68โ–•             });

      +30 vendor frames
  31  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

Logging the value of $this->method at src/Macro.php:62 results in the following log entry:

[2020-12-10 10:26:38] local.DEBUG: Closure [ <user> public method Illuminate\Foundation\Providers\{closure} ] {
  @@ /Users/standaniels/Desktop/temp/vendor/laravel/framework/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php 83 - 85
}

[2020-12-10 10:26:38] local.DEBUG: Method [ <user> public method __invoke ] {
  @@ /Users/standaniels/Desktop/temp/app/Foo.php 7 - 10

  - Parameters [1] {
    Parameter #0 [ <required> $string ]
  }
}

All 4 comments

Can you debug which method this is about?

Also maybe the code is simply buggy as getClosureScopeClass can return null, but nevertheless would be good to understand what specifically triggers this.

This seems to be caused by invokable classes. This how to reproduce this issue:

Versions:

  • laravel/framework 8.18.1
  • barryvdh/laravel-ide-helper 2.8.2
  • PHP 7.4.10

Step 1.

Create class App\Foo:

<?php

namespace App;

class Foo
{
    public function __invoke($var)
    {
        return $var;
    }
}

Step 2.

Register the macro by passing a new instance of App\Foo:

<?php

namespace App\Providers;

use App\Foo;
// ...

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        UrlGenerator::macro('foo', new Foo());
    }

    // ...
}

Step 3.

Run php artisan ide-helper:generate.

Result

   Error

  Call to a member function getMethods() on null

  at vendor/barryvdh/laravel-ide-helper/src/Macro.php:64
     60โ–•     {
     61โ–•         $enclosingClass = $this->method->getClosureScopeClass();
     62โ–•
     63โ–•         /** @var \ReflectionMethod $enclosingMethod */
  โžœ  64โ–•         $enclosingMethod = Collection::make($enclosingClass->getMethods())
     65โ–•             ->first(function (\ReflectionMethod $method) {
     66โ–•                 return $method->getStartLine() <= $this->method->getStartLine()
     67โ–•                     && $method->getEndLine() >= $this->method->getEndLine();
     68โ–•             });

      +30 vendor frames
  31  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

Logging the value of $this->method at src/Macro.php:62 results in the following log entry:

[2020-12-10 10:26:38] local.DEBUG: Closure [ <user> public method Illuminate\Foundation\Providers\{closure} ] {
  @@ /Users/standaniels/Desktop/temp/vendor/laravel/framework/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php 83 - 85
}

[2020-12-10 10:26:38] local.DEBUG: Method [ <user> public method __invoke ] {
  @@ /Users/standaniels/Desktop/temp/app/Foo.php 7 - 10

  - Parameters [1] {
    Parameter #0 [ <required> $string ]
  }
}

I don't use these kind of things ๐Ÿคทโ€โ™€๏ธ

Can you propose a PR / test to fix this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

beniaminorossini picture beniaminorossini  ยท  5Comments

ghost picture ghost  ยท  5Comments

anik786 picture anik786  ยท  3Comments

kickthemooon picture kickthemooon  ยท  3Comments

quantumwebco picture quantumwebco  ยท  4Comments