Skip to content

otobank/phpstan-doctrine-criteria

Repository files navigation

Doctrine Criteria extensions for PHPStan

This extension provides following features:

  • Validates entity fields to which criteria is applied.
  • Depends on phpstan-doctrine, and includes its features.

Usage

composer require otobank/doctrine-target-aware-criteria
composer require --dev otobank/phpstan-doctrine-criteria
includes:
    - vendor/otobank/phpstan-doctrine-criteria/extension.neon
    - vendor/otobank/phpstan-doctrine-criteria/rules.neon

Configuration

parameters:
    doctrine:
        objectManagerLoader: bootstrap/phpstan_doctrine_manager.php

See: https://github.com/phpstan/phpstan-doctrine

You must use custom criteria.

FooCriteria

namespace App\Criteria;

use App\Entity\Foo;
use Otobank\PHPStan\Doctrine\Criteria;

class FooCriteria extends Criteria
{
    public static function getTargetClass() : string
    {
        return Foo::class;
    }
}

Use FooCriteria

namespace App\Entity;

use App\Criteria\FooCriteria;

class Bar
{
    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\OneToMany(targetEntity="Foo", mappedBy="bar")
     */
    private $foos;

    public function getFilteredFoos()
    {
        $criteria = FooCriteria::create();
        $criteria = $criteria
            ->where($criteria->expr()->eq('fieldX', 1)) // Check if fieldX is defined in Foo class
        ;

        return $this->foos->matching($criteria);
    }
}

Author

Toshiyuki Fujita - [email protected] - https://github.com/kalibora

License

Licensed under the MIT License - see the LICENSE file for details


OTOBANK Inc.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages