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

Call to a member function getMethods() on null #1084

Closed
celorodovalho opened this issue Oct 24, 2020 · 11 comments · Fixed by #1210
Closed

Call to a member function getMethods() on null #1084

celorodovalho opened this issue Oct 24, 2020 · 11 comments · Fixed by #1210
Labels

Comments

@celorodovalho
Copy link

celorodovalho commented Oct 24, 2020

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();
     6263/** @var \ReflectionMethod $enclosingMethod */64$enclosingMethod = Collection::make($enclosingClass->getMethods())
     65▕             ->first(function (\ReflectionMethod $method) {
     66return $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:

  • artisan ide-helper:generate
@mfn
Copy link
Collaborator

mfn commented Oct 29, 2020

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.

@standaniels
Copy link
Contributor

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 ]
  }
}

@mfn
Copy link
Collaborator

mfn commented Dec 10, 2020

I don't use these kind of things 🤷‍♀️

Can you propose a PR / test to fix this?

@mfn
Copy link
Collaborator

mfn commented Jan 9, 2021

Fixed with #1124

@mfn mfn closed this as completed Jan 9, 2021
@mfn
Copy link
Collaborator

mfn commented Mar 15, 2021

Released via https://github.com/barryvdh/laravel-ide-helper/releases/tag/v2.9.1

@peterchaula
Copy link

@mfn the PR seems to have broken some of my classes. ReflectionMethod::getClosureScopeClass() returns null for non closures. See example below:

image

I'm proposing this change as a fix. I'll submit a PR if you're happy with the change

image

@mfn
Copy link
Collaborator

mfn commented May 31, 2021

But this change then will not support __invokeable classes?

Basically: yes, please make a PR but from a quick look just replacing it with isClosure might not be correct

@peterchaula
Copy link

@mfn thanks. I'll have another look at my proposed change and make changes if necessary and create the PR. Thanks.

@mfn mfn linked a pull request Jun 1, 2021 that will close this issue
6 tasks
@terry-lonesski
Copy link

terry-lonesski commented Jun 23, 2021

I still have
Call to a member function getMethods() on null
at vendor/barryvdh/laravel-ide-helper/src/Macro.php

/** @var \ReflectionMethod $enclosingMethod */
        $enclosingMethod = Collection::make($enclosingClass->getMethods())
            ->first(function (\ReflectionMethod $method) {
                return $method->getStartLine() <= $this->method->getStartLine()
                    && $method->getEndLine() >= $this->method->getEndLine();
            });

in some cases $enclosingClass is null

I have 2.10.0 version installed
Should I open new issue?

@JanMisker
Copy link
Contributor

I also still have this issue. I enabled debugging and I can see that the problem stems from some other package I installed that adds a macro on the Request.

Request::macro(...)

This causes a problem in addLocationToPhpDoc()

@JanMisker
Copy link
Contributor

I propose a quick, probably naive, fix here: #1239

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants