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

app()->make($className) returns type *NEVER* #1504

Closed
levu42 opened this issue Jan 3, 2023 · 2 comments · Fixed by #1506
Closed

app()->make($className) returns type *NEVER* #1504

levu42 opened this issue Jan 3, 2023 · 2 comments · Fixed by #1506

Comments

@levu42
Copy link

levu42 commented Jan 3, 2023

  • Larastan Version: 2.3.4
  • --level used: 8

Description

I have in my code this section:

        $model = $this->getModelClassName();
        $object = app()->make($model);

        \PHPStan\dumpType($object);

        if (! $object instanceof Model) {
            throw new ReflectionException("$model is not an instance of " . Model::class);
        }

where $this->getModelClassName() has a PHPDoc return type of class-name<Model>, and then the dumpType shows me: Dumped type: *NEVER*.

I guess, it's related to #1451 since it only happens since I upgraded from 2.3.0 -> 2.3.4.

The actual false positive error triggered in this section is by the way Unreachable statement - code above always terminates. for the code below the instanceof / throw section.

@szepeviktor
Copy link
Collaborator

@levu42
Copy link
Author

levu42 commented Jan 3, 2023

@szepeviktor Thanks for your fast reply!

In this case:

class X {
    /** @return class-string<Model> */
    private function getModelClassName(): string {...}

    private function foo(): void
    {
         $model = $this->getModelClassName();
         $object = app()->make($model);
    }

When I dump the type of $model I get Dumped type: class-string<Illuminate\Database\Eloquent\Model>.

The code you linked looks to me like the only way it returns NeverType is by $expr instanceof String_ evaluating to false. I don't know anything really about the PHPStan internals, so I have no clue how to fix that best, but from my understanding, class-string is a special case of string, so this should evaluate to true, not false.

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

Successfully merging a pull request may close this issue.

2 participants