otobank/phpstan-doctrine-criteria

Doctrine Criteria extensions for PHPStan

0.5.0 2023-03-01 23:52 UTC

This package is auto-updated.

Last update: 2024-11-07 20:41:41 UTC


README

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 - tfujita@otobank.co.jp - https://github.com/kalibora

License

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

OTOBANK Inc.