Skip to content

Commit c2c6c41

Browse files
committed
Symfony: detect callback constraints
1 parent 0c989a4 commit c2c6c41

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Provider/SymfonyUsageProvider.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ protected function shouldMarkAsUsed(ReflectionMethod $method): ?string
326326
return 'Route method via #[Route] attribute';
327327
}
328328

329+
if ($this->isMethodWithCallbackConstraintAttribute($method)) {
330+
return 'Callback constraint method via #[Assert\Callback] attribute';
331+
}
332+
329333
if ($this->isProbablySymfonyListener($method)) {
330334
return 'Probable listener method';
331335
}
@@ -461,6 +465,11 @@ protected function isMethodWithRouteAttribute(ReflectionMethod $method): bool
461465
|| $this->hasAttribute($method, 'Symfony\Component\Routing\Annotation\Route', $isInstanceOf);
462466
}
463467

468+
protected function isMethodWithCallbackConstraintAttribute(ReflectionMethod $method): bool
469+
{
470+
return $this->hasAttribute($method, 'Symfony\Component\Validator\Constraints\Callback');
471+
}
472+
464473
/**
465474
* Ideally, we would need to parse DIC xml to know this for sure just like phpstan-symfony does.
466475
*/

tests/Rule/data/providers/symfony.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1717
use Symfony\Component\Routing\Attribute\Route;
1818
use Symfony\Contracts\Service\Attribute\Required;
19+
use Symfony\Component\Validator\Constraints as Assert;
1920

2021
class SomeController {
2122

@@ -115,3 +116,9 @@ public function create(): self {
115116
class Sftp {
116117
const RETRY_LIMIT = 3; // used in yaml via !php/const
117118
}
119+
120+
class ValidatedModel
121+
{
122+
#[Assert\Callback]
123+
public function validate(): void {}
124+
}

0 commit comments

Comments
 (0)