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
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
'*/Source*/*',
'*/tests/*/Fixture*/Expected/*',
StringClassNameToClassConstantRector::class => [__DIR__ . '/config'],
\Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector::class => [__DIR__ . '/config'],

RenameForeachValueVariableToMatchMethodCallReturnTypeRector::class => [
// "data" => "datum" false positive
Expand All @@ -49,6 +50,7 @@
->withPreparedSets(
deadCode: true,
codeQuality: true,
codingStyle: true,
typeDeclarations: true,
privatization: true,
naming: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

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

use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

final class AllwoUse extends AbstractController
{
public function __construct()
{
}

#[Route('/some-action', name: 'some_action')]
public function someAction(LoggerInterface $logger)
{
$someClosure = function () use ($logger) {
$logger->log('level', 'value');
};
}
}

?>
-----
<?php

declare(strict_types=1);

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

use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

final class AllwoUse extends AbstractController
{
public function __construct(private readonly \Psr\Log\LoggerInterface $logger)
{
}

#[Route('/some-action', name: 'some_action')]
public function someAction()
{
$someClosure = function () {
$this->logger->log('level', 'value');
};
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private function removeSuffix(ClassMethod $classMethod, string $suffixToRemove):
if ($newName === $name) {
return null;
}

$classMethod->name = new Identifier($newName);
return $classMethod;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ private function isResponseReturnMethod(ClassMethod $classMethod, array $methods
if (! $methodCall->var instanceof Variable || $methodCall->var->name !== 'this') {
return false;
}

$functionName = $this->getName($methodCall->name);
if (! in_array($functionName, $methods, true)) {
return false;
Expand All @@ -186,21 +187,25 @@ private function refactorResponse(ClassMethod $classMethod): ?ClassMethod

return $classMethod;
}

if ($this->isResponseReturnMethod($classMethod, ['file'])) {
$classMethod->returnType = new FullyQualified(ResponseClass::BINARY_FILE);

return $classMethod;
}

if ($this->isResponseReturnMethod($classMethod, ['json'])) {
$classMethod->returnType = new FullyQualified(ResponseClass::JSON);

return $classMethod;
}

if ($this->isResponseReturnMethod($classMethod, ['stream'])) {
$classMethod->returnType = new FullyQualified(name: ResponseClass::STREAMED);

return $classMethod;
}

if ($this->isResponseReturnMethod($classMethod, ['render', 'forward', 'renderForm'])) {
$classMethod->returnType = new FullyQualified(ResponseClass::BASIC);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ private function refactorStmtsAwareNode(
$hasChanged = true;
}
}

return $hasChanged;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Symfony\CodeQuality\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
Expand Down Expand Up @@ -149,7 +150,17 @@ public function refactor(Node $node): ?Node
// replace param use with property fetch
$this->traverseNodesWithCallable((array) $classMethod->stmts, function (Node $node) use (
$paramNamesToReplace
): ?PropertyFetch {
): Closure|null|PropertyFetch {
if ($node instanceof Closure) {
foreach ($node->uses as $key => $closureUse) {
if ($this->isNames($closureUse->var, $paramNamesToReplace)) {
unset($node->uses[$key]);
}
}

return $node;
}

if (! $node instanceof Variable) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public function isDefaultsCall(MethodCall $methodCall): bool
if ($this->isName($currentMethodCall->name, 'defaults')) {
return true;
}

$currentMethodCall = $currentMethodCall->var;
}

Expand Down
1 change: 1 addition & 0 deletions rules/DependencyInjection/ThisGetTypeMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private function isValidContainerCall(MethodCall $methodCall): bool
if ($methodCall->var instanceof Variable && $this->nodeNameResolver->isName($methodCall->var, 'this')) {
return true;
}

return $methodCall->var instanceof PropertyFetch && $this->nodeNameResolver->isName(
$methodCall->var->var,
'this'
Expand Down
11 changes: 11 additions & 0 deletions rules/SwiftMailer/Rector/ClassMethod/SwiftMessageToEmailRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ public function refactor(Node $node): ?Node
if (! $this->isName($node->class, self::SWIFT_MESSAGE_FQN)) {
return null;
}

$args = $node->getArgs();
if ($args !== []) {
$node = new MethodCall(new New_(new FullyQualified(self::EMAIL_FQN)), 'subject', [$args[0]]);
} else {
$node->class = new FullyQualified(self::EMAIL_FQN);
}

$hasChanged = true;
}

Expand All @@ -161,6 +163,7 @@ public function refactor(Node $node): ?Node
if ($name === 'attach') {
$this->handleAttach($node);
}

if ($name === 'getId') {
$node = $this->handleId($node);
}
Expand Down Expand Up @@ -190,12 +193,15 @@ private function handleAddressMapping(MethodCall $methodCall, string $name): voi
if ($this->addressesMapping[$name] !== null) {
$methodCall->name = new Identifier($this->addressesMapping[$name]);
}

if ($methodCall->getArgs() === []) {
return;
}

if (! ($firstArg = $methodCall->args[0]) instanceof Arg) {
return;
}

if (
$firstArg->value instanceof Array_ &&
$firstArg->value->items !== []
Expand All @@ -212,12 +218,14 @@ private function handleAddressMapping(MethodCall $methodCall, string $name): voi
);
}
}

$methodCall->args = $newArgs;
} else {
$addressArguments = [new Arg($firstArg->value)];
if (isset($methodCall->args[1]) && ($secondArg = $methodCall->args[1]) instanceof Arg) {
$addressArguments[] = new Arg($secondArg->value);
}

$methodCall->args = [new Arg($this->createAddress($addressArguments))];
}
}
Expand All @@ -238,6 +246,7 @@ private function handleBody(MethodCall $methodCall, string $name): void
} else {
$methodCall->name = new Identifier('text');
}

$methodCall->args = [$methodCall->args[0]];
}

Expand All @@ -247,10 +256,12 @@ private function handleAttach(MethodCall $methodCall): void
if ($node instanceof StaticCall && $this->isName($node->name, 'fromPath')) {
$methodCall->args[0] = $node->args[0];
}

if ($node instanceof MethodCall) {
if ($this->isName($node->name, 'setFilename')) {
$methodCall->args[1] = $node->args[0];
}

if ($this->isName($node->name, 'setContentType')) {
$methodCall->args[2] = $node->args[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function refactor(Node $node): ?Node
if (! $hasChanged) {
return null;
}

return $node;
}

Expand All @@ -142,6 +143,7 @@ private function refactorOptionsArray(Array_ $optionsArray): bool
$hasChanged = true;
}
}

return $hasChanged;
}
}
1 change: 1 addition & 0 deletions rules/Symfony28/Rector/StaticCall/ParseFileRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ private function isArgumentYamlFile(StaticCall $staticCall): bool
if (! $nodeType instanceof ConstantStringType) {
return false;
}

return (bool) Strings::match($nodeType->getValue(), self::YAML_SUFFIX_REGEX);
}
}
1 change: 1 addition & 0 deletions rules/Symfony30/Rector/ClassMethod/GetRequestRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private function isActionWithGetRequestInBody(ClassMethod $classMethod): bool
if ($containsGetRequestMethod) {
return true;
}

/** @var MethodCall[] $getMethodCalls */
$getMethodCalls = $this->betterNodeFinder->find($classMethod, function (Node $node): bool {
if (! $node instanceof MethodCall) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ private function processChangeToConstant(Array_ $optionsArray, MethodCall $metho
if (! $optionsArrayItem instanceof ArrayItem) {
continue;
}

if (! $optionsArrayItem->key instanceof Expr) {
continue;
}

if (! $this->valueResolver->isValues($optionsArrayItem->key, ['type', 'entry_type'])) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Symfony\Component\Console\ConsoleEvents;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -33,11 +34,8 @@ public function getRuleDefinition(): RuleDefinition
return new RuleDefinition(
'Turns old event name with EXCEPTION to ERROR constant in Console in Symfony',
[
new CodeSample('"console.exception"', 'Symfony\Component\Console\ConsoleEvents::ERROR'),
new CodeSample(
'Symfony\Component\Console\ConsoleEvents::EXCEPTION',
'Symfony\Component\Console\ConsoleEvents::ERROR'
),
new CodeSample('"console.exception"', ConsoleEvents::class . '::ERROR'),
new CodeSample(ConsoleEvents::class . '::EXCEPTION', ConsoleEvents::class . '::ERROR'),
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ private function processArgumentPosition(New_|MethodCall $node, int $argumentPos
if (! $hasChanged) {
return null;
}

return $node;
}

Expand Down Expand Up @@ -147,6 +148,7 @@ private function processStringType(New_|MethodCall $expr, int $argumentPosition,
$args[$argumentPosition]->value = $this->nodeFactory->createArray($parts);
$hasChanged = true;
}

return $hasChanged;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ private function isIntegerTernaryIfElse(Ternary $ternary): bool
if (! $if instanceof Expr) {
$if = $ternary->cond;
}

/** @var Expr $else */
$else = $ternary->else;
$ifType = $this->getType($if);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function refactor(Node $node): ?Node
if (! $return->expr instanceof Int_) {
continue;
}

$classConstFetch = $this->convertNumberToConstant($return->expr);
if (! $classConstFetch instanceof ClassConstFetch) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function refactor(Node $node): ?Node
$classMethod
);
}

if ($this->isName(
$classMethod->name,
'resolve'
Expand Down Expand Up @@ -115,22 +116,27 @@ private function shouldRefactorClass(Class_ $class): bool
private function extractSupportsArguments(Class_ $class, int $key, ClassMethod $classMethod): array
{
$isIdentical = true;
$supportFirstArg = $supportSecondArg = null;
$supportFirstArg = null;
$supportSecondArg = null;

if ($classMethod->getStmts() === null) {
return [$isIdentical, $supportFirstArg, $supportSecondArg];
}

foreach ($classMethod->getStmts() as $stmt) {
if (! $stmt instanceof Return_) {
continue;
}

$expression = $stmt->expr;
if (! $expression instanceof BinaryOp) {
continue;
}

if ($expression instanceof NotIdentical) {
$isIdentical = false;
}

$supportFirstArg = $expression->left;
$supportSecondArg = $expression->right;
unset($class->stmts[$key]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ private function getNewArguments(?Expr $mapping, ?Expr $exprValue): array
) {
continue;
}

if (in_array($item->key->value, ['mapping', 'entity_manager'], true)) {
$probablyEntity = true;
}

$newArguments[] = new Arg($item->value, name: new Identifier($item->key->value));
}

Expand Down Expand Up @@ -244,6 +246,7 @@ private function getIndexForExprArg(array $args): ?int
return $key;
}
}

return null;
}
}
Loading