phpstan-doctrine
phpstan-doctrine copied to clipboard
Feature request: field validation when using matching()/Criteria
Right now, the phpstan-doctrine extension is able to validate that common loading patterns (findBy and friends) are only passed mapped columns, which is awesome and has saved me from more errors than I'd care to admit!
Today, I noticed that if you need to perform a more advanced query using Criteria
(e.g. fields greater/less than some value), that same validation doesn't appear to take place - I can mistype a field name and get a runtime error without advance warning from the plugin. This appears to be the case with or without the "advanced" configuration (objectManagerLoader) being performed.
I expect this is a low-priority, low-volume scenario - but it would be really great if somehow these mappings could get tracked all the way through and make it into analysis.
Rough example (setup excluded):
#[Entity]
class MyModel
{
#[Column]
public ?DateTime $dueAt;
}
// ...
use Doctrine\Common\Collections\Criteria;
$expr = Criteria::expr();
$criteria = Criteria::create()->where($expr->lt('due', new DateTime())); // note field name mismatch
assert($em instanceof Doctrine\ORM\EntityManagerInterface);
$models = $em->getRepository(MyModel::class)->matching($criteria)->toArray();
I expect based on type information, the error would probably have to appear on the matching()
call rather than in the criteria setup (since criteria on its own is basically a context-free WHERE clause); that would still be more than sufficient to get advance warning of a problem :)