Skip to content

Commit

Permalink
use more configs
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 9, 2021
1 parent 49a3725 commit 495b778
Show file tree
Hide file tree
Showing 39 changed files with 274 additions and 247 deletions.
2 changes: 1 addition & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -13965,7 +13965,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {

$services->set(InferParamFromClassMethodReturnRector::class)
->call('configure', [[
InferParamFromClassMethodReturnRector::INFER_PARAMS_FROM_CLASS_METHOD_RETURNS => ValueObjectInliner::inline([
InferParamFromClassMethodReturnRector::PARAM_FROM_CLASS_METHOD_RETURNS => ValueObjectInliner::inline([
new InferParamFromClassMethodReturn('SomeClass', 'process', 'getNodeTypes'),
]),
]]);
Expand Down
53 changes: 4 additions & 49 deletions packages/testing/src/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ abstract class AbstractRectorTestCase extends AbstractKernelTestCase
*/
private $autoloadTestFixture = true;

/**
* @var mixed[]
*/
private $oldParameterValues = [];

/**
* @var BetterStandardPrinter
*/
Expand Down Expand Up @@ -167,18 +162,6 @@ protected function setUp(): void
$this->nodeScopeResolver = $this->getService(NodeScopeResolver::class);

$this->configurePhpVersionFeatures();

$this->oldParameterValues = [];
}

protected function tearDown(): void
{
$this->restoreOldParameterValues();

// restore PHP version if changed
if ($this->getPhpVersion() !== self::PHP_VERSION_UNDEFINED) {
$this->setParameter(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_10);
}
}

protected function getRectorClass(): string
Expand Down Expand Up @@ -211,25 +194,10 @@ protected function yieldFilesFromDirectory(string $directory, string $suffix = '
return StaticFixtureFinder::yieldDirectory($directory, $suffix);
}

/**
* @param mixed $value
*/
protected function setParameter(string $name, $value): void
{
$parameterProvider = $this->getService(ParameterProvider::class);

if ($name !== Option::PHP_VERSION_FEATURES) {
$oldParameterValue = $parameterProvider->provideParameter($name);
$this->oldParameterValues[$name] = $oldParameterValue;
}

$parameterProvider->changeParameter($name, $value);
}

protected function getPhpVersion(): int
{
// to be implemented
return self::PHP_VERSION_UNDEFINED;
// default value to be implemented for testing lower versions
return PhpVersion::PHP_10;
}

protected function doTestFileInfoWithoutAutoload(SmartFileInfo $fileInfo): void
Expand Down Expand Up @@ -379,20 +347,7 @@ private function configurePhpVersionFeatures(): void
return;
}

$this->setParameter(Option::PHP_VERSION_FEATURES, $this->getPhpVersion());
}

private function restoreOldParameterValues(): void
{
if ($this->oldParameterValues === []) {
return;
}

$parameterProvider = $this->getService(ParameterProvider::class);

foreach ($this->oldParameterValues as $name => $oldParameterValue) {
$parameterProvider->changeParameter($name, $oldParameterValue);
}
$this->parameterProvider->changeParameter(Option::PHP_VERSION_FEATURES, $this->getPhpVersion());
}

private function ensureRectorClassIsValid(string $rectorClass, string $methodName): void
Expand All @@ -418,7 +373,7 @@ private function doTestFileMatchesExpectedContent(
SmartFileInfo $fixtureFileInfo,
array $extraFiles = []
): void {
$this->setParameter(Option::SOURCE, [$originalFileInfo->getRealPath()]);
$this->parameterProvider->changeParameter(Option::SOURCE, [$originalFileInfo->getRealPath()]);

if (! Strings::endsWith($originalFileInfo->getFilename(), '.blade.php') && in_array(
$originalFileInfo->getSuffix(),
Expand Down
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
]);
$services->set(InferParamFromClassMethodReturnRector::class)
->call('configure', [[
InferParamFromClassMethodReturnRector::INFER_PARAMS_FROM_CLASS_METHOD_RETURNS => $configuration,
InferParamFromClassMethodReturnRector::PARAM_FROM_CLASS_METHOD_RETURNS => $configuration,
]]);

$services->set(PreferThisOrSelfMethodCallRector::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Iterator;
use Rector\CodeQuality\Rector\Return_\SimplifyUselessVariableRector;
use Rector\Core\Configuration\Option;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

Expand All @@ -22,8 +21,6 @@ final class SimplifyUselessVariableRectorTest extends AbstractRectorTestCase
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->setParameter(Option::AUTO_IMPORT_NAMES, true);

$this->doTestFileInfo($fileInfo);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Rector\CodingStyle\Tests\Rector\FuncCall\PreslashSimpleFunctionRector;

use Iterator;
use Rector\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector;
use Rector\Core\Configuration\Option;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

Expand All @@ -17,7 +15,6 @@ final class AutoImportTest extends AbstractRectorTestCase
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->setParameter(Option::AUTO_IMPORT_NAMES, true);
$this->doTestFileInfo($fileInfo);
}

Expand All @@ -26,8 +23,8 @@ public function provideData(): Iterator
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureAutoImport');
}

protected function getRectorClass(): string
protected function provideConfigFileInfo(): ?SmartFileInfo
{
return PreslashSimpleFunctionRector::class;
return new SmartFileInfo(__DIR__ . '/config/auto_import.php');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Rector\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector;
use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, true);

$services = $containerConfigurator->services();
$services->set(PreslashSimpleFunctionRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector;

use Iterator;
use Rector\Core\Configuration\Option;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

Expand All @@ -20,9 +18,6 @@ final class DocBlockRectorTest extends AbstractRectorTestCase
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->setParameter(Option::AUTO_IMPORT_NAMES, true);
$this->setParameter(Option::IMPORT_DOC_BLOCKS, true);

$this->doTestFileInfo($fileInfo);
}

Expand All @@ -31,9 +26,8 @@ public function provideData(): Iterator
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureDocBlock');
}

protected function getRectorClass(): string
protected function provideConfigFileInfo(): ?SmartFileInfo
{
// here can be any Rector rule, as we're testing \Rector\PostRector\Rector\NameImportingPostRector in the full Retcor life cycle
return RenameClassRector::class;
return new SmartFileInfo(__DIR__ . '/config/import_doc_block_config.php');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
namespace Rector\CodingStyle\Tests\Rector\Namespace_\ImportFullyQualifiedNamesRector;

use Iterator;
use Rector\Core\Configuration\Option;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

Expand All @@ -20,9 +18,6 @@ final class ImportRootNamespaceClassesDisabledTest extends AbstractRectorTestCas
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->setParameter(Option::AUTO_IMPORT_NAMES, true);
$this->setParameter(Option::IMPORT_SHORT_CLASSES, false);

$this->doTestFileInfo($fileInfo);
}

Expand All @@ -34,9 +29,8 @@ public function provideData(): iterable
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureRoot');
}

protected function getRectorClass(): string
protected function provideConfigFileInfo(): ?SmartFileInfo
{
// the must be any rector class to run
return RenameClassRector::class;
return new SmartFileInfo(__DIR__ . '/config/not_import_short_classes.php');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
$parameters->set(Option::IMPORT_DOC_BLOCKS, true);

$services = $containerConfigurator->services();
$services->set(RenameClassRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
$parameters->set(Option::IMPORT_SHORT_CLASSES, false);

$services = $containerConfigurator->services();
$services->set(RenameClassRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
namespace Rector\DeadCode\Tests\Rector\ClassConst\RemoveUnusedClassConstantRector;

use Iterator;
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\ProjectType;
use Rector\DeadCode\Rector\ClassConst\RemoveUnusedClassConstantRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

Expand All @@ -18,7 +15,6 @@ final class OpenSourceRectorTest extends AbstractRectorTestCase
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->setParameter(Option::PROJECT_TYPE, ProjectType::OPEN_SOURCE);
$this->doTestFileInfo($fileInfo);
}

Expand All @@ -27,8 +23,8 @@ public function provideData(): Iterator
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureOpenSource');
}

protected function getRectorClass(): string
protected function provideConfigFileInfo(): ?SmartFileInfo
{
return RemoveUnusedClassConstantRector::class;
return new SmartFileInfo(__DIR__ . '/config/project_open_source.php');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\ProjectType;
use Rector\DeadCode\Rector\ClassConst\RemoveUnusedClassConstantRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PROJECT_TYPE, ProjectType::OPEN_SOURCE);

$services = $containerConfigurator->services();
$services->set(RemoveUnusedClassConstantRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedParameterRector;

use Iterator;
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\ProjectType;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedParameterRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

Expand All @@ -18,7 +15,6 @@ final class OpenSourceRectorTest extends AbstractRectorTestCase
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->setParameter(Option::PROJECT_TYPE, ProjectType::OPEN_SOURCE);
$this->doTestFileInfo($fileInfo);
}

Expand All @@ -27,8 +23,8 @@ public function provideData(): Iterator
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureOpenSource');
}

protected function getRectorClass(): string
protected function provideConfigFileInfo(): ?SmartFileInfo
{
return RemoveUnusedParameterRector::class;
return new SmartFileInfo(__DIR__ . '/config/project_open_source.php');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\ProjectType;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedParameterRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PROJECT_TYPE, ProjectType::OPEN_SOURCE);

$services = $containerConfigurator->services();
$services->set(RemoveUnusedParameterRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedPublicMethodRector;

use Iterator;
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\ProjectType;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPublicMethodRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

Expand All @@ -18,7 +15,6 @@ final class OpenSourceRectorTest extends AbstractRectorTestCase
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->setParameter(Option::PROJECT_TYPE, ProjectType::OPEN_SOURCE);
$this->doTestFileInfo($fileInfo);
}

Expand All @@ -27,8 +23,8 @@ public function provideData(): Iterator
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureOpenSource');
}

protected function getRectorClass(): string
protected function provideConfigFileInfo(): ?SmartFileInfo
{
return RemoveUnusedPublicMethodRector::class;
return new SmartFileInfo(__DIR__ . '/config/projet_open_source.php');
}
}
Loading

0 comments on commit 495b778

Please sign in to comment.