Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Provider/SymfonyUsageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ protected function shouldMarkAsUsed(ReflectionMethod $method): ?string
return 'Route method via #[Route] attribute';
}

if ($this->isMethodWithCallbackConstraintAttribute($method)) {
return 'Callback constraint method via #[Assert\Callback] attribute';
}

if ($this->isProbablySymfonyListener($method)) {
return 'Probable listener method';
}
Expand Down Expand Up @@ -461,6 +465,23 @@ protected function isMethodWithRouteAttribute(ReflectionMethod $method): bool
|| $this->hasAttribute($method, 'Symfony\Component\Routing\Annotation\Route', $isInstanceOf);
}

protected function isMethodWithCallbackConstraintAttribute(ReflectionMethod $method): bool
{
$attributes = $method->getDeclaringClass()->getAttributes('Symfony\Component\Validator\Constraints\Callback');

foreach ($attributes as $attribute) {
$arguments = $attribute->getArguments();

$callback = $arguments['callback'] ?? $arguments[0] ?? null;

if ($callback === $method->getName()) {
return true;
}
}

return $this->hasAttribute($method, 'Symfony\Component\Validator\Constraints\Callback');
}

/**
* Ideally, we would need to parse DIC xml to know this for sure just like phpstan-symfony does.
*/
Expand Down Expand Up @@ -502,6 +523,7 @@ private function isSymfonyInstalled(): bool
|| InstalledVersions::isInstalled('symfony/contracts')
|| InstalledVersions::isInstalled('symfony/console')
|| InstalledVersions::isInstalled('symfony/http-kernel')
|| InstalledVersions::isInstalled('symfony/validator')
|| InstalledVersions::isInstalled('symfony/dependency-injection');
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Rule/data/providers/symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Component\Validator\Constraints as Assert;

class SomeController {

Expand Down Expand Up @@ -115,3 +116,21 @@ public function create(): self {
class Sftp {
const RETRY_LIMIT = 3; // used in yaml via !php/const
}

class ModelValidator
{
public static function validate(): void {}
}

#[Assert\Callback([ModelValidator::class, 'validate'])]
#[Assert\Callback('validateBar')]
#[Assert\Callback(callback: 'validateBaz')]
class ValidatedModel
{
#[Assert\Callback]
public function validateFoo(): void {}

public function validateBar(): void {}

public function validateBaz(): void {}
}