[Phalcon] Add DecoupleSaveMethodCallWithArgumentToAssignRector

This commit is contained in:
TomasVotruba 2020-02-23 20:13:15 +01:00
parent 51781bff4d
commit 671fa8a547
5 changed files with 164 additions and 4 deletions

View File

@ -1,4 +1,4 @@
# All 459 Rectors Overview
# All 460 Rectors Overview
- [Projects](#projects)
- [General](#general)
@ -5807,6 +5807,27 @@ Add $_SERVER REQUEST_URI to method call
<br>
### `DecoupleSaveMethodCallWithArgumentToAssignRector`
- class: [`Rector\Phalcon\Rector\MethodCall\DecoupleSaveMethodCallWithArgumentToAssignRector`](/../master/rules/phalcon/src/Rector/MethodCall/DecoupleSaveMethodCallWithArgumentToAssignRector.php)
- [test fixtures](/../master/rules/phalcon/tests/Rector/MethodCall/DecoupleSaveMethodCallWithArgumentToAssignRector/Fixture)
Decouple Phalcon\Mvc\Model::save() with argument to assign()
```diff
class SomeClass
{
public function run(\Phalcon\Mvc\Model $model, $data)
{
- $model->save($data);
+ $model->save();
+ $model->assign($data);
}
}
```
<br>
### `FlashWithCssClassesToExtraCallRector`
- class: [`Rector\Phalcon\Rector\Assign\FlashWithCssClassesToExtraCallRector`](/../master/rules/phalcon/src/Rector/Assign/FlashWithCssClassesToExtraCallRector.php)

View File

@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
namespace Rector\Phalcon\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
/**
* @see https://github.com/rectorphp/rector/issues/2571
*
* @see \Rector\Phalcon\Tests\Rector\MethodCall\DecoupleSaveMethodCallWithArgumentToAssignRector\DecoupleSaveMethodCallWithArgumentToAssignRectorTest
*/
final class DecoupleSaveMethodCallWithArgumentToAssignRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Decouple Phalcon\Mvc\Model::save() with argument to assign()', [
new CodeSample(
<<<'PHP'
class SomeClass
{
public function run(\Phalcon\Mvc\Model $model, $data)
{
$model->save($data);
}
}
PHP
,
<<<'PHP'
class SomeClass
{
public function run(\Phalcon\Mvc\Model $model, $data)
{
$model->save();
$model->assign($data);
}
}
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\Model')) {
return null;
}
if (! $this->isName($node->name, 'save')) {
return null;
}
if (count($node->args) === 0) {
return null;
}
$assignMethodCall = $this->createMethodCall($node->var, 'assign');
$assignMethodCall->args = $node->args;
$node->args = [];
$this->addNodeAfterNode($assignMethodCall, $node);
return $node;
}
}

View File

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

View File

@ -0,0 +1,28 @@
<?php
namespace Rector\Phalcon\Tests\Rector\MethodCall\DecoupleSaveMethodCallWithArgumentToAssignRector\Fixture;
class SomeClass
{
public function run(\Phalcon\Mvc\Model $model, $data)
{
$model->save($data);
}
}
?>
-----
<?php
namespace Rector\Phalcon\Tests\Rector\MethodCall\DecoupleSaveMethodCallWithArgumentToAssignRector\Fixture;
class SomeClass
{
public function run(\Phalcon\Mvc\Model $model, $data)
{
$model->save();
$model->assign($data);
}
}
?>

View File

@ -86,7 +86,7 @@ final class EregToPcreTransformer
return $this->icache[$content] = '#' . $r . '#mi';
}
return $cache[$content] = '#' . $r . '#m';
return $this->cache[$content] = '#' . $r . '#m';
}
/**
@ -249,9 +249,8 @@ final class EregToPcreTransformer
} else {
$r[$rr] .= '{' . $matches[1] . '}';
}
$i = $ii + 1;
return $i;
return $ii + 1;
}
private function processBracket(string $content, int $i, int $l, array &$r, int $rr)