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
24 changes: 19 additions & 5 deletions src/Rules/Symfony/NoPublicNonActionMethodInControllerRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\Service\Attribute\Required;

/**
* Only route actions - "*Action()" and "__invoke()" - can be public in a controller.
*
* Magic methods, #[Required] setters, methods required by a parent class or interface and event subscriber
* listeners are skipped.
* Magic methods, #[Route] actions, #[Required] setters, methods required by a parent class or interface and
* event subscriber listeners are skipped.
*
* @see \Symplify\PHPStanRules\Tests\Rules\Symfony\NoPublicNonActionMethodInControllerRule\NoPublicNonActionMethodInControllerRuleTest
*
Expand Down Expand Up @@ -45,6 +46,14 @@ final class NoPublicNonActionMethodInControllerRule implements Rule
*/
private const REQUIRED_ATTRIBUTE = Required::class;

/**
* @var string[]
*/
private const ROUTE_ATTRIBUTES = [
Route::class,
\Symfony\Component\Routing\Annotation\Route::class,
];

/**
* @var string
*/
Expand Down Expand Up @@ -93,7 +102,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if ($this->hasRequiredAttribute($node) || $this->isInheritedContract($classReflection, $methodName)) {
if ($this->hasSkippedAttribute($node) || $this->isInheritedContract($classReflection, $methodName)) {
return [];
}

Expand All @@ -106,11 +115,16 @@ public function processNode(Node $node, Scope $scope): array
];
}

private function hasRequiredAttribute(ClassMethod $classMethod): bool
private function hasSkippedAttribute(ClassMethod $classMethod): bool
{
foreach ($classMethod->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === self::REQUIRED_ATTRIBUTE) {
$attributeName = $attr->name->toString();
if ($attributeName === self::REQUIRED_ATTRIBUTE) {
return true;
}

if (in_array($attributeName, self::ROUTE_ATTRIBUTES, true)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\Symfony\NoPublicNonActionMethodInControllerRule\Fixture;

use Symfony\Component\Routing\Annotation\Route as AnnotationRoute;
use Symfony\Component\Routing\Attribute\Route;

final class SkipRouteAttributeController
{
#[Route('/submit', name: 'submit', methods: ['POST'])]
public function submit(): void
{
}

#[AnnotationRoute('/legacy', name: 'legacy')]
public function legacy(): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static function provideData(): Iterator

yield [__DIR__ . '/Fixture/SomeController.php', [[$errorMessage, 9]]];
yield [__DIR__ . '/Fixture/SkipActionController.php', []];
yield [__DIR__ . '/Fixture/SkipRouteAttributeController.php', []];
}

protected function getRule(): Rule
Expand Down
Loading