Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Phalcon 4.0] Add FlashWithCssClassesToExtraCallRector #2437

Merged
merged 3 commits into from
Dec 16, 2019
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
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
"Rector\\Utils\\RectorGenerator\\": "utils/RectorGenerator/src",
"Rector\\StrictCodeQuality\\": "packages/StrictCodeQuality/src",
"Rector\\DynamicTypeAnalysis\\": "packages/DynamicTypeAnalysis/src",
"Rector\\PhpDeglobalize\\": "packages/PhpDeglobalize/src"
"Rector\\PhpDeglobalize\\": "packages/PhpDeglobalize/src",
"Rector\\Phalcon\\": "packages/Phalcon/src"
}
},
"autoload-dev": {
Expand Down Expand Up @@ -160,7 +161,8 @@
"Rector\\ZendToSymfony\\Tests\\": "packages/ZendToSymfony/tests",
"Rector\\StrictCodeQuality\\Tests\\": "packages/StrictCodeQuality/tests",
"Rector\\DynamicTypeAnalysis\\Tests\\": "packages/DynamicTypeAnalysis/tests",
"Rector\\PhpDeglobalize\\Tests\\": "packages/PhpDeglobalize/tests"
"Rector\\PhpDeglobalize\\Tests\\": "packages/PhpDeglobalize/tests",
"Rector\\Phalcon\\Tests\\": "packages/Phalcon/tests"
},
"classmap": [
"packages/Symfony/tests/Rector/FrameworkBundle/AbstractToConstructorInjectionRectorSource",
Expand Down
3 changes: 3 additions & 0 deletions config/set/phalcon/phalcon40.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,6 @@ services:
Rector\Renaming\Rector\ConstFetch\RenameConstantRector:
FILTER_SPECIAL_CHARS: 'FILTER_SPECIAL'
FILTER_ALPHANUM: 'FILTER_ALNUM'
Rector\Phalcon\Rector\Assign\FlashWithCssClassesToExtraCallRector: ~
Rector\Phalcon\Rector\Assign\NewApplicationToToFactoryWithDefaultContainerRector: ~
Rector\Phalcon\Rector\MethodCall\AddRequestToHandleMethodCallRector: ~
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\CodeQuality\Rector\FuncCall;

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\BinaryOp\Concat;
Expand Down Expand Up @@ -91,11 +92,11 @@ private function determineDelimiter(FuncCall $funcCall)
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;
}
Expand Down
8 changes: 5 additions & 3 deletions packages/NodeTypeResolver/src/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
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;
Expand Down Expand Up @@ -332,15 +334,15 @@ public function getStaticType(Node $node): Type

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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

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;

/**
* @see https://github.com/rectorphp/rector/issues/2408#issue-534441142
*
* @see \Rector\Phalcon\Tests\Rector\Assign\FlashWithCssClassesToExtraCallRector\FlashWithCssClassesToExtraCallRectorTest
*/
final class FlashWithCssClassesToExtraCallRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Add $cssClasses in Flash to separated method call', [
new CodeSample(
<<<'PHP'
class SomeClass {
public function run()
{
$cssClasses = [];
$flash = new Phalcon\Flash($cssClasses);
}
}
PHP
,
<<<'PHP'
class SomeClass {
public function run()
{
$cssClasses = [];
$flash = new Phalcon\Flash();
$flash->setCssClasses($cssClasses);
}
}
PHP

),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Assign::class];
}

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

if (! $this->isName($node->expr->class, 'Phalcon\Flash')) {
return null;
}

if (! isset($node->expr->args[0])) {
return null;
}

$argument = $node->expr->args[0];

// remove arg
unset($node->expr->args[0]);

// change the node

$variable = $node->var;
$setCssClassesMethodCall = new MethodCall($variable, 'setCssClasses', [$argument]);

$this->addNodeAfterNode($setCssClassesMethodCall, $node);

return $node;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

namespace Rector\Phalcon\Rector\Assign;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
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\Assign\NewApplicationToToFactoryWithDefaultContainerRector\NewApplicationToToFactoryWithDefaultContainerRectorTest
*/
final class NewApplicationToToFactoryWithDefaultContainerRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Change new application to default factory with application', [
new CodeSample(
<<<'PHP'
class SomeClass
{
public function run($di)
{
$application = new \Phalcon\Mvc\Application($di);

$response = $application->handle();
}
}
PHP
,
<<<'PHP'
class SomeClass
{
public function run($di)
{
$container = new \Phalcon\Di\FactoryDefault();
$application = new \Phalcon\Mvc\Application($container);

$response = $application->handle($_SERVER["REQUEST_URI"]);
}
}
PHP

),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Assign::class];
}

/**
* @param Assign $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isNewApplication($node->expr)) {
return null;
}

if (! $node->expr instanceof New_) {
return null;
}

$containerVariable = new Variable('container');
$factoryAssign = $this->createNewContainerToFactoryDefaultAssign($containerVariable);

$node->expr->args = [new Arg($containerVariable)];

$this->addNodeBeforeNode($factoryAssign, $node);

return $node;
}

private function isNewApplication(Expr $expr): bool
{
if (! $expr instanceof New_) {
return false;
}
return $this->isName($expr->class, 'Phalcon\Mvc\Application');
}

private function createNewContainerToFactoryDefaultAssign(Variable $variable): Assign
{
return new Assign($variable, new New_(new FullyQualified('Phalcon\Di\FactoryDefault')));
}
}
Original file line number Diff line number Diff line change
@@ -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'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\Phalcon\Tests\Rector\Assign\FlashWithCssClassesToExtraCallRector\Fixture;

class SomeClass {
public function run()
{
$cssClasses = [];
$flash = new \Phalcon\Flash($cssClasses);
}
}

?>
-----
<?php

namespace Rector\Phalcon\Tests\Rector\Assign\FlashWithCssClassesToExtraCallRector\Fixture;

class SomeClass {
public function run()
{
$cssClasses = [];
$flash = new \Phalcon\Flash();
$flash->setCssClasses($cssClasses);
}
}

?>
Loading