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 config/sets/symfony/symfony-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rector\Symfony\CodeQuality\Rector\ClassMethod\ResponseReturnTypeControllerActionRector;
use Rector\Symfony\CodeQuality\Rector\MethodCall\AssertSameResponseCodeWithDebugContentsRector;
use Rector\Symfony\CodeQuality\Rector\MethodCall\LiteralGetToRequestClassConstantRector;
use Rector\Symfony\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector;
use Rector\Symfony\CodeQuality\Rector\MethodCall\StringCastDebugResponseRector;
use Rector\Symfony\Symfony26\Rector\MethodCall\RedirectToRouteRector;

Expand All @@ -35,6 +36,7 @@

// request method
RequestIsMainRector::class,
ParameterBagTypedGetMethodCallRector::class,

// tests
AssertSameResponseCodeWithDebugContentsRector::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ final class SomeController
{
public function index(Request $request)
{
return $request->isMainRequest();
return $request->isMasterRequest();
}

public function second(Request $request)
{
return $request->isMainRequest();
return $request->isMasterRequest();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\Fixture;

use Symfony\Component\HttpFoundation\Request;

final class GetOnBoolean
{
public function run(Request $request)
{
$debug = (bool) $request->query->get('debug', false);
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\Fixture;

use Symfony\Component\HttpFoundation\Request;

final class GetOnBoolean
{
public function run(Request $request)
{
$debug = $request->query->getBoolean('debug');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\Fixture;

use Symfony\Component\HttpFoundation\Request;

final class SkipNonCasted
{
public function run(Request $request)
{
$debug = $request->query->get('debug', false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ParameterBagTypedGetMethodCallRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ParameterBagTypedGetMethodCallRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\CodeQuality\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\Symfony\Enum\SymfonyClass;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Symfony\Tests\CodeQuality\Rector\MethodCall\ParameterBagTypedGetMethodCallRector\ParameterBagTypedGetMethodCallRectorTest
*/
final class ParameterBagTypedGetMethodCallRector extends AbstractRector
{
public function __construct(
private readonly ValueResolver $valueResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Make use of specific ParameterBag::get*() method with native return type declaration',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\HttpFoundation\Request;

class SomeClass
{
public function run(Request $request)
{
$debug = (bool) $request->query->get('debug', false);
}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
use Symfony\Component\HttpFoundation\Request;

class SomeClass
{
public function run(Request $request)
{
$debug = $request->query->getBoolean('debug');
}
}

CODE_SAMPLE
),

]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class, Bool_::class];
}

/**
* @param MethodCall|Bool_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof Bool_) {
if (! $node->expr instanceof MethodCall) {
return null;
}

return $this->refactorMethodCall($node->expr);
}

return null;
}

private function refactorMethodCall(MethodCall $methodCall): ?MethodCall
{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first class callable need to be skipped early

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// default value must be defined
if (count($methodCall->getArgs()) !== 2) {
return null;
}

if (! $this->isName($methodCall->name, 'get')) {
return null;
}

$callerType = $this->getType($methodCall->var);
if (! $callerType instanceof ObjectType) {
return null;
}

if (! $callerType->isInstanceOf(SymfonyClass::PARAMETER_BAG)->yes()) {
return null;
}

// the getBoolean() method must exist
if (! $callerType->hasMethod('getBoolean')->yes()) {
return null;
}

$defaultArg = $methodCall->getArgs()[1];

if ($this->valueResolver->isFalse($defaultArg->value)) {
unset($methodCall->args[1]);
$methodCall->name = new Identifier('getBoolean');

return $methodCall;
}

return null;
}
}

This file was deleted.

5 changes: 5 additions & 0 deletions src/Enum/SymfonyClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,9 @@ final class SymfonyClass
* @var string
*/
public const CONTAINER_CONFIGURATOR = 'Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator';

/**
* @var string
*/
public const PARAMETER_BAG = 'Symfony\Component\HttpFoundation\ParameterBag';
}
18 changes: 0 additions & 18 deletions src/Set/FOSRestSetList.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/Set/JMSSetList.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/Set/SensiolabsSetList.php

This file was deleted.

Loading