Skip to content

Commit 95dbf4e

Browse files
authored
[symfony] Skip invokable listeners (#197)
1 parent 65fda1f commit 95dbf4e

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/Rules/Symfony/NoListenerWithoutContractRule.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Name;
99
use PhpParser\Node\Stmt\Class_;
10+
use PhpParser\Node\Stmt\ClassMethod;
1011
use PHPStan\Analyser\Scope;
1112
use PHPStan\Node\InClassNode;
1213
use PHPStan\Rules\Rule;
@@ -78,6 +79,11 @@ public function processNode(Node $node, Scope $scope): array
7879
return [];
7980
}
8081

82+
// is invokable listeners?
83+
if ($classLike->getMethod('__invoke') instanceof ClassMethod) {
84+
return [];
85+
}
86+
8187
if ($this->isDoctrineListener($classLike)) {
8288
return [];
8389
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symplify\PHPStanRules\Tests\Rules\Symfony\NoListenerWithoutContractRule\Fixture;
4+
5+
use Symfony\Component\HttpKernel\Event\RequestEvent;
6+
7+
final class SkipInvokableListener
8+
{
9+
public function __invoke(RequestEvent $event)
10+
{
11+
}
12+
}

tests/Rules/Symfony/NoListenerWithoutContractRule/NoListenerWithoutContractRuleTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public function testRule(array $filePaths, array $expectedErrorsWithLines): void
2121

2222
public static function provideData(): Iterator
2323
{
24+
// @see https://symfony.com/blog/new-in-symfony-4-1-invokable-event-listeners
25+
yield [[__DIR__ . '/Fixture/SkipInvokableListener.php'], []];
26+
2427
yield [[__DIR__ . '/Fixture/SkipSecurityListener.php'], []];
2528
yield [[__DIR__ . '/Fixture/SkipAnotherSecurityListener.php'], []];
2629
yield [[__DIR__ . '/Fixture/SkipFormListener.php'], []];

0 commit comments

Comments
 (0)