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
10 changes: 0 additions & 10 deletions config/level/nette/nette20.yml

This file was deleted.

5 changes: 0 additions & 5 deletions config/level/nette/nette21.yml

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,33 +1,53 @@
<?php declare(strict_types=1);

namespace Rector\Nette\Rector\Bootstrap;
namespace Rector\Rector\Constant;

use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;

final class RemoveConfiguratorConstantsRector extends AbstractRector
final class RenameClassConstantsUseToStringsRector extends AbstractRector
{
/**
* @var NodeFactory
*/
private $nodeFactory;

public function __construct(NodeFactory $nodeFactory)
{
/**
* @var string
*/
private $class;

/**
* @var string[]
*/
private $oldConstantToNewValue = [];

/**
* @param string[] $oldConstantToNewValue
*/
public function __construct(
NodeFactory $nodeFactory,
string $class = 'Nette\Configurator',
array $oldConstantToNewValue = [
'DEVELOPMENT' => 'development',
'PRODUCTION' => 'production',
]
) {
$this->nodeFactory = $nodeFactory;
$this->class = $class;
$this->oldConstantToNewValue = $oldConstantToNewValue;
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Turns properties with @inject to private properties and constructor injection', [
return new RectorDefinition('Replaces constant by value', [
new CodeSample('$value === Nette\Configurator::DEVELOPMENT', '$value === "development"'),
]);
}
Expand All @@ -40,24 +60,21 @@ public function isCandidate(Node $node): bool

$className = $this->getClassNameFromClassConstFetch($node);

if ($className !== 'Nette\Configurator') {
if ($className !== $this->class) {
return false;
}

return in_array((string) $node->name, ['DEVELOPMENT', 'PRODUCTION'], true);
return in_array((string) $node->name, array_keys($this->oldConstantToNewValue), true);
}

/**
* @param ClassConstFetch $classConstFetchNode
*/
public function refactor(Node $classConstFetchNode): ?Node
{
/** @var Identifier $constantName */
$constantName = $classConstFetchNode->name;

$originalConstantValue = $constantName->toLowerString();
$newValue = $this->oldConstantToNewValue[(string) $classConstFetchNode->name];

return $this->nodeFactory->createString($originalConstantValue);
return $this->nodeFactory->createString($newValue);
}

private function getClassNameFromClassConstFetch(ClassConstFetch $classConstFetchNode): string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php declare(strict_types=1);

namespace Rector\Nette\Tests\Rector\Bootstrap\RemoveConfiguratorConstantsRector;
namespace Rector\Tests\Rector\Constant\RenameClassConstantsUseToStringsRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/**
* @covers \Rector\Nette\Rector\Bootstrap\RemoveConfiguratorConstantsRector
* @covers \Rector\Rector\Constant\RenameClassConstantsUseToStringsRector
*/
final class RemoveConfiguratorConstantsRectorTest extends AbstractRectorTestCase
final class RenameClassConstantsUseToStringsRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideWrongToFixedFiles()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
services:
Rector\Rector\Constant\RenameClassConstantsUseToStringsRector: ~