phpstan-disallowed-calls icon indicating copy to clipboard operation
phpstan-disallowed-calls copied to clipboard

disallow method call with inheritance

Open verfriemelt-dot-org opened this issue 1 year ago • 3 comments

given a doctrine repository which looks like this:

/**
 * @extends ServiceEntityRepository<Version>
 *
 * @method Version|null find($id, $lockMode = null, $lockVersion = null)
 * @method Version|null findOneBy(array $criteria, array $orderBy = null)
 * @method Version[]    findAll()
 * @method Version[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class VersionRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Version::class);
    }
}

i want to disallow magic methods like $repo->findOneBy([ ... ]) which would work with:

  disallowedMethodCalls:
        -
            method: 'Doctrine\Persistence\ObjectRepository::find*()'
            message: 'to not use magic find*() methods'
            errorTip: 'add method to interface instead!'
            allowIn:
                - src/Repository/*
                - tests/*

but this will also disallow the following method:

class VersionRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Version::class);
    }

    public function findOneByNumber(string $versionNumber): ?Version
    {
      return $this->createQueryBuilder('v')
            ->andWhere('v.number = :val')
            ->setParameter('val', $versionNumber)
            ->getQuery()
            ->getOneOrNullResult();
    }
}
Calling Doctrine\Persistence\ObjectRepository::findOneByNumber() (as App\Repository\VersionRepository::findOneByNumber()) is forbidden, to not use magic find*() methods.

:thinking: is there an option or could an option be introduced, to limit the scope to only the parent class for that check?

verfriemelt-dot-org avatar Oct 11 '24 08:10 verfriemelt-dot-org