[Phalcon] Add AddRequestToHandleMethodCallRector

This commit is contained in:
TomasVotruba 2019-12-15 23:49:26 +01:00
parent 1b4dc68a81
commit 182da30324
12 changed files with 171 additions and 33 deletions

View File

@ -116,3 +116,4 @@ services:
FILTER_ALPHANUM: 'FILTER_ALNUM'
Rector\Phalcon\Rector\Assign\FlashWithCssClassesToExtraCallRector: ~
Rector\Phalcon\Rector\Assign\NewApplicationToToFactoryWithDefaultContainerRector: ~
Rector\Phalcon\Rector\MethodCall\AddRequestToHandleMethodCallRector: ~

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Rector\CodeQuality\Rector\FuncCall;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\Concat;
@ -91,11 +92,11 @@ PHP
if (! $leftMostConcatNode instanceof String_) {
return null;
}
$possibleLeftDelimiter = substr($leftMostConcatNode->value, 0, 1);
$possibleLeftDelimiter = Strings::substring($leftMostConcatNode->value, 0, 1);
if (! $rightMostConcatNode instanceof String_) {
return null;
}
$possibleRightDelimiter = substr(rtrim($rightMostConcatNode->value, self::ALL_MODIFIERS), -1, 1);
$possibleRightDelimiter = Strings::substring(rtrim($rightMostConcatNode->value, self::ALL_MODIFIERS), -1, 1);
if ($possibleLeftDelimiter === $possibleRightDelimiter) {
return $possibleLeftDelimiter;
}

View File

@ -22,6 +22,8 @@ use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
@ -332,15 +334,15 @@ final class NodeTypeResolver
if ($node instanceof Scalar) {
if ($nodeScope === null) {
if ($node instanceof Node\Scalar\DNumber) {
if ($node instanceof DNumber) {
return new ConstantFloatType($node->value);
}
if ($node instanceof Node\Scalar\String_) {
if ($node instanceof String_) {
return new ConstantStringType($node->value);
}
if ($node instanceof Node\Scalar\LNumber) {
if ($node instanceof LNumber) {
return new ConstantIntegerType($node->value);
}
}

View File

@ -6,6 +6,8 @@ namespace Rector\Phalcon\Rector\Assign;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -59,7 +61,7 @@ PHP
*/
public function refactor(Node $node): ?Node
{
if (! $node->expr instanceof Node\Expr\New_) {
if (! $node->expr instanceof New_) {
return null;
}
@ -79,7 +81,7 @@ PHP
// change the node
$variable = $node->var;
$setCssClassesMethodCall = new Node\Expr\MethodCall($variable, 'setCssClasses', [$argument]);
$setCssClassesMethodCall = new MethodCall($variable, 'setCssClasses', [$argument]);
$this->addNodeAfterNode($setCssClassesMethodCall, $node);

View File

@ -91,12 +91,7 @@ PHP
if (! $expr instanceof New_) {
return false;
}
if (! $this->isName($expr->class, 'Phalcon\Mvc\Application')) {
return false;
}
return true;
return $this->isName($expr->class, 'Phalcon\Mvc\Application');
}
private function createNewContainerToFactoryDefaultAssign(Variable $variable): Assign

View File

@ -0,0 +1,88 @@
<?php
declare(strict_types=1);
namespace Rector\Phalcon\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see https://github.com/rectorphp/rector/issues/2408
*/
/**
* @see \Rector\Phalcon\Tests\Rector\MethodCall\AddRequestToHandleMethodCallRector\AddRequestToHandleMethodCallRectorTest
*/
final class AddRequestToHandleMethodCallRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Add $_SERVER REQUEST_URI to method call', [
new CodeSample(
<<<'PHP'
class SomeClass {
public function run($di)
{
$application = new \Phalcon\Mvc\Application();
$response = $application->handle();
}
}
PHP
,
<<<'PHP'
class SomeClass {
public function run($di)
{
$application = new \Phalcon\Mvc\Application();
$response = $application->handle($_SERVER["REQUEST_URI"]);
}
}
PHP
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isObjectType($node->var, 'Phalcon\Mvc\Application')) {
return null;
}
if (! $this->isName($node->name, 'handle')) {
return null;
}
if ($node->args === null || $node->args !== []) {
return null;
}
$node->args[] = new Arg($this->createServerRequestUri());
return $node;
}
private function createServerRequestUri(): ArrayDimFetch
{
return new ArrayDimFetch(new Variable('_SERVER'), new String_('REQUEST_URI'));
}
}

View File

@ -2,12 +2,11 @@
namespace Rector\Phalcon\Tests\Rector\Assign\NewApplicationToToFactoryWithDefaultContainerRector\Fixture;
class SomeClass {
class SomeClass
{
public function run($di)
{
$application = new \Phalcon\Mvc\Application($di);
$response = $application->handle();
}
}
@ -17,13 +16,12 @@ class SomeClass {
namespace Rector\Phalcon\Tests\Rector\Assign\NewApplicationToToFactoryWithDefaultContainerRector\Fixture;
class SomeClass {
class SomeClass
{
public function run($di)
{
$container = new \Phalcon\Di\FactoryDefault();
$application = new \Phalcon\Mvc\Application($container);
$response = $application->handle($_SERVER["REQUEST_URI"]);
}
}

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Rector\Phalcon\Tests\Rector\MethodCall\AddRequestToHandleMethodCallRector;
use Iterator;
use Rector\Phalcon\Rector\MethodCall\AddRequestToHandleMethodCallRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class AddRequestToHandleMethodCallRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideDataForTest()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}
public function provideDataForTest(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
protected function getRectorClass(): string
{
return AddRequestToHandleMethodCallRector::class;
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\Phalcon\Tests\Rector\MethodCall\AddRequestToHandleMethodCallRector\Fixture;
class SomeClass {
public function run($di)
{
$application = new \Phalcon\Mvc\Application();
$response = $application->handle();
}
}
?>
-----
<?php
namespace Rector\Phalcon\Tests\Rector\MethodCall\AddRequestToHandleMethodCallRector\Fixture;
class SomeClass {
public function run($di)
{
$application = new \Phalcon\Mvc\Application();
$response = $application->handle($_SERVER['REQUEST_URI']);
}
}
?>

View File

@ -110,11 +110,6 @@ PHP
if ($parentNode instanceof Ternary) {
return true;
}
if (! isset($funcCall->args[0])) {
return true;
}
return false;
return ! isset($funcCall->args[0]);
}
}

View File

@ -20,12 +20,12 @@ parameters:
rector_recipe:
# run "bin/rector create" to create a new Rector + tests from this config
package: "Phalcon"
name: "NewApplicationToToFactoryWithDefaultContainerRector"
name: "AddRequestToHandleMethodCallRector"
node_types:
# put main node first, it is used to create namespace
- "Assign"
- "MethodCall"
description: "Change new application to default factory with application"
description: "Add $_SERVER REQUEST_URI to method call"
code_before: >
<?php
@ -33,8 +33,7 @@ parameters:
{
public function run($di)
{
$application = new \Phalcon\Mvc\Application($di);
$application = new \Phalcon\Mvc\Application();
$response = $application->handle();
}
}
@ -46,9 +45,7 @@ parameters:
{
public function run($di)
{
$container = new \Phalcon\Di\FactoryDefault();
$application = new \Phalcon\Mvc\Application($container);
$application = new \Phalcon\Mvc\Application();
$response = $application->handle($_SERVER["REQUEST_URI"]);
}
}

View File

@ -232,6 +232,7 @@ final class NodeFactory
{
$propertyBuilder = $this->builderFactory->property($name);
$propertyBuilder->makePrivate();
$property = $propertyBuilder->getNode();
$this->decorateParentPropertyProperty($property);
@ -257,6 +258,7 @@ final class NodeFactory
{
$propertyBuilder = $this->builderFactory->property($name);
$propertyBuilder->makePublic();
$property = $propertyBuilder->getNode();
$this->decorateParentPropertyProperty($property);