mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-19 06:18:07 +01:00
Add FunctionToMethodCallRector test
This commit is contained in:
parent
f44e42d2b9
commit
75982383ec
@ -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',
|
||||
],
|
||||
]
|
||||
),
|
||||
]);
|
||||
|
@ -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'],
|
||||
],
|
||||
]
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
@ -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,11 +45,12 @@ class SomeClass implements SomeInterface
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, [
|
||||
'$oldToNewInterfaces' => [
|
||||
'SomeOldInterface' => 'SomeInterface'
|
||||
,
|
||||
[
|
||||
'$oldToNewInterfaces' => [
|
||||
'SomeOldInterface' => 'SomeInterface',
|
||||
],
|
||||
]
|
||||
]
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
@ -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',
|
||||
]
|
||||
]
|
||||
],
|
||||
],
|
||||
]
|
||||
)
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -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',
|
||||
],
|
||||
],
|
||||
]
|
||||
),
|
||||
]);
|
||||
|
@ -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,24 +60,25 @@ final class UnsetAndIssetToMethodCallRector extends AbstractRector
|
||||
'$container->hasService("someKey");',
|
||||
[
|
||||
'$typeToMethodCalls' => [
|
||||
'Nette\DI\Container' => [
|
||||
Container::class => [
|
||||
'isset' => 'hasService',
|
||||
]
|
||||
]
|
||||
],
|
||||
],
|
||||
]
|
||||
),
|
||||
new ConfiguredCodeSample(
|
||||
'unset($container["someKey"])',
|
||||
'$container->removeService("someKey");',
|
||||
[
|
||||
[
|
||||
'$typeToMethodCalls' => [
|
||||
'Nette\DI\Container' => [
|
||||
'unset' => 'removeService'
|
||||
]
|
||||
]
|
||||
[
|
||||
'$typeToMethodCalls' => [
|
||||
Container::class => [
|
||||
'unset' => 'removeService',
|
||||
],
|
||||
],
|
||||
],
|
||||
]
|
||||
]),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -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']
|
||||
]
|
||||
)
|
||||
,
|
||||
[
|
||||
'$serviceDefinitionClass' => 'Nette\DI\ServiceDefinition',
|
||||
'$oldMethod' => 'setInject',
|
||||
'$newMethod' => 'addTag',
|
||||
'$newMethodArguments' => ['inject'],
|
||||
]
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -59,11 +59,11 @@ final class PropertyNameReplacerRector extends AbstractRector
|
||||
[
|
||||
'$perClassOldToNewProperties' => [
|
||||
'SomeClass' => [
|
||||
'someOldProperty' => 'someNewProperty'
|
||||
]
|
||||
]
|
||||
'someOldProperty' => 'someNewProperty',
|
||||
],
|
||||
],
|
||||
]
|
||||
)
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -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'],
|
||||
],
|
||||
],
|
||||
]
|
||||
),
|
||||
]);
|
||||
|
@ -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' => [
|
||||
'$constantToVisibilityByClass' => [
|
||||
'SOME_CONSTANT' => 'protected'
|
||||
]
|
||||
,
|
||||
[
|
||||
ParentObject::class => [
|
||||
'$constantToVisibilityByClass' => [
|
||||
'SOME_CONSTANT' => 'protected',
|
||||
],
|
||||
],
|
||||
]
|
||||
])]
|
||||
)]
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function someMethod()
|
||||
{
|
||||
$this->render('template', []);
|
||||
}
|
||||
}
|
@ -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';
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function someMethod()
|
||||
{
|
||||
view('template', []);
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
services:
|
||||
Rector\Rector\Function_\FunctionToMethodCallRector:
|
||||
$functionToMethodCall:
|
||||
'view': ['this', 'render']
|
Loading…
x
Reference in New Issue
Block a user