[CodeQualityStrict] Enable MoveOutMethodCallInsideIfConditionRector (#4502)

Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
Abdul Malik Ikhsan 2020-10-30 03:47:12 +07:00 committed by GitHub
parent 449801e1f3
commit a2ae659e0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 119 additions and 37 deletions

View File

@ -55,7 +55,8 @@ final class MultilineSpaceFormatPreserver
public function fixMultilineDescriptions(
AttributeAwareNodeInterface $attributeAwareNode
): AttributeAwareNodeInterface {
if (! $attributeAwareNode->getAttribute(Attribute::ORIGINAL_CONTENT)) {
$originalContent = $attributeAwareNode->getAttribute(Attribute::ORIGINAL_CONTENT);
if (! $originalContent) {
return $attributeAwareNode;
}

View File

@ -2,7 +2,6 @@
declare(strict_types=1);
use Rector\CodeQuality\Rector\If_\MoveOutMethodCallInsideIfConditionRector;
use Rector\CodingStyle\Rector\String_\SplitStringClassConstantToClassConstFetchRector;
use Rector\Core\Configuration\Option;
use Rector\Core\Rector\AbstractRector;
@ -78,7 +77,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
SplitStringClassConstantToClassConstFetchRector::class,
// false positives on constants used in rector-ci.php
RemoveUnusedClassConstantRector::class,
MoveOutMethodCallInsideIfConditionRector::class,
]);
# so Rector code is still PHP 7.2 compatible

View File

@ -8,6 +8,7 @@ use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Rector\Naming\Naming\ExpectedNameResolver;
use Rector\NodeNameResolver\NodeNameResolver;
@ -48,41 +49,34 @@ final class MethodCallToVariableNameResolver
public function resolveVariableName(MethodCall $methodCall): ?string
{
$methodCallVarName = $this->nodeNameResolver->getName($methodCall->var);
if ($methodCallVarName === null) {
return null;
}
$methodCallName = $this->nodeNameResolver->getName($methodCall->name);
if ($methodCallName === null) {
if ($methodCallVarName === null || $methodCallName === null) {
return null;
}
return $this->getVariableName($methodCall, $methodCallVarName, $methodCallName);
}
private function getVariableName(MethodCall $methodCall, string $methodCallVarName, string $methodCallName): string
{
$variableName = $this->expectedNameResolver->resolveForCall($methodCall);
if ($methodCall->args === [] && $variableName !== null && $variableName !== $methodCallVarName) {
return $variableName;
}
$fallbackVarName = $this->getFallbackVarName($methodCallVarName, $methodCallName);
$argValue = $methodCall->args[0]->value;
if ($argValue instanceof ClassConstFetch && $argValue->name instanceof Identifier) {
return Strings::replace(
strtolower($argValue->name->toString()),
self::CONSTANT_REGEX,
function ($matches): string {
return strtoupper($matches[2]);
}
);
return $this->getClassConstFetchVarName($argValue, $methodCallName);
}
$fallbackVarName = $this->getFallbackVarName($methodCallVarName, $methodCallName);
if ($argValue instanceof String_) {
return $this->getStringVarName($argValue, $methodCallVarName, $fallbackVarName);
}
if ($argValue instanceof Variable) {
$argumentName = $this->nodeNameResolver->getName($argValue);
if ($argumentName !== null && $variableName !== null) {
return $argumentName . ucfirst($variableName);
}
$argumentName = $this->nodeNameResolver->getName($argValue);
if ($argValue instanceof Variable && $argumentName !== null && $variableName !== null) {
return $argumentName . ucfirst($variableName);
}
return $fallbackVarName;
@ -93,16 +87,47 @@ final class MethodCallToVariableNameResolver
return $methodCallVarName . ucfirst($methodCallName);
}
private function getClassConstFetchVarName(ClassConstFetch $classConstFetch, string $methodCallName): string
{
/** @var Identifier $name */
$name = $classConstFetch->name;
$argValueName = strtolower($name->toString());
if ($argValueName !== 'class') {
return Strings::replace(
$argValueName,
self::CONSTANT_REGEX,
function ($matches): string {
return strtoupper($matches[2]);
}
);
}
if ($classConstFetch->class instanceof Name) {
return $this->normalizeStringVariableName($methodCallName) . $classConstFetch->class->getLast();
}
return $this->normalizeStringVariableName($methodCallName);
}
private function getStringVarName(String_ $string, string $methodCallVarName, string $fallbackVarName): string
{
$get = str_ireplace('get', '', $string->value . ucfirst($fallbackVarName));
$by = str_ireplace('by', '', $get);
$by = str_replace('-', '', $by);
if (Strings::match($by, self::START_ALPHA_REGEX) && $by !== $methodCallVarName) {
return $by;
$normalizeStringVariableName = $this->normalizeStringVariableName($string->value . ucfirst($fallbackVarName));
if (Strings::match(
$normalizeStringVariableName,
self::START_ALPHA_REGEX
) && $normalizeStringVariableName !== $methodCallVarName) {
return $normalizeStringVariableName;
}
return $fallbackVarName;
}
private function normalizeStringVariableName(string $string): string
{
$get = str_ireplace('get', '', $string);
$by = str_ireplace('by', '', $get);
return str_replace('-', '', $by);
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace Rector\CodeQuality\Tests\Rector\If_\MoveOutMethodCallInsideIfConditionRector\Fixture;
use Rector\CodeQuality\Tests\Rector\If_\MoveOutMethodCallInsideIfConditionRector\Source\SomeClassWithConstants;
class IfConditionMethodCallWithArgClassConstantSuffixClass
{
public function run($arg)
{
$obj = new self();
if ($obj->condition(SomeClassWithConstants::class) === 1) {
}
}
public function condition($arg): int
{
return 1;
}
}
?>
-----
<?php
namespace Rector\CodeQuality\Tests\Rector\If_\MoveOutMethodCallInsideIfConditionRector\Fixture;
use Rector\CodeQuality\Tests\Rector\If_\MoveOutMethodCallInsideIfConditionRector\Source\SomeClassWithConstants;
class IfConditionMethodCallWithArgClassConstantSuffixClass
{
public function run($arg)
{
$obj = new self();
$conditionSomeClassWithConstants = $obj->condition(SomeClassWithConstants::class);
if ($conditionSomeClassWithConstants === 1) {
}
}
public function condition($arg): int
{
return 1;
}
}
?>

View File

@ -95,8 +95,9 @@ CODE_SAMPLE
if ($classPhpDocInfo === null) {
return null;
}
$hasTypeLoggableTagValueNode = $classPhpDocInfo->hasByType(LoggableTagValueNode::class);
if (! $classPhpDocInfo->hasByType(LoggableTagValueNode::class)) {
if (! $hasTypeLoggableTagValueNode) {
return null;
}
@ -119,8 +120,9 @@ CODE_SAMPLE
if ($propertyPhpDocInfo === null) {
continue;
}
$hasTypeVersionedTagValueNode = $propertyPhpDocInfo->hasByType(VersionedTagValueNode::class);
if (! $propertyPhpDocInfo->hasByType(VersionedTagValueNode::class)) {
if (! $hasTypeVersionedTagValueNode) {
continue;
}

View File

@ -94,8 +94,9 @@ CODE_SAMPLE
if ($classPhpDocInfo === null) {
return null;
}
$hasTypeSoftDeleteableTagValueNode = $classPhpDocInfo->hasByType(SoftDeleteableTagValueNode::class);
if (! $classPhpDocInfo->hasByType(SoftDeleteableTagValueNode::class)) {
if (! $hasTypeSoftDeleteableTagValueNode) {
return null;
}

View File

@ -190,13 +190,15 @@ CODE_SAMPLE
if ($propertyPhpDocInfo === null) {
continue;
}
$hasTypeLocaleTagValueNode = $propertyPhpDocInfo->hasByType(LocaleTagValueNode::class);
if ($propertyPhpDocInfo->hasByType(LocaleTagValueNode::class)) {
if ($hasTypeLocaleTagValueNode) {
$this->removeNode($property);
continue;
}
$hasTypeTranslatableTagValueNode = $propertyPhpDocInfo->hasByType(TranslatableTagValueNode::class);
if (! $propertyPhpDocInfo->hasByType(TranslatableTagValueNode::class)) {
if (! $hasTypeTranslatableTagValueNode) {
continue;
}

View File

@ -141,8 +141,9 @@ CODE_SAMPLE
if ($classPhpDocInfo === null) {
return null;
}
$hasTypeTreeTagValueNode = $classPhpDocInfo->hasByType(TreeTagValueNode::class);
if (! $classPhpDocInfo->hasByType(TreeTagValueNode::class)) {
if (! $hasTypeTreeTagValueNode) {
return null;
}

View File

@ -99,8 +99,9 @@ final class DoctrineDocBlockResolver
if ($phpDocInfo === null) {
return false;
}
$hasTypeColumnTagValueNode = $phpDocInfo->hasByType(ColumnTagValueNode::class);
if ($phpDocInfo->hasByType(ColumnTagValueNode::class)) {
if ($hasTypeColumnTagValueNode) {
return true;
}
@ -124,8 +125,9 @@ final class DoctrineDocBlockResolver
if ($phpDocInfo === null) {
return false;
}
$hasTypeEntityTagValueNode = $phpDocInfo->hasByType(EntityTagValueNode::class);
if ($phpDocInfo->hasByType(EntityTagValueNode::class)) {
if ($hasTypeEntityTagValueNode) {
return true;
}

View File

@ -431,11 +431,13 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
private function keepFileInfoAttribute(Node $node, Node $originalNode): void
{
if ($node->getAttribute(AttributeKey::FILE_INFO) instanceof SmartFileInfo) {
$fileInfo = $node->getAttribute(AttributeKey::FILE_INFO);
if ($fileInfo instanceof SmartFileInfo) {
return;
}
$fileInfo = $originalNode->getAttribute(AttributeKey::FILE_INFO);
if ($originalNode->getAttribute(AttributeKey::FILE_INFO) !== null) {
if ($fileInfo !== null) {
$node->setAttribute(AttributeKey::FILE_INFO, $originalNode->getAttribute(AttributeKey::FILE_INFO));
} elseif ($originalNode->getAttribute(AttributeKey::PARENT_NODE) !== null) {
/** @var Node $parentOriginalNode */