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
12 changes: 11 additions & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use PhpCsFixer\Fixer\StringNotation\SingleQuoteFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
Expand All @@ -10,4 +11,13 @@
)
->withRootFiles()
->withPreparedSets(psr12: true, symplify: true, common: true, strict: true)
->withSkip(['*/Source/*', '*/Fixture/*', '*/Expected/*']);
->withSkip([
'*/Source/*',
'*/Fixture/*',
'*/Expected/*',

// skip change "\\Entity" to '\\Entity as cause removed the \\ on scoped build
SingleQuoteFixer::class => [
__DIR__ . '/rules/CodeQuality/Rector/Class_/ControllerMethodInjectionToConstructorRector.php',
],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\ControllerMethodInjectionToConstructorRector\Fixture;

use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

final class SkipRequiredAnnotation extends AbstractController
{
private LoggerInterface $logger;

/**
* @required
*/
public function autowire(LoggerInterface $logger)
{
$this->logger = $logger;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\CodeQuality\Rector\Class_\ControllerMethodInjectionToConstructorRector\Fixture;

use Psr\Log\LoggerInterface;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

final class SkipRequiredAttribute extends AbstractController
{
private LoggerInterface $logger;

#[Required]
public function autowire(LoggerInterface $logger)
{
$this->logger = $logger;
}
}
19 changes: 17 additions & 2 deletions src/Bridge/NodeAnalyzer/ControllerMethodAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
namespace Rector\Symfony\Bridge\NodeAnalyzer;

use PhpParser\Node\Stmt\ClassMethod;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Symfony\Enum\SymfonyAttribute;
use Rector\Symfony\TypeAnalyzer\ControllerAnalyzer;

final readonly class ControllerMethodAnalyzer
{
public function __construct(
private ControllerAnalyzer $controllerAnalyzer
private ControllerAnalyzer $controllerAnalyzer,
private PhpDocInfoFactory $phpDocInfoFactory,
private PhpAttributeAnalyzer $phpAttributeAnalyzer
) {
}

Expand All @@ -23,6 +29,15 @@ public function isAction(ClassMethod $classMethod): bool
return false;
}

return $classMethod->isPublic() && ! $classMethod->isStatic();
if ($classMethod->isPublic() && ! $classMethod->isStatic()) {
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
if ($phpDocInfo instanceof PhpDocInfo && $phpDocInfo->hasByName('required')) {
return false;
}

return ! $this->phpAttributeAnalyzer->hasPhpAttribute($classMethod, SymfonyAttribute::REQUIRED);
}

return false;
}
}