Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ parameters:
autoload_paths: []
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
nested_chain_method_call_limit: 30

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Configuration\Option;
use Rector\Core\PhpParser\Node\Manipulator\ClassManipulator;
use Rector\Core\PhpParser\Node\Manipulator\ClassMethodManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\PackageBuilder\Parameter\ParameterProvider;

/**
* @see https://www.php.net/manual/en/function.compact.php
Expand Down Expand Up @@ -52,12 +54,19 @@ final class RemoveUnusedParameterRector extends AbstractRector
*/
private $classMethodManipulator;

/**
* @var ParameterProvider
*/
private $parameterProvider;

public function __construct(
ClassManipulator $classManipulator,
ClassMethodManipulator $classMethodManipulator
ClassMethodManipulator $classMethodManipulator,
ParameterProvider $parameterProvider
) {
$this->classManipulator = $classManipulator;
$this->classMethodManipulator = $classMethodManipulator;
$this->parameterProvider = $parameterProvider;
}

public function getDefinition(): RectorDefinition
Expand Down Expand Up @@ -203,7 +212,8 @@ private function shouldSkip(ClassMethod $classMethod): bool
}

// 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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

?>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedParameterRector\Fixture;
namespace Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedParameterRector\FixtureOpenSource;

abstract class SkipAbstract
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 10 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,14 @@ final class Option
* @var string
*/
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';
}
27 changes: 27 additions & 0 deletions src/Testing/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@ abstract class AbstractRectorTestCase extends AbstractGenericRectorTestCase
*/
private $nodeScopeResolver;

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

protected function setUp(): void
{
$this->oldParameterValues = [];
$this->fixtureSplitter = new FixtureSplitter($this->getTempPath());

if ($this->provideConfig() !== '') {
Expand Down Expand Up @@ -117,6 +123,8 @@ protected function tearDown(): void
if ($this->getAutoImportNames() !== null) {
$this->setParameter(Option::AUTO_IMPORT_NAMES, false);
}

$this->restoreOldParameterValues();
}

protected function doTestFileWithoutAutoload(string $file): void
Expand Down Expand Up @@ -163,6 +171,12 @@ protected function getRectorInterface(): string
protected function setParameter(string $name, $value): void
{
$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);
}

Expand Down Expand Up @@ -282,4 +296,17 @@ private function doTestFileMatchesExpectedContent(
$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);
}
}
}