--level used: 8Larastan does not detect a nested SoftDeletes trait after updating from 0.7.4 to 0.7.5 or higher.
class User extends Authenticatable
{
public function group(): BelongsTo
{
return $this->belongsTo(Group::class)->withTrashed(); // Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\BelongsTo<App\\Group, App\\User>::withoutTrashed().
}
}
class Group extends Model
{
use NestedSoftDeletes;
}
trait NestedSoftDeletes
{
use SoftDeletes;
}
Hi,
We are using the PHPStan's MethodReflection method hasTraitUse here to determine if a class uses the given trait. Looks like this needs to recursive. But I don't know if it's possible.
Please feel free to create a PR that fixes it in the code I pointed out. Also you can ask in PHPStan if you need help with how to solve this.
@ondrejmirtes Could you help us whether hasTraitUse is recursive?
Try using ClassReflection::getTraits() instead, or send a fix to PHPStan :)
@canvural I've added a function in #838 that collects nested traits as the PHPStan's collectTraits function is private. Let me know if this is acceptable.
@IAmRGroot Did you try ClassReflection::getTraits() first as @ondrejmirtes suggested?
@canvural Yes!
ClassReflection::getTraits gives App\Group: {"App\\Traits\\NestedSoftDeletes":{}}
(new) collectTraitNames gives App\Group: ["App\\Traits\\NestedSoftDeletes","Illuminate\\Database\\Eloquent\\SoftDeletes"]
See the ClassReflection::collectTraits private function. One could send a PR to make it public, but until then you would need to use your own function.
I'm not sure if it's the best thing to duplicate collectTraits method in Larastan. @ondrejmirtes ClassReflection::getTraits() does not look like it's recursive, as @IAmRGroot pointed out. Would you be fine with a PR that is making collectTraits public, or making hasTraitUse use collectTraits, or do you have any other idea for a solution?
I'm fine with changing getTraits() to be recursive, as getInterfaces already are...
@IAmRGroot You can send a PR to PHPStan repo first that changes getTraits to be recursive. Then we can use it here, to solve the issue.
Most helpful comment
@IAmRGroot You can send a PR to PHPStan repo first that changes
getTraitsto be recursive. Then we can use it here, to solve the issue.