[CodeQuality] Register MoveOutMethodCallInsideIfConditionRector to config code-quality set (#4477)

* [CodeQuality] Register MoveOutMethodCallInsideIfConditionRector to config code-quality set

* rename use string parameter

* phpstan

* fix false positive string param to be var === variable name

* fix false positive

* phpstan

* ensure not same variable

* phpstan

* add ucfirst after var

* add fixture failing test for variable exists to be skipped

* add fixture failing test for variable exists in previuos statement

* phpstan space

* add handling variable exists in previous statement and in ClassMethod/Functions args

* handle exists but inside if

* reduce complexity

* clean up

* clean up

* cs fix

* phpstan

* remove - in variable

* use fallback when first character is numeric

* using regex to check string start alpha for variable

* cs fix

* use ClassConstFetch name to set variable name

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* clean up

* fix

* [ci-review] Rector Rectify

* Update rules/code-quality/src/Rector/If_/MoveOutMethodCallInsideIfConditionRector.php

* Update rules/code-quality/src/Rector/If_/MoveOutMethodCallInsideIfConditionRector.php

Co-authored-by: rector-bot <tomas@getrector.org>
Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
This commit is contained in:
Abdul Malik Ikhsan 2020-10-26 21:03:26 +07:00 committed by GitHub
parent 20e95bee61
commit 7a1ea1328b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
51 changed files with 293 additions and 67 deletions

View File

@ -45,6 +45,7 @@ use Rector\CodeQuality\Rector\Identical\StrlenZeroToIdenticalEmptyStringRector;
use Rector\CodeQuality\Rector\If_\CombineIfRector;
use Rector\CodeQuality\Rector\If_\ConsecutiveNullCompareReturnsToNullCoalesceQueueRector;
use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\CodeQuality\Rector\If_\MoveOutMethodCallInsideIfConditionRector;
use Rector\CodeQuality\Rector\If_\ShortenElseIfRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
use Rector\CodeQuality\Rector\If_\SimplifyIfIssetToNullCoalescingRector;
@ -160,4 +161,5 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(FixClassCaseSensitivityNameRector::class);
$services->set(IssetOnPropertyObjectToPropertyExistsRector::class);
$services->set(NewStaticToNewSelfRector::class);
$services->set(MoveOutMethodCallInsideIfConditionRector::class);
};

View File

@ -401,7 +401,10 @@ final class PhpDocInfoPrinter
private function hasDescription(AttributeAwarePhpDocTagNode $attributeAwarePhpDocTagNode): bool
{
if (! $attributeAwarePhpDocTagNode->getAttribute(Attribute::HAS_DESCRIPTION_WITH_ORIGINAL_SPACES)) {
$hasDescriptionWithOriginalSpaces = $attributeAwarePhpDocTagNode->getAttribute(
Attribute::HAS_DESCRIPTION_WITH_ORIGINAL_SPACES
);
if (! $hasDescriptionWithOriginalSpaces) {
return false;
}

View File

@ -45,8 +45,9 @@ final class NetteInjectDetector
if ($phpDocInfo === null) {
continue;
}
$injectPhpDocInfoTagsName = $phpDocInfo->getTagsByName('inject');
if ($phpDocInfo->getTagsByName('inject') === []) {
if ($injectPhpDocInfoTagsName === []) {
continue;
}

View File

@ -49,7 +49,8 @@ final class NameImportingPostRector extends AbstractPostRector
public function enterNode(Node $node): ?Node
{
if (! $this->parameterProvider->provideParameter(Option::AUTO_IMPORT_NAMES)) {
$autoImportNames = $this->parameterProvider->provideParameter(Option::AUTO_IMPORT_NAMES);
if (! $autoImportNames) {
return null;
}

View File

@ -773,3 +773,9 @@ parameters:
- utils/project-validator/src/Process/ParallelTaskRunner.php # 19
- '#Parameter \#1 \$node of method Rector\\DeadCode\\Rector\\Plus\\RemoveDeadZeroAndOneOperationRector\:\:refactor\(\) expects PhpParser\\Node\\Expr\\AssignOp\\Div\|PhpParser\\Node\\Expr\\AssignOp\\Minus\|PhpParser\\Node\\Expr\\AssignOp\\Mul\|PhpParser\\Node\\Expr\\AssignOp\\Plus\|PhpParser\\Node\\Expr\\BinaryOp\\Div\|PhpParser\\Node\\Expr\\BinaryOp\\Minus\|PhpParser\\Node\\Expr\\BinaryOp\\Mul\|PhpParser\\Node\\Expr\\BinaryOp\\Plus, PhpParser\\Node\\Expr\\AssignOp\|PhpParser\\Node\\Expr\\BinaryOp given#'
-
message: '#Variable "\$underscoreCamelCasePropertyRenamerRename" is too long with 40 chars\. Narrow it under 40 chars#'
paths:
- rules/naming/src/Rector/Property/UnderscoreToCamelCasePropertyNameRector.php # 104
- rules/naming/src/Rector/Property/UnderscoreToCamelCasePropertyNameRector.php # 106

View File

@ -152,10 +152,11 @@ CODE_SAMPLE
} else {
return null;
}
$nextNode = $foreach->getAttribute(AttributeKey::NEXT_NODE);
// is next node Return?
if ($foreach->getAttribute(AttributeKey::NEXT_NODE) instanceof Return_) {
$this->return = $foreach->getAttribute(AttributeKey::NEXT_NODE);
if ($nextNode instanceof Return_) {
$this->return = $nextNode;
$this->removeNode($this->return);
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Rector\CodeQuality\Rector\If_;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\ClassConstFetch;
@ -11,6 +12,10 @@ use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\BooleanType;
@ -19,12 +24,25 @@ use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\Naming\Naming\ExpectedNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
/**
* @see \Rector\CodeQuality\Tests\Rector\If_\MoveOutMethodCallInsideIfConditionRector\MoveOutMethodCallInsideIfConditionRectorTest
*/
final class MoveOutMethodCallInsideIfConditionRector extends AbstractRector
{
/**
* @var string
* @see https://regex101.com/r/LTykey/1
*/
private const START_ALPHA_REGEX = '#^[a-zA-Z]#';
/**
* @var string
* @see https://regex101.com/r/sYIKpj/1
*/
private const CONSTANT_REGEX = '#(_)([a-z])#';
/**
* @var ExpectedNameResolver
*/
@ -132,7 +150,10 @@ CODE_SAMPLE
private function moveOutMethodCall(MethodCall $methodCall, If_ $if): ?If_
{
$variableName = $this->getVariableName($methodCall);
if ($variableName === null) {
if ($variableName === null || $this->isVariableExists(
$if,
$variableName
) || $this->isVariableExistsInParentNode($if, $variableName)) {
return null;
}
@ -172,16 +193,79 @@ CODE_SAMPLE
}
$variableName = $this->expectedNameResolver->resolveForCall($methodCall);
if ($variableName !== null) {
if ($methodCall->args === [] && $variableName !== null && $variableName !== $methodCallVarName) {
return $variableName;
}
$arg0 = $methodCall->args[0]->value;
if ($arg0 instanceof ClassConstFetch && $arg0->name instanceof Identifier) {
$explodeUnderscore = explode('_', $arg0->name->toString());
return $methodCallVarName . ucfirst(strtolower((string) end($explodeUnderscore)));
return Strings::replace(
strtolower($arg0->name->toString()),
self::CONSTANT_REGEX,
function ($matches): string {
return strtoupper($matches[2]);
}
);
}
$fallbackVarName = $this->getFallbackVarName($methodCallVarName, $methodCallName);
if ($arg0 instanceof String_) {
return $this->getStringVarName($arg0, $methodCallVarName, $fallbackVarName);
}
return $fallbackVarName;
}
private function isVariableExists(If_ $if, string $variableName): bool
{
return (bool) $this->betterNodeFinder->findFirstPrevious($if, function (Node $node) use ($variableName): bool {
return $node instanceof Variable && $node->name === $variableName;
});
}
private function isVariableExistsInParentNode(If_ $if, string $variableName): bool
{
$parentNode = $if->getAttribute(AttributeKey::PARENT_NODE);
while ($parentNode) {
if ($parentNode instanceof ClassMethod || $parentNode instanceof Function_) {
return $this->isVariableExistsInParams($parentNode->params, $variableName);
}
$parentNode = $parentNode->getAttribute(AttributeKey::PARENT_NODE);
}
return false;
}
private function getFallbackVarName(string $methodCallVarName, string $methodCallName): string
{
return $methodCallVarName . ucfirst($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;
}
return $fallbackVarName;
}
/**
* @param Param[] $parameters
*/
private function isVariableExistsInParams(array $parameters, string $variableName): bool
{
foreach ($parameters as $param) {
if ($param->var instanceof Variable && $param->var->name === $variableName) {
return true;
}
}
return false;
}
}

View File

@ -151,7 +151,8 @@ CODE_SAMPLE
private function haveNestedTernary(array $nodes): bool
{
foreach ($nodes as $node) {
if ($this->betterNodeFinder->findInstanceOf($node, Ternary::class) !== []) {
$betterNodeFinderFindInstanceOf = $this->betterNodeFinder->findInstanceOf($node, Ternary::class);
if ($betterNodeFinderFindInstanceOf !== []) {
return true;
}
}

View File

@ -9,9 +9,6 @@ class IfConditionMethodCallWithArg
$obj = new self();
if ($obj->condition($arg + 1) === 1) {
}
if ($obj->condition($arg + 2) === 2) {
}
}
@ -35,10 +32,6 @@ class IfConditionMethodCallWithArg
$objCondition = $obj->condition($arg + 1);
if ($objCondition === 1) {
}
$objCondition = $obj->condition($arg + 2);
if ($objCondition === 2) {
}
}

View File

@ -36,12 +36,12 @@ class IfConditionMethodCallWithArgClassConstant
public function run($arg)
{
$obj = new self();
$objArg1 = $obj->condition(SomeClassWithConstants::ARG1);
if ($objArg1 === 1) {
$arg1 = $obj->condition(SomeClassWithConstants::ARG1);
if ($arg1 === 1) {
}
$objArg2 = $obj->condition(SomeClassWithConstants::ARG2);
if ($objArg2 === 2) {
$arg2 = $obj->condition(SomeClassWithConstants::ARG2);
if ($arg2 === 2) {
}
}

View File

@ -42,8 +42,8 @@ abstract class AbstractCommand extends Command
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$inputDebug = $input->getOption(Option::OPTION_DEBUG);
if ($inputDebug) {
$optionDebug = $input->getOption(Option::OPTION_DEBUG);
if ($optionDebug) {
return;
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace Rector\CodeQuality\Tests\Rector\If_\MoveOutMethodCallInsideIfConditionRector\Fixture;
class IfConditionMethodCallWithArgHasNumericStringInFirstArg
{
public function run()
{
$obj = new self();
if ($obj->condition('1a') === 1) {
}
}
public function condition($arg): int
{
return 1;
}
}
?>
-----
<?php
namespace Rector\CodeQuality\Tests\Rector\If_\MoveOutMethodCallInsideIfConditionRector\Fixture;
class IfConditionMethodCallWithArgHasNumericStringInFirstArg
{
public function run()
{
$obj = new self();
$objCondition = $obj->condition('1a');
if ($objCondition === 1) {
}
}
public function condition($arg): int
{
return 1;
}
}
?>

View File

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

View File

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

View File

@ -100,7 +100,8 @@ final class NameImporter
private function shouldSkipName(Name $name): bool
{
if ($name->getAttribute(AttributeKey::VIRTUAL_NODE)) {
$virtualNode = $name->getAttribute(AttributeKey::VIRTUAL_NODE);
if ($virtualNode) {
return true;
}

View File

@ -66,8 +66,9 @@ CODE_SAMPLE
{
$doubleQuoteCount = substr_count($node->value, '"');
$singleQuoteCount = substr_count($node->value, "'");
$kind = $node->getAttribute(AttributeKey::KIND);
if ($node->getAttribute(AttributeKey::KIND) === String_::KIND_SINGLE_QUOTED) {
if ($kind === String_::KIND_SINGLE_QUOTED) {
$this->processSingleQuoted($node, $doubleQuoteCount, $singleQuoteCount);
}

View File

@ -55,9 +55,10 @@ final class VariableUseFinder
if ($parentNode instanceof Assign && ($parentNode->var instanceof Variable && $parentNode->var === $node)) {
return false;
}
$nodeNameResolverGetName = $this->nodeNameResolver->getName($node);
// simple variable only
if ($this->nodeNameResolver->getName($node) === null) {
if ($nodeNameResolverGetName === null) {
return false;
}

View File

@ -78,9 +78,10 @@ CODE_SAMPLE
if ($class === null) {
return null;
}
$nodeRepositoryFindInterface = $this->nodeRepository->findInterface($class);
// 0. constants declared in interfaces have to be public
if ($this->nodeRepository->findInterface($class) !== null) {
if ($nodeRepositoryFindInterface !== null) {
$this->makePublic($node);
return $node;
}

View File

@ -65,8 +65,9 @@ CODE_SAMPLE
if (strtolower((string) $defaultValueNode->name) !== 'null') {
return null;
}
$nodeNode = $node->getAttribute(AttributeKey::PREVIOUS_NODE);
if ($node->getAttribute(AttributeKey::PREVIOUS_NODE) instanceof NullableType) {
if ($nodeNode instanceof NullableType) {
return null;
}

View File

@ -135,9 +135,10 @@ final class FluentChainMethodCallRootExtractor
// the method call, does not belong to the
$staticType = $this->nodeTypeResolver->getStaticType($methodCall);
$parentNode = $methodCall->getAttribute(AttributeKey::PARENT_NODE);
// no assign
if ($methodCall->getAttribute(AttributeKey::PARENT_NODE) instanceof Expression) {
if ($parentNode instanceof Expression) {
$variableName = $this->propertyNaming->fqnToVariableName($staticType);
// the assign expresison must be break

View File

@ -66,9 +66,10 @@ final class EntityWithMissingUuidProvider
if (! $this->doctrineDocBlockResolver->isDoctrineEntityClassWithIdProperty($class)) {
continue;
}
$uuidClassProperty = $class->getProperty('uuid');
// already has $uuid property
if ($class->getProperty('uuid') !== null) {
if ($uuidClassProperty !== null) {
continue;
}

View File

@ -93,8 +93,9 @@ CODE_SAMPLE
$factoryClass = $factoryInfo['class'];
$factoryMethod = $factoryInfo['method'];
$className = $node->getAttribute(AttributeKey::CLASS_NAME);
if ($node->getAttribute(AttributeKey::CLASS_NAME) === $factoryClass) {
if ($className === $factoryClass) {
continue;
}

View File

@ -258,7 +258,8 @@ final class ExpectedNameResolver
private function resolveReturnTypeFromArrayType(Expr $expr, ArrayType $arrayType): ?Type
{
if (! $expr->getAttribute(AttributeKey::PARENT_NODE) instanceof Foreach_) {
$parentNode = $expr->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Foreach_) {
return null;
}

View File

@ -121,8 +121,9 @@ CODE_SAMPLE
if ($paramRename === null) {
continue;
}
$matchTypeParamRenamerRename = $this->matchTypeParamRenamer->rename($paramRename);
if ($this->matchTypeParamRenamer->rename($paramRename) === null) {
if ($matchTypeParamRenamerRename === null) {
continue;
}

View File

@ -119,8 +119,9 @@ CODE_SAMPLE
if ($propertyRename === null) {
continue;
}
$matchTypePropertyRenamerRename = $this->matchTypePropertyRenamer->rename($propertyRename);
if ($this->matchTypePropertyRenamer->rename($propertyRename) !== null) {
if ($matchTypePropertyRenamerRename !== null) {
$this->hasChanged = true;
}
}

View File

@ -99,9 +99,10 @@ CODE_SAMPLE
if ($propertyRename === null) {
return null;
}
$property = $this->boolPropertyRenamer->rename($propertyRename);
// dd($propertyRename->getClassLike());
if ($this->boolPropertyRenamer->rename($propertyRename) === null) {
if ($property === null) {
return null;
}

View File

@ -101,8 +101,9 @@ CODE_SAMPLE
if ($propertyRename === null) {
return null;
}
$property = $this->underscoreCamelCasePropertyRenamer->rename($propertyRename);
if ($this->underscoreCamelCasePropertyRenamer->rename($propertyRename) === null) {
if ($property === null) {
return null;
}

View File

@ -99,7 +99,8 @@ CODE_SAMPLE
private function processAboveTestInclude(Include_ $include): void
{
if ($include->getAttribute(AttributeKey::CLASS_NODE) === null) {
$classLike = $include->getAttribute(AttributeKey::CLASS_NODE);
if ($classLike === null) {
$this->removeNode($include);
}
}

View File

@ -307,8 +307,9 @@ CODE_SAMPLE
if (! $this->isName($node, '#^(render|action)#')) {
return true;
}
$hasRouteAnnotation = $node->getAttribute(ExplicitRouteAnnotationDecorator::HAS_ROUTE_ANNOTATION);
if ($node->getAttribute(ExplicitRouteAnnotationDecorator::HAS_ROUTE_ANNOTATION)) {
if ($hasRouteAnnotation) {
return true;
}

View File

@ -94,9 +94,10 @@ final class PhpSpecClassToPHPUnitClassRector extends AbstractPhpSpecToPHPUnitRec
$this->testedObjectType = new ObjectType($testedClass);
$this->classInsertManipulator->addPropertyToClass($node, $propertyName, $this->testedObjectType);
$classMethod = $node->getMethod('let');
// add let if missing
if ($node->getMethod('let') === null) {
if ($classMethod === null) {
if (! $this->letManipulator->isLetNeededInClass($node)) {
return null;
}

View File

@ -187,8 +187,9 @@ final class PhpSpecPromisesToPHPUnitAssertRector extends AbstractPhpSpecToPHPUni
/** @var Class_ $classLike */
$classLike = $node->getAttribute(AttributeKey::CLASS_NODE);
$classMethod = $classLike->getMethod($methodName);
// it's a method call, skip
if ($classLike->getMethod($methodName) !== null) {
if ($classMethod !== null) {
return null;
}

View File

@ -80,9 +80,10 @@ CODE_SAMPLE
if (! $this->isName($classLike, $this->getName($node))) {
return null;
}
$classMethod = $classLike->getMethod(MethodName::CONSTRUCT);
// does it already have a __construct method?
if ($classLike->getMethod(MethodName::CONSTRUCT) === null) {
if ($classMethod === null) {
$node->name = new Identifier(MethodName::CONSTRUCT);
}

View File

@ -58,9 +58,10 @@ final class IsArrayAndDualCheckToAble
if ((string) $instanceOfClass !== $type) {
return null;
}
$nodeNameResolverGetName = $this->nodeNameResolver->getName($funcCallNode);
/** @var FuncCall $funcCallNode */
if ($this->nodeNameResolver->getName($funcCallNode) !== 'is_array') {
if ($nodeNameResolverGetName !== 'is_array') {
return null;
}

View File

@ -102,9 +102,10 @@ CODE_SAMPLE
if (! $this->isName($funcCall, 'count')) {
return true;
}
$alreadyChangedOnCount = $funcCall->getAttribute(self::ALREADY_CHANGED_ON_COUNT);
// check if it has some condition before already, if so, probably it's already handled
if ($funcCall->getAttribute(self::ALREADY_CHANGED_ON_COUNT)) {
if ($alreadyChangedOnCount) {
return true;
}

View File

@ -61,8 +61,9 @@ CODE_SAMPLE
return null;
}
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($node->getAttribute(AttributeKey::PARENT_NODE) instanceof Expression) {
if ($parentNode instanceof Expression) {
$this->removeNode($node);
return null;

View File

@ -60,7 +60,8 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
if (! in_array($node->getAttribute(AttributeKey::KIND), [String_::KIND_HEREDOC, String_::KIND_NOWDOC], true)) {
$kind = $node->getAttribute(AttributeKey::KIND);
if (! in_array($kind, [String_::KIND_HEREDOC, String_::KIND_NOWDOC], true)) {
return null;
}

View File

@ -61,7 +61,8 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
if ($node->getAttribute(AttributeKey::KIND) !== Double::KIND_REAL) {
$kind = $node->getAttribute(AttributeKey::KIND);
if ($kind !== Double::KIND_REAL) {
return null;
}

View File

@ -162,8 +162,9 @@ CODE_SAMPLE
private function shouldSkipMethodCall(MethodCall $methodCall): bool
{
$scope = $methodCall->getAttribute(AttributeKey::SCOPE);
// just added node → skip it
if ($methodCall->getAttribute(AttributeKey::SCOPE) === null) {
if ($scope === null) {
return true;
}

View File

@ -98,9 +98,10 @@ CODE_SAMPLE
// Remember when we have already processed this constant recursively
$node->setAttribute(self::HAS_NEW_ACCESS_LEVEL, true);
$nodeRepositoryFindInterface = $this->nodeRepository->findInterface($class);
// 0. constants declared in interfaces have to be public
if ($this->nodeRepository->findInterface($class) !== null) {
if ($nodeRepositoryFindInterface !== null) {
$this->makePublic($node);
return $node;
}
@ -132,7 +133,8 @@ CODE_SAMPLE
private function shouldSkip(ClassConst $classConst): bool
{
if ($classConst->getAttribute(self::HAS_NEW_ACCESS_LEVEL)) {
$hasNewAccessLevel = $classConst->getAttribute(self::HAS_NEW_ACCESS_LEVEL);
if ($hasNewAccessLevel) {
return true;
}

View File

@ -221,9 +221,10 @@ final class ClassRenamer
}
$classLike->name = new Identifier($newClassNamePart);
$classNamingGetNamespace = $this->classNaming->getNamespace($name);
// Old class did not have any namespace, we need to wrap class with Namespace_ node
if ($newNamespacePart && ! $this->classNaming->getNamespace($name)) {
if ($newNamespacePart && ! $classNamingGetNamespace) {
$this->changeNameToFullyQualifiedName($classLike);
$nameNode = new Name($newNamespacePart);
@ -301,8 +302,9 @@ final class ClassRenamer
if (! $implementName instanceof Name) {
continue;
}
$virtualNode = $implementName->getAttribute(AttributeKey::VIRTUAL_NODE);
if (! $implementName->getAttribute(AttributeKey::VIRTUAL_NODE)) {
if (! $virtualNode) {
continue;
}

View File

@ -59,19 +59,22 @@ CODE_SAMPLE
if ($className === null) {
return null;
}
$nodeRepositoryFindMethodCallsOnClass = $this->nodeRepository->findMethodCallsOnClass($className);
// 1. is in static call?
if ($this->nodeRepository->findMethodCallsOnClass($className) !== []) {
if ($nodeRepositoryFindMethodCallsOnClass !== []) {
return null;
}
$parsedNodeCollectorFindNewsByClass = $this->parsedNodeCollector->findNewsByClass($className);
// 2. is in new?
if ($this->parsedNodeCollector->findNewsByClass($className) !== []) {
if ($parsedNodeCollectorFindNewsByClass !== []) {
return null;
}
$nodeRepositoryFindChildrenOfClass = $this->nodeRepository->findChildrenOfClass($className);
// 3. does it have any children
if ($this->nodeRepository->findChildrenOfClass($className) === []) {
if ($nodeRepositoryFindChildrenOfClass === []) {
return null;
}

View File

@ -110,7 +110,8 @@ CODE_SAMPLE
private function shouldSkipMethodCall(MethodCall $methodCall): bool
{
if ($methodCall->getAttribute(AttributeKey::IS_FRESH_NODE)) {
$isFreshNode = $methodCall->getAttribute(AttributeKey::IS_FRESH_NODE);
if ($isFreshNode) {
return true;
}

View File

@ -83,8 +83,9 @@ CODE_SAMPLE
private function shouldSkipMethodCall(MethodCall $methodCall): bool
{
$originalNode = $methodCall->getAttribute(AttributeKey::ORIGINAL_NODE);
// skip just added calls
if ($methodCall->getAttribute(AttributeKey::ORIGINAL_NODE) === null) {
if ($originalNode === null) {
return true;
}
@ -95,8 +96,9 @@ CODE_SAMPLE
if (! $this->isName($methodCall->name, 'isValid')) {
return true;
}
$previousNode = $methodCall->getAttribute(AttributeKey::PREVIOUS_NODE);
if ($methodCall->getAttribute(AttributeKey::PREVIOUS_NODE) !== null) {
if ($previousNode !== null) {
return true;
}

View File

@ -259,7 +259,8 @@ CODE_SAMPLE
private function addBuildFormMethod(Class_ $class, ClassMethod $classMethod): void
{
if ($class->getMethod('buildForm') !== null) {
$buildFormClassMethod = $class->getMethod('buildForm');
if ($buildFormClassMethod !== null) {
return;
}
@ -271,7 +272,8 @@ CODE_SAMPLE
*/
private function addConfigureOptionsMethod(Class_ $class, array $namesToArgs): void
{
if ($class->getMethod('configureOptions') !== null) {
$configureOptionsClassMethod = $class->getMethod('configureOptions');
if ($configureOptionsClassMethod !== null) {
return;
}

View File

@ -33,9 +33,10 @@ final class ConstructorAssignDetector extends AbstractAssignDetector
/** @var Assign $assign */
$assign = $node;
$isFirstLevelStatement = $assign->getAttribute(AttributeKey::IS_FIRST_LEVEL_STATEMENT);
// cannot be nested
if ($assign->getAttribute(AttributeKey::IS_FIRST_LEVEL_STATEMENT) !== true) {
if ($isFirstLevelStatement !== true) {
return null;
}

View File

@ -301,8 +301,9 @@ final class Configuration
if ($noProgressBar) {
return false;
}
$optionOutputFormat = $input->getOption(Option::OPTION_OUTPUT_FORMAT);
if ($input->getOption(Option::OPTION_OUTPUT_FORMAT) === JsonOutputFormatter::NAME) {
if ($optionOutputFormat === JsonOutputFormatter::NAME) {
return false;
}
return $input->getOption(Option::OPTION_OUTPUT_FORMAT) !== CheckstyleOutputFormatter::NAME;

View File

@ -65,7 +65,8 @@ abstract class AbstractCommand extends Command
protected function initialize(InputInterface $input, OutputInterface $output): void
{
if ($input->getOption(Option::OPTION_DEBUG)) {
$optionDebug = $input->getOption(Option::OPTION_DEBUG);
if ($optionDebug) {
if ($this->getApplication() === null) {
return;
}
@ -76,9 +77,10 @@ abstract class AbstractCommand extends Command
// clear cache
$this->changedFilesDetector->clear();
}
$optionClearCache = $input->getOption(Option::OPTION_CLEAR_CACHE);
// clear cache
if ($input->getOption(Option::OPTION_CLEAR_CACHE)) {
if ($optionClearCache) {
$this->changedFilesDetector->clear();
}
}

View File

@ -30,9 +30,10 @@ final class SymfonyStyleFactory
// to configure all -v, -vv, -vvv options without memory-lock to Application run() arguments
$this->privatesCaller->callPrivateMethod(new Application(), 'configureIO', $argvInput, $consoleOutput);
$debugArgvInputParameterOption = $argvInput->getParameterOption('--debug');
// --debug is called
if ($argvInput->getParameterOption('--debug') === null) {
if ($debugArgvInputParameterOption === null) {
$consoleOutput->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
}

View File

@ -167,8 +167,9 @@ final class BetterNodeFinder
{
foreach ($types as $type) {
$this->ensureIsNodeClass($type, __METHOD__, 1);
$nodeFinderFindFirstInstanceOf = $this->nodeFinder->findFirstInstanceOf($nodes, $type);
if ($this->nodeFinder->findFirstInstanceOf($nodes, $type) === null) {
if ($nodeFinderFindFirstInstanceOf === null) {
continue;
}

View File

@ -226,8 +226,9 @@ final class IfManipulator
if (! $this->isIfWithoutElseAndElseIfs($currentIf)) {
return [];
}
$betterNodeFinderFindInstanceOf = $this->betterNodeFinder->findInstanceOf($currentIf->stmts, Return_::class);
if ($this->betterNodeFinder->findInstanceOf($currentIf->stmts, Return_::class) !== []) {
if ($betterNodeFinderFindInstanceOf !== []) {
return [];
}

View File

@ -360,7 +360,8 @@ final class BetterStandardPrinter extends Standard
*/
protected function pScalar_String(String_ $string): string
{
if ($string->getAttribute(AttributeKey::IS_REGULAR_PATTERN)) {
$isRegularPattern = $string->getAttribute(AttributeKey::IS_REGULAR_PATTERN);
if ($isRegularPattern) {
$kind = $string->getAttribute(AttributeKey::KIND, String_::KIND_SINGLE_QUOTED);
if ($kind === String_::KIND_DOUBLE_QUOTED) {
return $this->wrapValueWith($string, '"');