mirror of
https://github.com/rectorphp/rector.git
synced 2024-07-11 03:53:31 +00:00
add project_type
This commit is contained in:
parent
f71c896a92
commit
3e7e3bf7dc
|
@ -14,6 +14,9 @@ parameters:
|
||||||
autoload_paths: []
|
autoload_paths: []
|
||||||
rector_recipe: []
|
rector_recipe: []
|
||||||
|
|
||||||
|
# this helps to separate opened 3rd party code vs private code approach (e.g. use of public constants)
|
||||||
|
project_type: "proprietary" # or "open-source"
|
||||||
|
|
||||||
# lower for performance; higher to prevent bugs with fluent interfaces like https://github.com/rectorphp/rector/issues/1646, or https://github.com/rectorphp/rector/issues/2444
|
# lower for performance; higher to prevent bugs with fluent interfaces like https://github.com/rectorphp/rector/issues/1646, or https://github.com/rectorphp/rector/issues/2444
|
||||||
nested_chain_method_call_limit: 30
|
nested_chain_method_call_limit: 30
|
||||||
|
|
||||||
|
|
|
@ -8,12 +8,14 @@ use PhpParser\Node;
|
||||||
use PhpParser\Node\Param;
|
use PhpParser\Node\Param;
|
||||||
use PhpParser\Node\Stmt\Class_;
|
use PhpParser\Node\Stmt\Class_;
|
||||||
use PhpParser\Node\Stmt\ClassMethod;
|
use PhpParser\Node\Stmt\ClassMethod;
|
||||||
|
use Rector\Core\Configuration\Option;
|
||||||
use Rector\Core\PhpParser\Node\Manipulator\ClassManipulator;
|
use Rector\Core\PhpParser\Node\Manipulator\ClassManipulator;
|
||||||
use Rector\Core\PhpParser\Node\Manipulator\ClassMethodManipulator;
|
use Rector\Core\PhpParser\Node\Manipulator\ClassMethodManipulator;
|
||||||
use Rector\Core\Rector\AbstractRector;
|
use Rector\Core\Rector\AbstractRector;
|
||||||
use Rector\Core\RectorDefinition\CodeSample;
|
use Rector\Core\RectorDefinition\CodeSample;
|
||||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||||
|
use Symplify\PackageBuilder\Parameter\ParameterProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see https://www.php.net/manual/en/function.compact.php
|
* @see https://www.php.net/manual/en/function.compact.php
|
||||||
|
@ -52,12 +54,19 @@ final class RemoveUnusedParameterRector extends AbstractRector
|
||||||
*/
|
*/
|
||||||
private $classMethodManipulator;
|
private $classMethodManipulator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ParameterProvider
|
||||||
|
*/
|
||||||
|
private $parameterProvider;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
ClassManipulator $classManipulator,
|
ClassManipulator $classManipulator,
|
||||||
ClassMethodManipulator $classMethodManipulator
|
ClassMethodManipulator $classMethodManipulator,
|
||||||
|
ParameterProvider $parameterProvider
|
||||||
) {
|
) {
|
||||||
$this->classManipulator = $classManipulator;
|
$this->classManipulator = $classManipulator;
|
||||||
$this->classMethodManipulator = $classMethodManipulator;
|
$this->classMethodManipulator = $classMethodManipulator;
|
||||||
|
$this->parameterProvider = $parameterProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDefinition(): RectorDefinition
|
public function getDefinition(): RectorDefinition
|
||||||
|
@ -203,7 +212,8 @@ PHP
|
||||||
}
|
}
|
||||||
|
|
||||||
// skip as possible contract for 3rd party
|
// skip as possible contract for 3rd party
|
||||||
if ($classMethod->isAbstract()) {
|
$projetType = $this->parameterProvider->provideParameter(Option::PROJECT_TYPE);
|
||||||
|
if ($classMethod->isAbstract() && $projetType === Option::PROJECT_TYPE_OPEN_SOURCE) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedParameterRector\Fixture;
|
||||||
|
|
||||||
|
abstract class AbstractClass
|
||||||
|
{
|
||||||
|
public abstract function foo(string $value);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class ChildClass extends AbstractClass
|
||||||
|
{
|
||||||
|
public function foo(string $value)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
-----
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedParameterRector\Fixture;
|
||||||
|
|
||||||
|
abstract class AbstractClass
|
||||||
|
{
|
||||||
|
public abstract function foo();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class ChildClass extends AbstractClass
|
||||||
|
{
|
||||||
|
public function foo()
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedParameterRector\Fixture;
|
namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedParameterRector\FixtureOpenSource;
|
||||||
|
|
||||||
abstract class SkipAbstract
|
abstract class SkipAbstract
|
||||||
{
|
{
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedParameterRector;
|
||||||
|
|
||||||
|
use Iterator;
|
||||||
|
use Rector\Core\Configuration\Option;
|
||||||
|
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||||
|
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedParameterRector;
|
||||||
|
|
||||||
|
final class OpenSourceRectorTest extends AbstractRectorTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @dataProvider provideData()
|
||||||
|
*/
|
||||||
|
public function test(string $file): void
|
||||||
|
{
|
||||||
|
$this->setParameter(Option::PROJECT_TYPE, Option::PROJECT_TYPE_OPEN_SOURCE);
|
||||||
|
$this->doTestFile($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideData(): Iterator
|
||||||
|
{
|
||||||
|
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureOpenSource');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getRectorClass(): string
|
||||||
|
{
|
||||||
|
return RemoveUnusedParameterRector::class;
|
||||||
|
}
|
||||||
|
}
|
|
@ -75,4 +75,14 @@ final class Option
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public const OPTION_OUTPUT_FILE = 'output-file';
|
public const OPTION_OUTPUT_FILE = 'output-file';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public const PROJECT_TYPE = 'project_type';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public const PROJECT_TYPE_OPEN_SOURCE = 'open-source';
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,8 +55,14 @@ abstract class AbstractRectorTestCase extends AbstractGenericRectorTestCase
|
||||||
*/
|
*/
|
||||||
private $nodeScopeResolver;
|
private $nodeScopeResolver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var mixed[]
|
||||||
|
*/
|
||||||
|
private $oldParameterValues = [];
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
|
$this->oldParameterValues = [];
|
||||||
$this->fixtureSplitter = new FixtureSplitter($this->getTempPath());
|
$this->fixtureSplitter = new FixtureSplitter($this->getTempPath());
|
||||||
|
|
||||||
if ($this->provideConfig() !== '') {
|
if ($this->provideConfig() !== '') {
|
||||||
|
@ -117,6 +123,8 @@ abstract class AbstractRectorTestCase extends AbstractGenericRectorTestCase
|
||||||
if ($this->getAutoImportNames() !== null) {
|
if ($this->getAutoImportNames() !== null) {
|
||||||
$this->setParameter(Option::AUTO_IMPORT_NAMES, false);
|
$this->setParameter(Option::AUTO_IMPORT_NAMES, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->restoreOldParameterValues();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function doTestFileWithoutAutoload(string $file): void
|
protected function doTestFileWithoutAutoload(string $file): void
|
||||||
|
@ -163,6 +171,12 @@ abstract class AbstractRectorTestCase extends AbstractGenericRectorTestCase
|
||||||
protected function setParameter(string $name, $value): void
|
protected function setParameter(string $name, $value): void
|
||||||
{
|
{
|
||||||
$parameterProvider = self::$container->get(ParameterProvider::class);
|
$parameterProvider = self::$container->get(ParameterProvider::class);
|
||||||
|
|
||||||
|
if (! in_array($name, [Option::PHP_VERSION_FEATURES, Option::AUTO_IMPORT_NAMES], true)) {
|
||||||
|
$oldParameterValue = $parameterProvider->provideParameter($name);
|
||||||
|
$this->oldParameterValues[$name] = $oldParameterValue;
|
||||||
|
}
|
||||||
|
|
||||||
$parameterProvider->changeParameter($name, $value);
|
$parameterProvider->changeParameter($name, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -282,4 +296,17 @@ abstract class AbstractRectorTestCase extends AbstractGenericRectorTestCase
|
||||||
$this->assertStringMatchesFormat($expectedFileContent, $changedContent, 'Caused by ' . $fixtureFile);
|
$this->assertStringMatchesFormat($expectedFileContent, $changedContent, 'Caused by ' . $fixtureFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function restoreOldParameterValues(): void
|
||||||
|
{
|
||||||
|
if ($this->oldParameterValues === []) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$parameterProvider = self::$container->get(ParameterProvider::class);
|
||||||
|
|
||||||
|
foreach ($this->oldParameterValues as $name => $oldParameterValue) {
|
||||||
|
$parameterProvider->changeParameter($name, $oldParameterValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user