use standard __ for vars

This commit is contained in:
TomasVotruba 2020-07-28 17:15:30 +02:00
parent 6817edb902
commit 2eddb274c2
25 changed files with 352 additions and 135 deletions

View File

@ -1,4 +1,4 @@
# All 537 Rectors Overview
# All 539 Rectors Overview
- [Projects](#projects)
- [General](#general)
@ -30,7 +30,7 @@
- [MockistaToMockery](#mockistatomockery) (2)
- [MysqlToMysqli](#mysqltomysqli) (4)
- [Naming](#naming) (3)
- [Nette](#nette) (12)
- [Nette](#nette) (13)
- [NetteCodeQuality](#nettecodequality) (4)
- [NetteKdyby](#nettekdyby) (4)
- [NetteTesterToPHPUnit](#nettetestertophpunit) (3)
@ -5284,6 +5284,32 @@ Change `file_put_contents()` to `FileSystem::write()`
<br><br>
### `GetConfigWithDefaultsArgumentToArrayMergeInCompilerExtensionRector`
- class: [`Rector\Nette\Rector\MethodCall\GetConfigWithDefaultsArgumentToArrayMergeInCompilerExtensionRector`](/../master/rules/nette/src/Rector/MethodCall/GetConfigWithDefaultsArgumentToArrayMergeInCompilerExtensionRector.php)
- [test fixtures](/../master/rules/nette/tests/Rector/MethodCall/GetConfigWithDefaultsArgumentToArrayMergeInCompilerExtensionRector/Fixture)
Change `$this->getConfig($defaults)` to `array_merge`
```diff
use Nette\DI\CompilerExtension;
final class SomeExtension extends CompilerExtension
{
private $defaults = [
'key' => 'value'
];
public function loadConfiguration()
{
- $config = $this->getConfig($this->defaults);
+ $config = array_merge($this->defaults, $this->getConfig());
}
}
```
<br><br>
### `JsonDecodeEncodeToNetteUtilsJsonDecodeEncodeRector`
- class: [`Rector\Nette\Rector\FuncCall\JsonDecodeEncodeToNetteUtilsJsonDecodeEncodeRector`](/../master/rules/nette/src/Rector/FuncCall/JsonDecodeEncodeToNetteUtilsJsonDecodeEncodeRector.php)
@ -12160,7 +12186,7 @@ Change @return types and type from static analysis to type declarations if not a
## General
- [Core](#core) (39)
- [Core](#core) (40)
## Core
@ -12794,6 +12820,48 @@ return function (ContainerConfigurator $containerConfigurator) : void {
<br><br>
### `MethodCallToStaticCallRector`
- class: [`Rector\Core\Rector\MethodCall\MethodCallToStaticCallRector`](/../master/src/Rector/MethodCall/MethodCallToStaticCallRector.php)
- [test fixtures](/../master/tests/Rector/MethodCall/MethodCallToStaticCallRector/Fixture)
Change method call to desired static call
```php
<?php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Rector\Core\Rector\MethodCall\MethodCallToStaticCallRector;
return function (ContainerConfigurator $containerConfigurator) : void {
$services = $containerConfiguration->services();
$services->set(MethodCallToStaticCallRector::class)
->arg('$methodCallsToStaticCalls', ['AnotherDependency' => [['StaticCaller', 'anotherMethod']]]);
};
```
```diff
final class SomeClass
{
private $anotherDependency;
public function __construct(AnotherDependency $anotherDependency)
{
$this->anotherDependency = $anotherDependency;
}
public function loadConfiguration()
{
- return $this->anotherDependency->process('value');
+ return StaticCaller::anotherMethod('value');
}
}
```
<br><br>
### `NewObjectToFactoryCreateRector`
- class: [`Rector\Core\Rector\Architecture\Factory\NewObjectToFactoryCreateRector`](/../master/src/Rector/Architecture/Factory/NewObjectToFactoryCreateRector.php)

View File

@ -74,6 +74,8 @@ return static function (ContainerConfigurator $containerConfigurator): void {
# generated from /vendor
__DIR__ . '/packages/doctrine-annotation-generated/src/ConstantPreservingDocParser.php',
__DIR__ . '/packages/doctrine-annotation-generated/src/ConstantPreservingAnnotationReader.php',
// template files
__DIR__ . '/packages/rector-generator/templates/*',
]);
$parameters->set(Option::SKIP, [

View File

@ -140,9 +140,6 @@ final class CreateRectorCommand extends Command
$templateFileInfos = $this->templateFinder->find($configuration);
dump($templateFileInfos);
die;
$isUnwantedOverride = $this->overrideGuard->isUnwantedOverride(
$templateFileInfos,
$templateVariables,
@ -155,10 +152,10 @@ final class CreateRectorCommand extends Command
return ShellCode::SUCCESS;
}
// $this->configFilesystem->appendRectorServiceToSet($configuration, $templateVariables);
$this->generateFiles($templateFileInfos, $templateVariables, $configuration);
$this->configFilesystem->appendRectorServiceToSet($configuration, $templateVariables);
$this->printSuccess($configuration->getName());
return ShellCode::SUCCESS;

View File

@ -20,7 +20,7 @@ final class ConfigFilesystem
/**
* @var string
*/
private const RECTOR_FQN_NAME_PATTERN = 'Rector\_Package_\Rector\_Category_\_Name_';
private const RECTOR_FQN_NAME_PATTERN = 'Rector\__Package__\Rector\__Category__\__Name__';
/**
* @var TemplateFactory

View File

@ -9,6 +9,7 @@ use Rector\Core\Set\SetResolver;
use Rector\RectorGenerator\Guard\RecipeGuard;
use Rector\RectorGenerator\ValueObject\Configuration;
use Rector\RectorGenerator\ValueObject\RecipeOption;
use Symplify\SetConfigResolver\ValueObject\Set;
final class ConfigurationFactory
{
@ -38,12 +39,8 @@ final class ConfigurationFactory
$nodeTypeClasses = $rectorRecipe[RecipeOption::NODE_TYPES];
$category = $this->resolveCategoryFromFqnNodeTypes($nodeTypeClasses);
$extraFileContent = isset($rectorRecipe[RecipeOption::EXTRA_FILE_CONTENT]) ? $this->normalizeCode(
$rectorRecipe[RecipeOption::EXTRA_FILE_CONTENT]
) : null;
$set = $rectorRecipe['set'] ? $this->setResolver->resolveSetByName($rectorRecipe['set']) : null;
$extraFileContent = $this->resolveExtraFileContent($rectorRecipe);
$set = $this->resolveeSet($rectorRecipe);
return new Configuration(
$rectorRecipe[RecipeOption::PACKAGE],
@ -83,4 +80,20 @@ final class ConfigurationFactory
{
return Strings::startsWith($code, '<?php');
}
private function resolveExtraFileContent(array $rectorRecipe)
{
return isset($rectorRecipe[RecipeOption::EXTRA_FILE_CONTENT]) ? $this->normalizeCode(
$rectorRecipe[RecipeOption::EXTRA_FILE_CONTENT]
) : null;
}
private function resolveeSet(array $rectorRecipe): ?Set
{
if ($rectorRecipe[RecipeOption::SET]) {
return $this->setResolver->resolveSetByName($rectorRecipe[RecipeOption::SET]);
}
return null;
}
}

View File

@ -8,6 +8,7 @@ use Nette\Utils\Strings;
use Rector\RectorGenerator\Finder\TemplateFinder;
use Rector\RectorGenerator\ValueObject\Configuration;
use Rector\RectorGenerator\ValueObject\Package;
use Rector\RectorGenerator\ValueObject\RecipeOption;
use Symplify\SmartFileSystem\SmartFileInfo;
final class TemplateFileSystem
@ -23,19 +24,17 @@ final class TemplateFileSystem
$destination = $smartFileInfo->getRelativeFilePathFromDirectory(TemplateFinder::TEMPLATES_DIRECTORY);
// normalize core package
if ($configuration->getPackage() === 'Rector') {
$destination = Strings::replace($destination, '#rules\/_package_/tests/Rector#', 'tests/Rector');
$destination = Strings::replace($destination, '#rules\/_package_/src/Rector#', 'src/Rector');
if ($configuration->getPackage() === RecipeOption::PACKAGE_CORE) {
$destination = Strings::replace($destination, '#rules\/__package__/tests/Rector#', 'tests/Rector');
$destination = Strings::replace($destination, '#rules\/__package__/src/Rector#', 'src/Rector');
} elseif ($configuration->getPackage() === Package::UTILS) {
// special keyword for 3rd party Rectors, not for core Github contribution
$destination = Strings::replace($destination, '#packages\/_Package_#', 'utils/rector');
$destination = Strings::replace($destination, '#packages\/__Package__#', 'utils/rector');
}
if (! Strings::match($destination, '#fixture[\d+]*\.php\.inc#')) {
$destination = rtrim($destination, '.inc');
}
return $this->applyVariables($destination, $templateVariables);
// remove _Configured|_Extra prefix
$destination = $this->applyVariables($destination, $templateVariables);
return Strings::replace($destination, '#(__Configured|__Extra)#', '');
}
/**

View File

@ -5,7 +5,6 @@ declare(strict_types=1);
namespace Rector\RectorGenerator\Finder;
use Rector\RectorGenerator\ValueObject\Configuration;
use Symfony\Component\Finder\Finder;
use Symplify\SmartFileSystem\Finder\FinderSanitizer;
use Symplify\SmartFileSystem\SmartFileInfo;
@ -33,33 +32,53 @@ final class TemplateFinder
{
$filePaths = [];
if ($configuration->getRuleConfiguration()) {
$filePaths[] = __DIR__ . '/../../templates/rules/_package_/src/Rector/_Category_/_Name_WithConfiguration_.php.inc';
$filePaths[] = __DIR__ . '/../../templates/rules/_package_/tests/Rector/_Category_/_Name_/Configured_Name_Test.php.inc';
} else {
$filePaths[] = __DIR__ . '/../../templates/rules/_package_/src/Rector/_Category_/_Name_.php.inc';
$filePaths[] = __DIR__ . '/../../templates/rules/_package_/tests/Rector/_Category_/_Name_/_Name_Test.php.inc';
if ($configuration->getExtraFileContent()) {
$filePaths[] = __DIR__ . '/../../templates/rules/__package__/tests/Rector/__Category__/__Name__/Source/extra_file.php';
}
$filePaths = $this->addRuleAndTestCase($configuration, $filePaths);
$filePaths[] = $this->resolveFixtureFilePath($configuration->isPhpSnippet());
if ($configuration->getExtraFileContent()) {
$filePaths[] = __DIR__ . '/../../templates/rules/_package_/tests/Rector/_Category_/_Name_/Source/extra_file.php.inc';
$filePaths[] = __DIR__ . '/../../templates/rules/_package_/tests/Rector/_Category_/_Name_/_Name_ExtraTest.php.inc';
} else {
$filePaths[] = __DIR__ . '/../../templates/rules/_package_/tests/Rector/_Category_/_Name_/_Name_Test.php.inc';
}
return $this->finderSanitizer->sanitize($filePaths);
}
private function resolveFixtureFilePath(bool $isPhpSnippet): string
{
if ($isPhpSnippet) {
return __DIR__ . '/../../templates/rules/_package_/tests/Rector/_Category_/_Name_/Fixture/fixture.php.inc';
return __DIR__ . '/../../templates/rules/__package__/tests/Rector/__Category__/__Name__/Fixture/fixture.php';
}
// is html snippet
return __DIR__ . '/../../templates/rules/_package_/tests/Rector/_Category_/_Name_/Fixture/html_fixture.php.inc';
return __DIR__ . '/../../templates/rules/__package__/tests/Rector/__Category__/__Name__/Fixture/html_fixture.php';
}
/**
* @param string[] $filePaths
* @return string[]
*/
private function addRuleAndTestCase(Configuration $configuration, array $filePaths): array
{
if ($configuration->getRuleConfiguration()) {
$filePaths[] = __DIR__ . '/../../templates/rules/__package__/src/Rector/__Category__/__Configured__Name__.php';
if ($configuration->getExtraFileContent()) {
$filePaths[] = __DIR__ . '/../../templates/rules/__package__/tests/Rector/__Category__/__Name__/__Configured__Extra__Name__Test.php';
} else {
$filePaths[] = __DIR__ . '/../../templates/rules/__package__/tests/Rector/__Category__/__Name__/__Configured__Name__Test.php';
}
return $filePaths;
}
if ($configuration->getExtraFileContent()) {
$filePaths[] = __DIR__ . '/../../templates/rules/__package__/tests/Rector/__Category__/__Name__/__Extra__Name__Test.php';
} else {
$filePaths[] = __DIR__ . '/../../templates/rules/__package__/tests/Rector/__Category__/__Name__/__Name__Test.php';
}
$filePaths[] = __DIR__ . '/../../templates/rules/__package__/src/Rector/__Category__/__Name__.php';
return $filePaths;
}
}

View File

@ -5,10 +5,11 @@ declare(strict_types=1);
namespace Rector\RectorGenerator;
use Nette\Utils\Strings;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use Rector\Core\Exception\NotImplementedYetException;
use Rector\Core\PhpParser\Node\NodeFactory;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\RectorGenerator\ValueObject\Configuration;
@ -19,6 +20,7 @@ final class TemplateVariablesFactory
* @var BetterStandardPrinter
*/
private $betterStandardPrinter;
/**
* @var NodeFactory
*/
@ -36,27 +38,38 @@ final class TemplateVariablesFactory
public function createFromConfiguration(Configuration $configuration): array
{
$data = [
'_Package_' => $configuration->getPackage(),
'_package_' => $configuration->getPackageDirectory(),
'_Category_' => $configuration->getCategory(),
'_Description_' => $configuration->getDescription(),
'_Name_' => $configuration->getName(),
'_CodeBefore_' => trim($configuration->getCodeBefore()) . PHP_EOL,
'_CodeBeforeExample_' => $this->createCodeForDefinition($configuration->getCodeBefore()),
'_CodeAfter_' => trim($configuration->getCodeAfter()) . PHP_EOL,
'_CodeAfterExample_' => $this->createCodeForDefinition($configuration->getCodeAfter()),
'_Source_' => $this->createSourceDocBlock($configuration->getSource()),
'_Configuration_' => $this->createConfiguration($configuration->getRuleConfiguration()),
'__Package__' => $configuration->getPackage(),
'__package__' => $configuration->getPackageDirectory(),
'__Category__' => $configuration->getCategory(),
'__Description__' => $configuration->getDescription(),
'__Name__' => $configuration->getName(),
'__CodeBefore__' => trim($configuration->getCodeBefore()) . PHP_EOL,
'__CodeBeforeExample__' => $this->createCodeForDefinition($configuration->getCodeBefore()),
'__CodeAfter__' => trim($configuration->getCodeAfter()) . PHP_EOL,
'__CodeAfterExample__' => $this->createCodeForDefinition($configuration->getCodeAfter()),
'__Source__' => $this->createSourceDocBlock($configuration->getSource()),
];
if ($configuration->getExtraFileContent() !== null && $configuration->getExtraFileName() !== null) {
$data['_ExtraFileName_'] = $configuration->getExtraFileName();
$data['_ExtraFileContent_'] = trim($configuration->getExtraFileContent()) . PHP_EOL;
$data['_ExtraFileContentExample_'] = $this->createCodeForDefinition($configuration->getExtraFileContent());
if ($configuration->getRuleConfiguration() !== []) {
$data['__RuleConfiguration__'] = $this->createRuleConfiguration($configuration->getRuleConfiguration());
$data['__ConfigurationProperty__'] = $this->createConfigurationProperty(
$configuration->getRuleConfiguration()
);
$data['__ConfigurationConstructor__'] = $this->createConfigurationConstructor(
$configuration->getRuleConfiguration()
);
}
$data['_NodeTypes_Php_'] = $this->createNodeTypePhp($configuration);
$data['_NodeTypes_Doc_'] = '\\' . implode('|\\', $configuration->getNodeTypes());
if ($configuration->getExtraFileContent() !== null && $configuration->getExtraFileName() !== null) {
$data['__ExtraFileName__'] = $configuration->getExtraFileName();
$data['__ExtraFileContent__'] = trim($configuration->getExtraFileContent()) . PHP_EOL;
$data['__ExtraFileContentExample__'] = $this->createCodeForDefinition(
$configuration->getExtraFileContent()
);
}
$data['__NodeTypes_Php__'] = $this->createNodeTypePhp($configuration);
$data['__NodeTypes_Doc__'] = '\\' . implode('|\\', $configuration->getNodeTypes());
return $data;
}
@ -93,26 +106,62 @@ final class TemplateVariablesFactory
private function createNodeTypePhp(Configuration $configuration): string
{
$arrayNodes = [];
$referencingClassConsts = [];
foreach ($configuration->getNodeTypes() as $nodeType) {
$classConstFetchNode = new ClassConstFetch(new FullyQualified($nodeType), 'class');
$arrayNodes[] = new ArrayItem($classConstFetchNode);
$referencingClassConsts[] = $this->nodeFactory->createClassConstReference($nodeType);
}
return $this->betterStandardPrinter->print(new Array_($arrayNodes));
$array = $this->nodeFactory->createArray($referencingClassConsts);
return $this->betterStandardPrinter->print($array);
}
/**
* @param mixed[] $configuration
*/
private function createConfiguration(array $configuration): string
private function createRuleConfiguration(array $configuration): string
{
if ($configuration === []) {
return '';
$array = $this->nodeFactory->createArray($configuration);
return $this->betterStandardPrinter->print($array);
}
private function createConfigurationProperty(array $ruleConfiguration): string
{
$properties = [];
foreach (array_keys($ruleConfiguration) as $variable) {
$variable = ltrim($variable, '$');
$type = new ArrayType(new MixedType(), new MixedType());
$properties[] = $this->nodeFactory->createPrivatePropertyFromNameAndType($variable, $type);
}
$array = $this->nodeFactory->createArray($configuration);
return $this->betterStandardPrinter->print($properties);
}
return $this->betterStandardPrinter->prettyPrint([$array]);
private function createConfigurationConstructor(array $ruleConfiguration): string
{
$classMethod = $this->nodeFactory->createPublicMethod('__construct');
$assigns = [];
$params = [];
foreach ($ruleConfiguration as $variable => $values) {
$variable = ltrim($variable, '$');
$assign = $this->nodeFactory->createPropertyAssignment($variable);
$assigns[] = new Expression($assign);
if (is_array($values)) {
$type = new ArrayType(new MixedType(), new MixedType());
} elseif (is_string($values)) {
$type = new StringType();
} else {
throw new NotImplementedYetException();
}
$params[] = $this->nodeFactory->createParamFromNameAndType($variable, $type);
}
$classMethod->params = $params;
$classMethod->stmts = $assigns;
return $this->betterStandardPrinter->print($classMethod);
}
}

View File

@ -70,6 +70,7 @@ final class Configuration
* @var string[]
*/
private $source = [];
/**
* @var array
*/
@ -189,6 +190,14 @@ final class Configuration
return $this->extraFileName;
}
/**
* @return mixed[]
*/
public function getRuleConfiguration(): array
{
return $this->ruleConfiguration;
}
private function setName(string $name): void
{
if (! Strings::endsWith($name, 'Rector')) {
@ -197,12 +206,4 @@ final class Configuration
$this->name = $name;
}
/**
* @return mixed[]
*/
public function getRuleConfiguration(): array
{
return $this->ruleConfiguration;
}
}

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\_Package_\Rector\_Category_;
namespace Rector\__Package__\Rector\__Category__;
use PhpParser\Node;
use Rector\Core\Rector\AbstractRector;
@ -10,18 +10,22 @@ use Rector\Core\RectorDefinition\ConfiguredCodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
/**
_Source_
* @see \Rector\_Package_\Tests\Rector\_Category_\_Name_\_Name_Test
__Source__
* @see \Rector\__Package__\Tests\Rector\__Category__\__Name__\__Name__Test
*/
final class _Name_ extends AbstractRector
final class __Name__ extends AbstractRector
{
__ConfigurationProperty__
__ConfigurationConstructor__
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('_Description_', [
return new RectorDefinition('__Description__', [
new ConfiguredCodeSample(
_CodeBeforeExample_,
_CodeAfterExample_,
_RuleConfiguration_
__CodeBeforeExample__,
__CodeAfterExample__,
__RuleConfiguration__
)
]);
}
@ -31,11 +35,11 @@ final class _Name_ extends AbstractRector
*/
public function getNodeTypes(): array
{
return _NodeTypes_Php_;
return __NodeTypes_Php__;
}
/**
* @param _NodeTypes_Doc_ $node
* @param __NodeTypes_Doc__ $node
*/
public function refactor(Node $node): ?Node
{

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\_Package_\Rector\_Category_;
namespace Rector\__Package__\Rector\__Category__;
use PhpParser\Node;
use Rector\Core\Rector\AbstractRector;
@ -11,16 +11,16 @@ use Rector\Core\RectorDefinition\RectorDefinition;
/**
_Source_
* @see \Rector\_Package_\Tests\Rector\_Category_\_Name_\_Name_Test
* @see \Rector\__Package__\Tests\Rector\__Category__\__Name__\__Name__Test
*/
final class _Name_ extends AbstractRector
final class __Name__ extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('_Description_', [
return new RectorDefinition('__Description__', [
new CodeSample(
_CodeBeforeExample_,
_CodeAfterExample_
__CodeBeforeExample__,
__CodeAfterExample__
)
]);
}
@ -30,11 +30,11 @@ final class _Name_ extends AbstractRector
*/
public function getNodeTypes(): array
{
return _NodeTypes_Php_;
return __NodeTypes_Php__;
}
/**
* @param _NodeTypes_Doc_ $node
* @param __NodeTypes_Doc__ $node
*/
public function refactor(Node $node): ?Node
{

View File

@ -0,0 +1,13 @@
<?php
namespace Rector\__Package__\Tests\Rector\__Category__\__Name__\Fixture;
__CodeBefore__
?>
-----
<?php
namespace Rector\__Package__\Tests\Rector\__Category__\__Name__\Fixture;
__CodeAfter__
?>

View File

@ -0,0 +1,5 @@
<?php
namespace Rector\__Package__\Tests\Rector\__Category__\__Name__\Source;
__ExtraFileContent__

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Rector\__Package__\Tests\Rector\__Category__\__Name__;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
final class __Name__ExtraTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $inputFile, string $expectedExtraFileName, string $expectedExtraContentFilePath): void
{
$this->doTestFileInfo($inputFile);
$this->doTestExtraFile($expectedExtraFileName, $expectedExtraContentFilePath);
}
public function provideData(): \Iterator
{
yield [__DIR__ . '/Fixture/fixture.php.inc', '__ExtraFileName__', __DIR__ . '/Source/extra_file.php'];
}
protected function getRectorsWithConfiguration(): array
{
return [
\Rector\__Package__\Rector\__Category__\__Name__::class =>
__RuleConfiguration__
];
}
}

View File

@ -2,11 +2,11 @@
declare(strict_types=1);
namespace Rector\_Package_\Tests\Rector\_Category_\_Name_;
namespace Rector\__Package__\Tests\Rector\__Category__\__Name__;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
final class Configured_Name_Test extends AbstractRectorTestCase
final class __Name__Test extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
@ -24,8 +24,8 @@ final class Configured_Name_Test extends AbstractRectorTestCase
protected function getRectorsWithConfiguration(): array
{
return [
\Rector\_Package_\Rector\_Category_\_Name_::class =>
_RuleConfiguration_
\Rector\__Package__\Rector\__Category__\__Name__::class =>
__RuleConfiguration__
];
}
}

View File

@ -2,11 +2,11 @@
declare(strict_types=1);
namespace Rector\_Package_\Tests\Rector\_Category_\_Name_;
namespace Rector\__Package__\Tests\Rector\__Category__\__Name__;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
final class _Name_ExtraTest extends AbstractRectorTestCase
final class __Name__Test extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
@ -19,11 +19,11 @@ final class _Name_ExtraTest extends AbstractRectorTestCase
public function provideData(): \Iterator
{
yield [__DIR__ . '/Fixture/fixture.php.inc', '_ExtraFileName_', __DIR__ . '/Source/extra_file.php'];
yield [__DIR__ . '/Fixture/fixture.php.inc', '__ExtraFileName__', __DIR__ . '/Source/extra_file.php'];
}
protected function getRectorClass(): string
{
return \Rector\_Package_\Rector\_Category_\_Name_::class;
return \Rector\__Package__\Rector\__Category__\__Name__::class;
}
}

View File

@ -2,11 +2,11 @@
declare(strict_types=1);
namespace Rector\_Package_\Tests\Rector\_Category_\_Name_;
namespace Rector\__Package__\Tests\Rector\__Category__\__Name__;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
final class _Name_Test extends AbstractRectorTestCase
final class __Name__Test extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
@ -23,6 +23,6 @@ final class _Name_Test extends AbstractRectorTestCase
protected function getRectorClass(): string
{
return \Rector\_Package_\Rector\_Category_\_Name_::class;
return \Rector\__Package__\Rector\__Category__\__Name__::class;
}
}

View File

@ -1,13 +0,0 @@
<?php
namespace Rector\_Package_\Tests\Rector\_Category_\_Name_\Fixture;
_CodeBefore_
?>
-----
<?php
namespace Rector\_Package_\Tests\Rector\_Category_\_Name_\Fixture;
_CodeAfter_
?>

View File

@ -1,5 +0,0 @@
<?php
namespace Rector\_Package_\Tests\Rector\_Category_\_Name_\Source;
_ExtraFileContent_

View File

@ -53,7 +53,10 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$parameters->set(Option::AUTOLOAD_PATHS, [__DIR__ . '/compiler/src']);
$parameters->set(Option::EXCLUDE_PATHS, [
'/Fixture/', '/Source/', '/Expected/', __DIR__ . '/packages/doctrine-annotation-generated/src/*',
'/Fixture/', '/Source/', '/Expected/',
__DIR__ . '/packages/doctrine-annotation-generated/src/*',
// tempalte files
__DIR__ . '/packages/rector-generator/templates/*',
]);
$parameters->set(Option::EXCLUDE_RECTORS, [

View File

@ -82,6 +82,6 @@ PHP
}
}
return $node;
return null;
}
}

View File

@ -7,7 +7,7 @@ namespace Rector\Core\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\ConfiguredCodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
/**
@ -15,10 +15,22 @@ use Rector\Core\RectorDefinition\RectorDefinition;
*/
final class MethodCallToStaticCallRector extends AbstractRector
{
/**
* @var mixed[]
*/
private $methodCallsToStaticCalls;
public function __construct(array $methodCallsToStaticCalls = [])
{
dump($methodCallsToStaticCalls);
die;
$this->methodCallsToStaticCalls = $methodCallsToStaticCalls;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Change method call to desired static call', [
new CodeSample(
new ConfiguredCodeSample(
<<<'PHP'
final class SomeClass
{
@ -52,7 +64,12 @@ final class SomeClass
}
}
PHP
,
[
'$methodCallsToStaticCalls' => [
'AnotherDependency' => [['StaticCaller', 'anotherMethod']],
],
]
),
]);
}
@ -70,6 +87,13 @@ PHP
*/
public function refactor(Node $node): ?Node
{
if ($this->methodCallsToStaticCalls === []) {
return null;
}
// foreach ($this->methodCallsToStaticCalls as ) {
// }
// change the node
return $node;

View File

@ -24,8 +24,14 @@ final class MethodCallToStaticCallRectorTest extends AbstractRectorTestCase
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
protected function getRectorClass(): string
protected function getRectorsWithConfiguration(): array
{
return MethodCallToStaticCallRector::class;
return [
MethodCallToStaticCallRector::class => [
'$methodCallsToStaticCalls' => [
'AnotherDependency' => [['StaticCaller', 'anotherMethod']],
],
],
];
}
}