larastan: Literal string is not a `non-empty-string`
- Larastan Version: 1.0.0
--levelused: 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';
}

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)
Fix released in: https://github.com/nunomaduro/larastan/releases/tag/1.0.1
@mfn That doesn’t look anything specific to Larastan. Try to reproduce it on phpstan.org maybe.
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
Hi,
The issue is because
getRouteKeyNamehas return type hintstringbutwhereInexpectsnon-empty-stringIt is not about literal strings.A bug can happen. It’s highly unlikely but
getRouteKeyNamecan return empty string ('') for some reason. This is here to prevent that.We can solve it by also stubbing the
getRouteKeyNamebut 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.