larastan: Literal string is not a `non-empty-string`

  • Larastan Version: 1.0.0
  • --level used: 7
  • Pull request with failing test:

Description

non-empty-string appeared as errors even no actually bugs happen

Laravel code where the issue was found


#  381
        $products = Product::whereIn((new Product())->getRouteKeyName(), $values)
            ->pluck('title', (new Product())->getRouteKeyName())
            ->toArray();

# 417
        $users = User::whereIn((new User())->getRouteKeyName(), $values)
            ->get()
            ->pluck('full_name', (new User())->getRouteKeyName())
            ->toArray();

# 610
       $products = Product::whereIn((new Product())->getRouteKeyName(), $this->freeProducts)->get();

# 627
       ? User::whereIn(
                (new User())->getRouteKeyName(),
                $this->specificCustomers
            )->get()->all()
            : [];

# models
    public function getRouteKeyName(): string
    {
        return 'slug';
    }

Screenshot from 2021-11-05 11-44-09

I try to search the solution but it was not found any

thank you

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 28 (7 by maintainers)

Most upvoted comments

@mfn That doesn’t look anything specific to Larastan. Try to reproduce it on phpstan.org maybe.

Code also expects PK, so probably @return non-empty-string can be added.

PHPStan will alert if you accidentally set an empty string https://phpstan.org/r/3ef8d187-62e2-43c0-854d-ef0432260bd0

No 😦 https://github.com/phpstan/phpstan/issues/3770#issuecomment-793564311 You can do it inside the closure

/** @var non-empty-string $direction */

Hi,

The issue is because getRouteKeyName has return type hint string but whereIn expects non-empty-string It is not about literal strings.

even no actually bugs happen

A bug can happen. It’s highly unlikely but getRouteKeyName can return empty string ('') for some reason. This is here to prevent that.

We can solve it by also stubbing the getRouteKeyName but now I also think the benefits are not that much.

@nunomaduro We can revert that commit I think.

Running into this as well, seems to be fixed in phpstan v1.0.2: https://github.com/phpstan/phpstan-src/commit/1283db7ae426142af99b41a480c964fb8a2c8c83

cc @canvural This is the issue we have discussed.