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

[Tests] Move from getRectorsWithConfiguration() to provideConfigFileInfo() to make test configs more intuitive #5468

Merged
merged 12 commits into from
Feb 9, 2021
Merged
45 changes: 2 additions & 43 deletions config/set/dead-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Rector\DeadCode\Rector\ClassMethod\RemoveDeadRecursiveClassMethodRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveDelegatingParentCallRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveEmptyClassMethodRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedParameterRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\DeadCode\Rector\Concat\RemoveConcatAutocastRector;
Expand Down Expand Up @@ -49,90 +50,48 @@

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(UnwrapFutureCompatibleIfFunctionExistsRector::class);

$services->set(UnwrapFutureCompatibleIfPhpVersionRector::class);

$services->set(RecastingRemovalRector::class);

$services->set(RemoveDeadStmtRector::class);

$services->set(RemoveDuplicatedArrayKeyRector::class);

$services->set(RemoveUnusedForeachKeyRector::class);

$services->set(RemoveParentCallWithoutParentRector::class);

$services->set(RemoveEmptyClassMethodRector::class);

$services->set(RemoveUnusedPrivatePropertyRector::class);

$services->set(RemoveDoubleAssignRector::class);

$services->set(RemoveUnusedParameterRector::class);

$services->set(SimplifyMirrorAssignRector::class);

$services->set(RemoveOverriddenValuesRector::class);

$services->set(RemoveUnusedPrivateConstantRector::class);

$services->set(RemoveUnusedPrivateMethodRector::class);

$services->set(RemoveCodeAfterReturnRector::class);

$services->set(RemoveDeadConstructorRector::class);

$services->set(RemoveDeadReturnRector::class);

$services->set(RemoveDeadIfForeachForRector::class);

$services->set(RemoveAndTrueRector::class);

$services->set(RemoveDefaultArgumentValueRector::class);

$services->set(RemoveConcatAutocastRector::class);

$services->set(SimplifyUselessVariableRector::class);

$services->set(RemoveDelegatingParentCallRector::class);

$services->set(RemoveDuplicatedInstanceOfRector::class);

$services->set(RemoveDuplicatedCaseInSwitchRector::class);

$services->set(RemoveUnusedDoctrineEntityMethodAndPropertyRector::class);

$services->set(RemoveSetterOnlyPropertyAndMethodCallRector::class);

$services->set(RemoveNullPropertyInitializationRector::class);

$services->set(RemoveUnreachableStatementRector::class);

$services->set(SimplifyIfElseWithSameContentRector::class);

$services->set(TernaryToBooleanOrFalseToBooleanAndRector::class);

$services->set(RemoveEmptyTestMethodRector::class);

$services->set(RemoveDeadTryCatchRector::class);

$services->set(RemoveUnusedClassConstantRector::class);

$services->set(RemoveUnusedVariableAssignRector::class);

$services->set(RemoveDuplicatedIfReturnRector::class);

$services->set(RemoveUnusedFunctionRector::class);

$services->set(RemoveUnusedNonEmptyArrayBeforeForeachRector::class);

$services->set(RemoveAssignOfVoidReturnFunctionRector::class);

$services->set(RemoveDeadRecursiveClassMethodRector::class);

$services->set(RemoveEmptyMethodCallRector::class);

$services->set(RemoveDeadConditionAboveReturnRector::class);
$services->set(RemoveUnusedConstructorParamRector::class);
};
1 change: 1 addition & 0 deletions config/set/downgrade-php71.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

// skip classes used in PHP DocBlocks, like in /** @var \Some\Class */ [default: true]
$parameters->set(Option::IMPORT_DOC_BLOCKS, false);

$services->set(DowngradeClassConstantVisibilityRector::class);
$services->set(DowngradePipeToMultiCatchExceptionRector::class);
$services->set(SymmetricArrayDestructuringToListRector::class);
Expand Down
1 change: 1 addition & 0 deletions config/set/downgrade-php72.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@

// skip classes used in PHP DocBlocks, like in /** @var \Some\Class */ [default: true]
$parameters->set(Option::IMPORT_DOC_BLOCKS, false);

$services->set(DowngradeParameterTypeWideningRector::class);
};
1 change: 1 addition & 0 deletions config/set/downgrade-php73.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

// skip classes used in PHP DocBlocks, like in /** @var \Some\Class */ [default: true]
$parameters->set(Option::IMPORT_DOC_BLOCKS, false);

$services->set(DowngradeTrailingCommasInFunctionCallsRector::class);
$services->set(SetCookieOptionsArrayToArgumentsRector::class);
};
1 change: 1 addition & 0 deletions config/set/downgrade-php74.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

// skip classes used in PHP DocBlocks, like in /** @var \Some\Class */ [default: true]
$parameters->set(Option::IMPORT_DOC_BLOCKS, false);

$services->set(DowngradeFreadFwriteFalsyToNegationRector::class);
$services->set(DowngradeReturnSelfTypeDeclarationRector::class);
};
1 change: 1 addition & 0 deletions config/set/downgrade-php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

// skip classes used in PHP DocBlocks, like in /** @var \Some\Class */ [default: true]
$parameters->set(Option::IMPORT_DOC_BLOCKS, false);

$services->set(DowngradePropertyPromotionRector::class);
$services->set(DowngradeNonCapturingCatchesRector::class);
$services->set(DowngradeMatchToSwitchRector::class);
Expand Down
6 changes: 3 additions & 3 deletions config/set/guzzle50.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Transform\Rector\FuncCall\FuncCallToMethodCallRector;
use Rector\Transform\Rector\StaticCall\StaticCallToFuncCallRector;
use Rector\Transform\ValueObject\FuncNameToMethodCallName;
use Rector\Transform\ValueObject\FuncCallToMethodCall;
use Rector\Transform\ValueObject\StaticCallToFuncCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;
Expand All @@ -19,8 +19,8 @@
$services->set(FluentChainMethodCallToNormalMethodCallRector::class);

$configuration = [
new FuncNameToMethodCallName('GuzzleHttp\json_decode', 'GuzzleHttp\Utils', 'jsonDecode'),
new FuncNameToMethodCallName('GuzzleHttp\get_path', 'GuzzleHttp\Utils', 'getPath'),
new FuncCallToMethodCall('GuzzleHttp\json_decode', 'GuzzleHttp\Utils', 'jsonDecode'),
new FuncCallToMethodCall('GuzzleHttp\get_path', 'GuzzleHttp\Utils', 'getPath'),
];

$services->set(FuncCallToMethodCallRector::class)
Expand Down
32 changes: 16 additions & 16 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -9544,17 +9544,17 @@ Move array argument from tests into data provider [configurable]
- class: `Rector\PHPUnit\Rector\Class_\ArrayArgumentInTestToDataProviderRector`

```php
use Rector\PHPUnit\Rector\Class_\ArrayArgumentInTestToDataProviderRector;
use Rector\PHPUnit\Rector\Class_\ArrayArgumentToDataProviderRector;
use Rector\PHPUnit\ValueObject\ArrayArgumentToDataProvider;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(ArrayArgumentInTestToDataProviderRector::class)
$services->set(ArrayArgumentToDataProviderRector::class)
->call('configure', [[
ArrayArgumentInTestToDataProviderRector::ARRAY_ARGUMENTS_TO_DATA_PROVIDERS => ValueObjectInliner::inline([
ArrayArgumentToDataProviderRector::ARRAY_ARGUMENTS_TO_DATA_PROVIDERS => ValueObjectInliner::inline([
new ArrayArgumentToDataProvider('PHPUnit\Framework\TestCase', 'doTestMultiple', 'doTestSingle', 'number'),
]),
]]);
Expand Down Expand Up @@ -13868,7 +13868,7 @@ In case you have accidentally removed use imports but code still contains partia

```php
use Rector\Restoration\Rector\Namespace_\CompleteImportForPartialAnnotationRector;
use Rector\Restoration\ValueObject\UseWithAlias;
use Rector\Restoration\ValueObject\CompleteImportForPartialAnnotation;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

Expand All @@ -13878,7 +13878,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(CompleteImportForPartialAnnotationRector::class)
->call('configure', [[
CompleteImportForPartialAnnotationRector::USE_IMPORTS_TO_RESTORE => ValueObjectInliner::inline([
new UseWithAlias('Doctrine\ORM\Mapping', 'ORM'),
new CompleteImportForPartialAnnotation('Doctrine\ORM\Mapping', 'ORM'),
]),
]]);
};
Expand Down Expand Up @@ -15411,17 +15411,17 @@ Replaces constant by value
- class: `Rector\Transform\Rector\ClassConstFetch\ClassConstFetchToStringRector`

```php
use Rector\Transform\Rector\ClassConstFetch\ClassConstFetchToStringRector;
use Rector\Transform\Rector\ClassConstFetch\ClassConstFetchToValueRector;
use Rector\Transform\ValueObject\ClassConstFetchToValue;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(ClassConstFetchToStringRector::class)
$services->set(ClassConstFetchToValueRector::class)
->call('configure', [[
ClassConstFetchToStringRector::CLASS_CONST_FETCHES_TO_VALUES => ValueObjectInliner::inline([
ClassConstFetchToValueRector::CLASS_CONST_FETCHES_TO_VALUES => ValueObjectInliner::inline([
new ClassConstFetchToValue('Nette\Configurator', 'DEVELOPMENT', 'development'),
new ClassConstFetchToValue('Nette\Configurator', 'PRODUCTION', 'production'),
]),
Expand Down Expand Up @@ -15493,7 +15493,7 @@ Turns defined function calls to local method calls.

```php
use Rector\Transform\Rector\FuncCall\FuncCallToMethodCallRector;
use Rector\Transform\ValueObject\FuncNameToMethodCallName;
use Rector\Transform\ValueObject\FuncCallToMethodCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

Expand All @@ -15503,7 +15503,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(FuncCallToMethodCallRector::class)
->call('configure', [[
FuncCallToMethodCallRector::FUNC_CALL_TO_CLASS_METHOD_CALL => ValueObjectInliner::inline([
new FuncNameToMethodCallName('view', 'Namespaced\SomeRenderer', 'render'),
new FuncCallToMethodCall('view', 'Namespaced\SomeRenderer', 'render'),
]),
]]);
};
Expand Down Expand Up @@ -16488,7 +16488,7 @@ Turns defined `__isset`/`__unset` calls to specific method calls.

```php
use Rector\Transform\Rector\Isset_\UnsetAndIssetToMethodCallRector;
use Rector\Transform\ValueObject\IssetUnsetToMethodCall;
use Rector\Transform\ValueObject\UnsetAndIssetToMethodCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

Expand All @@ -16498,7 +16498,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(UnsetAndIssetToMethodCallRector::class)
->call('configure', [[
UnsetAndIssetToMethodCallRector::ISSET_UNSET_TO_METHOD_CALL => ValueObjectInliner::inline([
new IssetUnsetToMethodCall('SomeContainer', 'hasService', 'removeService'),
new UnsetAndIssetToMethodCall('SomeContainer', 'hasService', 'removeService'),
]),
]]);
};
Expand All @@ -16516,7 +16516,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {

```php
use Rector\Transform\Rector\Isset_\UnsetAndIssetToMethodCallRector;
use Rector\Transform\ValueObject\IssetUnsetToMethodCall;
use Rector\Transform\ValueObject\UnsetAndIssetToMethodCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

Expand All @@ -16526,7 +16526,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(UnsetAndIssetToMethodCallRector::class)
->call('configure', [[
UnsetAndIssetToMethodCallRector::ISSET_UNSET_TO_METHOD_CALL => ValueObjectInliner::inline([
new IssetUnsetToMethodCall('SomeContainer', 'hasService', 'removeService'),
new UnsetAndIssetToMethodCall('SomeContainer', 'hasService', 'removeService'),
]),
]]);
};
Expand Down Expand Up @@ -16967,7 +16967,7 @@ Change visibility of constant from parent class.

```php
use Rector\Visibility\Rector\ClassConst\ChangeConstantVisibilityRector;
use Rector\Visibility\ValueObject\ClassConstantVisibilityChange;
use Rector\Visibility\ValueObject\ChangeConstantVisibility;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

Expand All @@ -16977,7 +16977,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(ChangeConstantVisibilityRector::class)
->call('configure', [[
ChangeConstantVisibilityRector::CLASS_CONSTANT_VISIBILITY_CHANGES => ValueObjectInliner::inline([
new ClassConstantVisibilityChange('ParentObject', 'SOME_CONSTANT', 2),
new ChangeConstantVisibility('ParentObject', 'SOME_CONSTANT', 2),
]),
]]);
};
Expand Down
12 changes: 11 additions & 1 deletion packages/node-removal/src/NodeRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
Expand Down Expand Up @@ -89,8 +90,17 @@ public function removeStmt(Node $node, int $key): void
unset($node->stmts[$key]);
}

public function removeParam(ClassMethod $classMethod, int $key): void
/**
* @param int|Param $keyOrParam
*/
public function removeParam(ClassMethod $classMethod, $keyOrParam): void
{
if ($keyOrParam instanceof Param) {
$key = $keyOrParam->getAttribute(AttributeKey::PARAMETER_POSITION);
} else {
$key = $keyOrParam;
}

if ($classMethod->params === null) {
throw new ShouldNotHappenException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Rector\__Package__\Tests\Rector\__Category__\__Name__;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class __Name__Test extends AbstractRectorTestCase
{
Expand All @@ -22,14 +23,8 @@ final class __Name__Test extends AbstractRectorTestCase
yield [new \Symplify\SmartFileSystem\SmartFileInfo(__DIR__ . '/Fixture/some_class.php.inc'), '__ExtraFileName__', __DIR__ . '/Source/extra_file.php'];
}

/**
* @return mixed[]
*/
protected function getRectorsWithConfiguration(): array
protected function provideConfigFileInfo(): ?\Symplify\SmartFileSystem\SmartFileInfo
{
return [
\Rector\__Package__\Rector\__Category__\__Name__::class =>
__TestRuleConfiguration__
];
return new \Symplify\SmartFileSystem\SmartFileInfo(__DIR__ . '/configured_rule.php');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ final class __Name__Test extends AbstractRectorTestCase
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* @return mixed[]
*/
protected function getRectorsWithConfiguration(): array
protected function provideConfigFileInfo(): ?\Symplify\SmartFileSystem\SmartFileInfo
{
return [
\Rector\__Package__\Rector\__Category__\__Name__::class =>
__TestRuleConfiguration__
];
return new \Symplify\SmartFileSystem\SmartFileInfo(__DIR__ . '/configured_rule.php');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ final class WhateverRectorTest extends AbstractRectorTestCase
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* @return mixed[]
*/
protected function getRectorsWithConfiguration(): array
protected function provideConfigFileInfo(): ?\Symplify\SmartFileSystem\SmartFileInfo
{
return [
\Rector\ModeratePackage\Rector\MethodCall\WhateverRector::class =>
[\Rector\ModeratePackage\Rector\MethodCall\WhateverRector::CLASS_TYPE_TO_METHOD_NAME => ['SomeClass' => 'configure']]
];
return new \Symplify\SmartFileSystem\SmartFileInfo(__DIR__ . '/configured_rule.php');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ final class WhateverRectorTest extends AbstractRectorTestCase
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* @return mixed[]
*/
protected function getRectorsWithConfiguration(): array
protected function provideConfigFileInfo(): ?\Symplify\SmartFileSystem\SmartFileInfo
{
return [
\Utils\Rector\Rector\MethodCall\WhateverRector::class =>
[\Utils\Rector\Rector\MethodCall\WhateverRector::CLASS_TYPE_TO_METHOD_NAME => ['SomeClass' => 'configure']]
];
return new \Symplify\SmartFileSystem\SmartFileInfo(__DIR__ . '/configured_rule.php');
}
}
Loading