rector/rules/php55/src/Rector/String_/StringClassNameToClassConstantRector.php

156 lines
3.7 KiB
PHP
Raw Normal View History

2019-10-13 05:59:52 +00:00
<?php
declare(strict_types=1);
namespace Rector\Php55\Rector\String_;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
2020-05-03 19:30:01 +00:00
use Rector\Core\Util\StaticRectorStrings;
use Rector\Core\ValueObject\PhpVersionFeature;
2019-09-10 06:21:20 +00:00
use Rector\NodeTypeResolver\ClassExistenceStaticHelper;
use Rector\NodeTypeResolver\Node\AttributeKey;
use ReflectionClass;
/**
* @see https://wiki.php.net/rfc/class_name_scalars
* @see https://github.com/symfony/symfony/blob/2.8/UPGRADE-2.8.md#form
2020-05-03 19:30:01 +00:00
*
* @see \Rector\Php55\Tests\Rector\String_\StringClassNameToClassConstantRector\StringClassNameToClassConstantRectorTest
*/
final class StringClassNameToClassConstantRector extends AbstractRector
{
/**
* @var string[]
*/
private $classesToSkip = [];
/**
* @var string[]
*/
private $sensitiveExistingClasses = [];
/**
* @var string[]
*/
private $sensitiveNonExistingClasses = [];
/**
* @param string[] $classesToSkip
*/
public function __construct(array $classesToSkip = [
// can be string
'Error',
'Exception',
])
{
$this->classesToSkip = $classesToSkip;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Replace string class names by <class>::class constant', [
new CodeSample(
2019-09-18 06:14:35 +00:00
<<<'PHP'
class AnotherClass
{
}
class SomeClass
{
public function run()
{
return 'AnotherClass';
}
}
2019-09-18 06:14:35 +00:00
PHP
,
2019-09-18 06:14:35 +00:00
<<<'PHP'
class AnotherClass
{
}
class SomeClass
{
public function run()
{
return \AnotherClass::class;
}
}
2019-09-18 06:14:35 +00:00
PHP
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [String_::class];
}
/**
* @param String_ $node
*/
public function refactor(Node $node): ?Node
{
2019-12-26 19:54:00 +00:00
if (! $this->isAtLeastPhpVersion(PhpVersionFeature::CLASSNAME_CONSTANT)) {
return null;
}
$classLikeName = $node->value;
// remove leading slash
$classLikeName = ltrim($classLikeName, '\\');
if ($classLikeName === '') {
return null;
}
if (! $this->classLikeSensitiveExists($classLikeName)) {
return null;
}
2020-05-03 19:30:01 +00:00
if (StaticRectorStrings::isInArrayInsensitive($classLikeName, $this->classesToSkip)) {
return null;
}
2020-06-29 21:19:37 +00:00
$fullyQualified = new FullyQualified($classLikeName);
/** @see \Rector\PostRector\Collector\UseNodesToAddCollector::isShortImported() */
2020-06-29 21:19:37 +00:00
$fullyQualified->setAttribute(AttributeKey::FILE_INFO, $node->getAttribute(AttributeKey::FILE_INFO));
2020-06-29 21:19:37 +00:00
return new ClassConstFetch($fullyQualified, 'class');
}
private function classLikeSensitiveExists(string $classLikeName): bool
{
2019-09-10 06:21:20 +00:00
if (! ClassExistenceStaticHelper::doesClassLikeExist($classLikeName)) {
return false;
}
// already known values
if (in_array($classLikeName, $this->sensitiveExistingClasses, true)) {
return true;
}
if (in_array($classLikeName, $this->sensitiveNonExistingClasses, true)) {
return false;
}
2020-06-29 21:19:37 +00:00
$reflectionClass = new ReflectionClass($classLikeName);
2020-06-29 21:19:37 +00:00
if ($classLikeName !== $reflectionClass->getName()) {
$this->sensitiveNonExistingClasses[] = $classLikeName;
return false;
}
$this->sensitiveExistingClasses[] = $classLikeName;
return true;
}
}