Larastan: Nested SoftDelete not detected since 0.7.5

Created on 27 May 2021  路  9Comments  路  Source: nunomaduro/larastan

  • Larastan Version: ^0.7.5
  • --level used: 8
  • Pull request with failing test: #838

Description

Larastan does not detect a nested SoftDeletes trait after updating from 0.7.4 to 0.7.5 or higher.

Laravel code where the issue was found

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;
}
Waiting for feedback

Most helpful comment

@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.

All 9 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zlodes picture zlodes  路  3Comments

fgilio picture fgilio  路  4Comments

danielcosta picture danielcosta  路  4Comments

spaceemotion picture spaceemotion  路  4Comments

bogdankharchenko picture bogdankharchenko  路  4Comments