Add FunctionToMethodCallRector test

This commit is contained in:
Tomas Votruba 2018-08-01 21:35:45 +02:00
parent f44e42d2b9
commit 75982383ec
14 changed files with 111 additions and 54 deletions

View File

@ -8,7 +8,6 @@ use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Use_;
use Rector\Node\Attribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -38,8 +37,8 @@ final class ClassReplacerRector extends AbstractRector
'$value = new SomeNewClass;',
[
'$oldToNewClasses' => [
'SomeOldClass' => 'SomeNewClass'
]
'SomeOldClass' => 'SomeNewClass',
],
]
),
]);

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Rector\Rector\Dynamic;
namespace Rector\Rector\Function_;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
@ -8,7 +8,7 @@ use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use Rector\Node\MethodCallNodeFactory;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class FunctionToMethodCallRector extends AbstractRector
@ -35,7 +35,15 @@ final class FunctionToMethodCallRector extends AbstractRector
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Turns defined function calls to local method calls.', [
new CodeSample('view("...", []);', '$this->render("...", []);'),
new ConfiguredCodeSample(
'view("...", []);',
'$this->render("...", []);',
[
'$functionToMethodCall' => [
'view' => ['this', 'render'],
],
]
),
]);
}

View File

@ -7,7 +7,6 @@ use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use Rector\Node\Attribute;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -46,10 +45,11 @@ class SomeClass implements SomeInterface
{
}
CODE_SAMPLE
, [
,
[
'$oldToNewInterfaces' => [
'SomeOldInterface' => 'SomeInterface'
]
'SomeOldInterface' => 'SomeInterface',
],
]
),
]);

View File

@ -13,7 +13,6 @@ use Rector\Node\MethodCallNodeFactory;
use Rector\NodeAnalyzer\ExpressionAnalyzer;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -71,8 +70,8 @@ final class GetAndSetToMethodCallRector extends AbstractRector
'$typeToMethodCalls' => [
'SomeContainer' => [
'set' => 'addService',
]
]
],
],
]
),
new ConfiguredCodeSample(
@ -82,10 +81,10 @@ final class GetAndSetToMethodCallRector extends AbstractRector
'$typeToMethodCalls' => [
'SomeContainer' => [
'get' => 'getService',
],
],
]
]
]
)
),
]);
}

View File

@ -10,7 +10,6 @@ use Rector\Node\MethodCallNodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -82,9 +81,9 @@ CODE_SAMPLE
[
'$typeToMethodCalls' => [
'SomeObject' => [
'toString' => 'getPath'
]
]
'toString' => 'getPath',
],
],
]
),
]);

View File

@ -2,6 +2,7 @@
namespace Rector\Rector\MagicDisclosure;
use Nette\DI\Container;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Isset_;
@ -11,7 +12,6 @@ use PhpParser\Node\Stmt\Unset_;
use Rector\Node\MethodCallNodeFactory;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -60,10 +60,10 @@ final class UnsetAndIssetToMethodCallRector extends AbstractRector
'$container->hasService("someKey");',
[
'$typeToMethodCalls' => [
'Nette\DI\Container' => [
Container::class => [
'isset' => 'hasService',
]
]
],
],
]
),
new ConfiguredCodeSample(
@ -72,12 +72,13 @@ final class UnsetAndIssetToMethodCallRector extends AbstractRector
[
[
'$typeToMethodCalls' => [
'Nette\DI\Container' => [
'unset' => 'removeService'
Container::class => [
'unset' => 'removeService',
],
],
],
]
]
]
]),
),
]);
}

View File

@ -82,13 +82,14 @@ CODE_SAMPLE
$serviceDefinition = new Nette\DI\ServiceDefinition;
$serviceDefinition->addTag('inject');
CODE_SAMPLE
, [
,
[
'$serviceDefinitionClass' => 'Nette\DI\ServiceDefinition',
'$oldMethod' => 'setInject',
'$newMethod' => 'addTag',
'$newMethodArguments' => ['inject']
'$newMethodArguments' => ['inject'],
]
)
),
]);
}

View File

@ -59,11 +59,11 @@ final class PropertyNameReplacerRector extends AbstractRector
[
'$perClassOldToNewProperties' => [
'SomeClass' => [
'someOldProperty' => 'someNewProperty'
'someOldProperty' => 'someNewProperty',
],
],
]
]
]
)
),
]);
}

View File

@ -11,7 +11,6 @@ use Rector\Node\MethodCallNodeFactory;
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
@ -78,9 +77,9 @@ CODE_SAMPLE
[
'$perClassPropertyToMethods' => [
'SomeObject' => [
'property' => ['getProperty', 'setProperty']
]
]
'property' => ['getProperty', 'setProperty'],
],
],
]
),
]);

View File

@ -7,9 +7,9 @@ use PhpParser\Node\Stmt\ClassConst;
use Rector\Node\Attribute;
use Rector\NodeModifier\VisibilityModifier;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\ConfiguredCodeSample;
use Rector\RectorDefinition\RectorDefinition;
use Rector\Tests\Rector\Visibility\ChangeConstantVisibilityRector\Source\ParentObject;
final class ChangeConstantVisibilityRector extends AbstractRector
{
@ -60,13 +60,15 @@ class MyClass extends FrameworkClass
protected const SOME_CONSTANT = 1;
}
CODE_SAMPLE
, [
'Rector\Tests\Rector\Visibility\ChangeConstantVisibilityRector\Source\ParentObject' => [
,
[
ParentObject::class => [
'$constantToVisibilityByClass' => [
'SOME_CONSTANT' => 'protected'
'SOME_CONSTANT' => 'protected',
],
],
]
]
])]
)]
);
}

View File

@ -0,0 +1,9 @@
<?php
class SomeClass
{
public function someMethod()
{
$this->render('template', []);
}
}

View File

@ -0,0 +1,27 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Function_\FunctionToMethodCallRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
final class FunctionToMethodCallRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideWrongToFixedFiles()
*/
public function test(string $wrong, string $fixed): void
{
$this->doTestFileMatchesExpectedContent($wrong, $fixed);
}
public function provideWrongToFixedFiles(): Iterator
{
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
}
protected function provideConfig(): string
{
return __DIR__ . '/config.yml';
}
}

View File

@ -0,0 +1,9 @@
<?php
class SomeClass
{
public function someMethod()
{
view('template', []);
}
}

View File

@ -0,0 +1,4 @@
services:
Rector\Rector\Function_\FunctionToMethodCallRector:
$functionToMethodCall:
'view': ['this', 'render']