From 992b1400ac5a7cc84b1e9df2494dc09cfa213f6d Mon Sep 17 00:00:00 2001 From: l0gic Date: Fri, 5 Jun 2026 09:31:40 +0500 Subject: [PATCH] chore: raise PHPStan to level 9 Guard the validated value with a scalar/Stringable check before stringifying it in the violation message. --- README.md | 2 +- phpstan.neon | 2 +- src/AssertEntityExistValidator.php | 6 ++++++ src/AssertEntityNotExistValidator.php | 6 ++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b6e24e5..1b293f2 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![CI](https://img.shields.io/github/actions/workflow/status/k2gl/entity-exist/ci.yml?branch=main&label=CI&logo=github)](https://github.com/k2gl/entity-exist/actions/workflows/ci.yml) [![Latest Stable Version](https://img.shields.io/packagist/v/k2gl/entity-exist?logo=packagist&logoColor=white)](https://packagist.org/packages/k2gl/entity-exist) [![Total Downloads](https://img.shields.io/packagist/dt/k2gl/entity-exist?logo=packagist&logoColor=white)](https://packagist.org/packages/k2gl/entity-exist) -[![PHPStan Level](https://img.shields.io/badge/PHPStan-level%206-2a5ea7?logo=php&logoColor=white)](https://phpstan.org) +[![PHPStan Level](https://img.shields.io/badge/PHPStan-level%209-2a5ea7?logo=php&logoColor=white)](https://phpstan.org) [![License](https://img.shields.io/packagist/l/k2gl/entity-exist?color=yellowgreen)](https://packagist.org/packages/k2gl/entity-exist) ## Requirements diff --git a/phpstan.neon b/phpstan.neon index 6e451fa..3e639cd 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,4 +1,4 @@ parameters: - level: 6 + level: 9 paths: - src diff --git a/src/AssertEntityExistValidator.php b/src/AssertEntityExistValidator.php index 778ae64..52362a8 100644 --- a/src/AssertEntityExistValidator.php +++ b/src/AssertEntityExistValidator.php @@ -8,6 +8,8 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; +use Stringable; final class AssertEntityExistValidator extends ConstraintValidator { @@ -23,6 +25,10 @@ public function validate(mixed $value, Constraint $constraint): void return; } + if (! is_scalar($value) && ! $value instanceof Stringable) { + throw new UnexpectedValueException($value, expectedType: 'string'); + } + $data = $this->entityManager->getRepository($constraint->entity)->findOneBy([ $constraint->property => $value, ]); diff --git a/src/AssertEntityNotExistValidator.php b/src/AssertEntityNotExistValidator.php index 0353c56..c1d2b81 100644 --- a/src/AssertEntityNotExistValidator.php +++ b/src/AssertEntityNotExistValidator.php @@ -8,6 +8,8 @@ use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; use Symfony\Component\Validator\Exception\UnexpectedTypeException; +use Symfony\Component\Validator\Exception\UnexpectedValueException; +use Stringable; final class AssertEntityNotExistValidator extends ConstraintValidator { @@ -23,6 +25,10 @@ public function validate(mixed $value, Constraint $constraint): void return; } + if (! is_scalar($value) && ! $value instanceof Stringable) { + throw new UnexpectedValueException($value, expectedType: 'string'); + } + $data = $this->entityManager->getRepository($constraint->entity)->findOneBy([ $constraint->property => $value, ]);