[CodeQualityStrict] Add CountArrayToEmptyArrayComparisonRector to CodeQualityStrict (#4496)

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-29 15:59:39 +07:00 committed by GitHub
parent 2116630748
commit 9e0b632e4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
62 changed files with 81 additions and 77 deletions

View File

@ -3,9 +3,11 @@
declare(strict_types=1);
use Rector\CodeQuality\Rector\If_\MoveOutMethodCallInsideIfConditionRector;
use Rector\Performance\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(MoveOutMethodCallInsideIfConditionRector::class);
$services->set(CountArrayToEmptyArrayComparisonRector::class);
};

View File

@ -11,7 +11,7 @@ final class EmptyPhpDocDetector
{
public function isPhpDocNodeEmpty(PhpDocNode $phpDocNode): bool
{
if (count($phpDocNode->children) === 0) {
if ($phpDocNode->children === []) {
return true;
}

View File

@ -86,7 +86,7 @@ final class RectorsDocumentationPrinter
*/
private function printRectorsWithHeadline(array $rectors, string $headline): string
{
if (count($rectors) === 0) {
if ($rectors === []) {
return '';
}

View File

@ -35,12 +35,12 @@ abstract class AbstractArgumentProbeRector extends AbstractRector
}
// we need some params to analyze
if (count((array) $classMethod->params) === 0) {
if ((array) $classMethod->params === []) {
return true;
}
// method without body doesn't need analysis
return count((array) $classMethod->stmts) === 0;
return (array) $classMethod->stmts === [];
}
protected function getClassMethodReference(ClassMethod $classMethod): ?string

View File

@ -34,7 +34,7 @@ final class ClassConstNameResolver implements NodeNameResolverInterface
*/
public function resolve(Node $node): ?string
{
if (count($node->consts) === 0) {
if ($node->consts === []) {
return null;
}

View File

@ -34,7 +34,7 @@ final class PropertyNameResolver implements NodeNameResolverInterface
*/
public function resolve(Node $node): ?string
{
if (count($node->props) === 0) {
if ($node->props === []) {
return null;
}

View File

@ -34,7 +34,7 @@ final class UseNameResolver implements NodeNameResolverInterface
*/
public function resolve(Node $node): ?string
{
if (count($node->uses) === 0) {
if ($node->uses === []) {
return null;
}

View File

@ -116,7 +116,7 @@ final class TypeFactory
*/
private function createUnionOrSingleType(array $types): Type
{
if (count($types) === 0) {
if ($types === []) {
return new MixedType();
}

View File

@ -38,7 +38,7 @@ final class NodesToAddCollector implements NodeCollectorInterface
public function isActive(): bool
{
return count($this->nodesToAddAfter) > 0 || count($this->nodesToAddBefore) > 0;
return $this->nodesToAddAfter !== [] || $this->nodesToAddBefore !== [];
}
public function addNodeBeforeNode(Node $addedNode, Node $positionNode): void

View File

@ -21,7 +21,7 @@ final class NodesToReplaceCollector implements NodeCollectorInterface
public function isActive(): bool
{
return count($this->nodesToReplace) > 0;
return $this->nodesToReplace !== [];
}
/**

View File

@ -39,15 +39,15 @@ final class PropertyToAddCollector implements NodeCollectorInterface
public function isActive(): bool
{
if (count($this->propertiesByClass) > 0) {
if ($this->propertiesByClass !== []) {
return true;
}
if (count($this->propertiesWithoutConstructorByClass) > 0) {
if ($this->propertiesWithoutConstructorByClass !== []) {
return true;
}
return count($this->constantsByClass) > 0;
return $this->constantsByClass !== [];
}
public function addPropertyToClass(string $propertyName, ?Type $propertyType, Class_ $class): void

View File

@ -42,7 +42,7 @@ final class UseNodesToAddCollector implements NodeCollectorInterface
public function isActive(): bool
{
return count($this->useImportTypesInFilePath) > 0 || count($this->functionUseImportTypesInFilePath) > 0;
return $this->useImportTypesInFilePath !== [] || $this->functionUseImportTypesInFilePath !== [];
}
/**

View File

@ -66,7 +66,7 @@ final class UseAddingPostRector extends AbstractPostRector
public function beforeTraverse(array $nodes): array
{
// no nodes → just return
if (count($nodes) === 0) {
if ($nodes === []) {
return $nodes;
}

View File

@ -2,6 +2,7 @@
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;
@ -31,6 +32,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$parameters->set(Option::SETS, [
SetList::CODING_STYLE,
SetList::CODE_QUALITY,
SetList::CODE_QUALITY_STRICT,
SetList::DEAD_CODE,
SetList::NETTE_UTILS_CODE_QUALITY,
SetList::SOLID,
@ -76,6 +78,7 @@ 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

@ -70,7 +70,7 @@ CODE_SAMPLE
// special case of nested array items
if ($node->valueVar instanceof Array_) {
$node->valueVar = $this->refactorArrayForeachValue($node->valueVar, $node);
if (count($node->valueVar->items) > 0) {
if ($node->valueVar->items !== []) {
return null;
}
} elseif ($node->valueVar instanceof Variable) {

View File

@ -74,7 +74,7 @@ CODE_SAMPLE
return null;
}
if (count($node->elseifs) > 0) {
if ($node->elseifs !== []) {
return null;
}

View File

@ -118,7 +118,7 @@ CODE_SAMPLE
private function shouldSkip(If_ $if): bool
{
if (count($if->elseifs) > 0) {
if ($if->elseifs !== []) {
return true;
}
@ -211,7 +211,7 @@ CODE_SAMPLE
return false;
}
if (count($if->elseifs) > 0) {
if ($if->elseifs !== []) {
return false;
}

View File

@ -130,7 +130,7 @@ CODE_SAMPLE
*/
private function createReturnNodes(array $newNodes): ?Expr
{
if (count($newNodes) === 0) {
if ($newNodes === []) {
return null;
}

View File

@ -25,7 +25,7 @@ final class UseImportsRemover
$this->removeUseFromUse($removedShortUses, $stmt);
// nothing left → remove
if (count($stmt->uses) === 0) {
if ($stmt->uses === []) {
unset($stmts[$stmtKey]);
}
}
@ -46,7 +46,7 @@ final class UseImportsRemover
$this->removeUseFromUse($removedShortUses, $stmt);
// nothing left → remove
if (count($stmt->uses) === 0) {
if ($stmt->uses === []) {
unset($namespace->stmts[$namespaceKey]);
}
}

View File

@ -103,7 +103,7 @@ final class CurrentAndParentClassMethodComparator
return false;
}
if (count($parentStaticCallArgs) === 0) {
if ($parentStaticCallArgs === []) {
return true;
}

View File

@ -68,7 +68,7 @@ CODE_SAMPLE
// variable is used
$variableUsages = $this->findVariableUsages($classMethod, $node);
if (count($variableUsages) > 0) {
if ($variableUsages !== []) {
return null;
}

View File

@ -73,7 +73,7 @@ CODE_SAMPLE
return null;
}
if ($node->stmts === null || count($node->stmts) > 0) {
if ($node->stmts === null || $node->stmts !== []) {
return null;
}

View File

@ -72,7 +72,7 @@ CODE_SAMPLE
return null;
}
if ($node->finally !== null && count($node->finally->stmts) > 0) {
if ($node->finally !== null && $node->finally->stmts !== []) {
return null;
}

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Rector\Decouple\NodeFactory;
use function count;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
@ -21,7 +20,7 @@ final class ConstructorClassMethodFactory
*/
public function create(array $properties): ?ClassMethod
{
if (count($properties) === 0) {
if ($properties === []) {
return null;
}

View File

@ -50,7 +50,7 @@ final class SameClassMethodCallAnalyzer
array $calleeUniqueTypes,
FirstCallFactoryAwareInterface $firstCallFactoryAware
): bool {
if (count($calleeUniqueTypes) === 0) {
if ($calleeUniqueTypes === []) {
return false;
}

View File

@ -246,7 +246,7 @@ CODE_SAMPLE
*/
private function hasAnyItemByRef(array $items): bool
{
return count($this->getItemsByRef($items, self::ANY)) > 0;
return $this->getItemsByRef($items, self::ANY) !== [];
}
/**

View File

@ -102,9 +102,9 @@ CODE_SAMPLE
private function shouldRefactor(Array_ $array): bool
{
// Check that any item in the array is the spread
return count(array_filter($array->items, function (?ArrayItem $item): bool {
return array_filter($array->items, function (?ArrayItem $item): bool {
return $item !== null && $item->unpack;
})) > 0;
}) !== [];
}
private function refactorNode(Array_ $array): Node

View File

@ -86,7 +86,7 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
if (count($node->args) === 0) {
if ($node->args === []) {
return null;
}

View File

@ -100,7 +100,7 @@ CODE_SAMPLE
}
if ($node instanceof FuncCall) {
if (count($node->args) === 0) {
if ($node->args === []) {
return $variable;
}

View File

@ -106,7 +106,7 @@ CODE_SAMPLE
}
if (
count($node->args) === 0
$node->args === []
|| ! $this->isProbablyMysql($node->args[0]->value)
) {
$connectionVariable = $this->findConnectionVariable($node);

View File

@ -173,7 +173,7 @@ final class ExpectedNameResolver
}
// call with args can return different value, so skip there if not sure about the type
if (count($expr->args) > 0) {
if ($expr->args !== []) {
return null;
}

View File

@ -81,7 +81,7 @@ final class EventValueObjectClassFactory
*/
private function decorateWithConstructorIfHasArgs(ClassBuilder $classBuilder, array $args): void
{
if (count($args) === 0) {
if ($args === []) {
return;
}

View File

@ -167,7 +167,7 @@ CODE_SAMPLE
$methodCall->args[1] = new Arg($this->createClassConstantReference($classType));
if (count($optionsArray->items) > 0) {
if ($optionsArray->items !== []) {
$methodCall->args[2] = new Arg($optionsArray);
}
}

View File

@ -74,8 +74,8 @@ class SomeClass implements FoodRecipeInterface
}
CODE_SAMPLE
, [
self::METHOD_ORDER_BY_INTERFACES => [
'FoodRecipeInterface' => ['getDescription', 'process'],
self::METHOD_ORDER_BY_INTERFACES => [
'FoodRecipeInterface' => ['getDescription', 'process'],
],
]),
]);
@ -104,14 +104,14 @@ CODE_SAMPLE
foreach ($implementedInterfaces as $implementedInterface) {
$methodOrder = $this->methodOrderByInterfaces[$implementedInterface] ?? null;
if ($methodOrder === null) {
continue;
continue;
}
$oldToNewKeys = $this->stmtOrder->createOldToNewKeys($publicMethodOrderByKey, $methodOrder);
// nothing to re-order
if (array_keys($oldToNewKeys) === array_values($oldToNewKeys)) {
return null;
return null;
}
$this->stmtOrder->reorderClassStmtsByOldToNewKeys($node, $oldToNewKeys);
@ -136,11 +136,11 @@ CODE_SAMPLE
foreach ($class->stmts as $key => $classStmt) {
if (! $classStmt instanceof ClassMethod) {
continue;
continue;
}
if (! $classStmt->isPublic()) {
continue;
continue;
}
/** @var string $classMethodName */

View File

@ -66,7 +66,7 @@ CODE_SAMPLE
return null;
}
if (count($node->args) === 0) {
if ($node->args === []) {
return null;
}

View File

@ -59,7 +59,7 @@ final class RandomFunctionRector extends AbstractRector
$node->name = new Name($newFunctionName);
// special case: random_int(); → random_int(0, getrandmax());
if ($newFunctionName === 'random_int' && count($node->args) === 0) {
if ($newFunctionName === 'random_int' && $node->args === []) {
$node->args[0] = new Arg(new LNumber(0));
$node->args[1] = new Arg($this->createFuncCall('mt_getrandmax'));
}

View File

@ -63,7 +63,7 @@ CODE_SAMPLE
return null;
}
if (count($node->args) > 0) {
if ($node->args !== []) {
return null;
}

View File

@ -86,7 +86,7 @@ final class RemoveExtraParametersRector extends AbstractRector
*/
private function shouldSkip(Node $node): bool
{
if (count($node->args) === 0) {
if ($node->args === []) {
return true;
}

View File

@ -109,7 +109,7 @@ CODE_SAMPLE
]);
// is key included? add it to foreach
if (count($listNode->items) > 0) {
if ($listNode->items !== []) {
/** @var ArrayItem|null $keyItem */
$keyItem = array_pop($listNode->items);

View File

@ -67,7 +67,7 @@ CODE_SAMPLE
return null;
}
if (count($node->stmts) > 0) {
if ($node->stmts !== []) {
return null;
}

View File

@ -131,7 +131,7 @@ CODE_SAMPLE
// 1. find self::$container->get(x)
$serviceTypes = $this->selfContainerMethodCallCollector->collectContainerGetServiceTypes($node, false);
if (count($serviceTypes) === 0) {
if ($serviceTypes === []) {
return null;
}

View File

@ -76,7 +76,7 @@ CODE_SAMPLE
/** @var Namespace_[] $namespaceNodes */
$namespaceNodes = $this->betterNodeFinder->findInstanceOf($nodes, Namespace_::class);
if (count($namespaceNodes) > 0) {
if ($namespaceNodes !== []) {
$this->processNamespaceNodes($smartFileInfo, $namespaceNodes, $nodes, $shouldDelete);
} else {
$this->processNodesWithoutNamespace($nodes, $smartFileInfo, $shouldDelete);

View File

@ -56,7 +56,7 @@ final class RemoveUselessJustForSakeInterfaceRector extends AbstractRector
*/
public function refactor(Node $node): ?Node
{
if (count((array) $node->implements) === 0) {
if ((array) $node->implements === []) {
return null;
}

View File

@ -69,7 +69,7 @@ final class BundleClassResolver
$controllerDirectory = dirname($controllerDirectory);
}
if (count($bundleFiles) === 0) {
if ($bundleFiles === []) {
return null;
}

View File

@ -175,7 +175,7 @@ CODE_SAMPLE
$this->refactorReturn($return, $classMethod, $sensioTemplateTagValueNode, $hasThisRenderOrReturnsResponse);
}
if (count($returns) === 0) {
if ($returns === []) {
$thisRenderMethodCall = $this->thisRenderFactory->create(
$classMethod,
null,

View File

@ -45,11 +45,11 @@ final class ClassMethodNodeRemover
public function removeClassMethodIfUseless(ClassMethod $classMethod): void
{
if (count((array) $classMethod->params) > 0) {
if ((array) $classMethod->params !== []) {
return;
}
if (count((array) $classMethod->stmts) > 0) {
if ((array) $classMethod->stmts !== []) {
return;
}
@ -71,7 +71,7 @@ final class ClassMethodNodeRemover
/** @var StaticCall $node */
$this->removeParamFromArgs($node, $paramName);
if (count($node->args) === 0) {
if ($node->args === []) {
$this->nodesToRemoveCollector->addNodeToRemove($node);
}
@ -88,7 +88,7 @@ final class ClassMethodNodeRemover
}
/** @var StaticCall $stmt */
if (count($stmt->args) > 0) {
if ($stmt->args !== []) {
continue;
}

View File

@ -277,7 +277,7 @@ CODE_SAMPLE
/** @var string $className */
$className = $class->getAttribute(AttributeKey::CLASS_NAME);
if (count($this->objectTypesToInject) === 0) {
if ($this->objectTypesToInject === []) {
return;
}

View File

@ -60,7 +60,7 @@ final class KernelTestCaseNodeFactory
public function createSetUpClassMethodWithGetTypes(Class_ $class, array $serviceTypes): ?ClassMethod
{
$assigns = $this->createSelfContainerGetWithTypeAssigns($class, $serviceTypes);
if (count($assigns) === 0) {
if ($assigns === []) {
return null;
}

View File

@ -118,7 +118,7 @@ CODE_SAMPLE
// 1. find self::$container->get(<X>)
$serviceTypes = $this->selfContainerMethodCallCollector->collectContainerGetServiceTypes($node);
if (count($serviceTypes) === 0) {
if ($serviceTypes === []) {
return null;
}

View File

@ -137,7 +137,7 @@ CODE_SAMPLE
unset($optionsArrayNode->items[$key]);
// remove empty array
if (count($optionsArrayNode->items) === 0) {
if ($optionsArrayNode->items === []) {
unset($methodCall->args[1]);
}

View File

@ -135,7 +135,7 @@ CODE_SAMPLE
return null;
}
if (count($argValue->args) > 0) {
if ($argValue->args !== []) {
$methodCall = $this->moveArgumentsToOptions(
$methodCall,
$position,

View File

@ -186,7 +186,7 @@ CODE_SAMPLE
$propertyFetchNode = $this->createPropertyFetch('this', $expectedName->getName());
if (count($funcCall->args) === 0) {
if ($funcCall->args === []) {
if ($argumentFuncCallToMethodCall->getMethodIfNoArgs()) {
return new MethodCall($propertyFetchNode, $argumentFuncCallToMethodCall->getMethodIfNoArgs());
}
@ -246,7 +246,7 @@ CODE_SAMPLE
ArrayFuncCallToMethodCall $arrayFuncCallToMethodCall,
PropertyFetch $propertyFetch
): ?Node {
if (count($funcCall->args) === 0) {
if ($funcCall->args === []) {
return $propertyFetch;
}

View File

@ -87,7 +87,7 @@ CODE_SAMPLE
*/
public function refactor(Node $node): ?Node
{
if (count($node->getParams()) === 0) {
if ($node->getParams() === []) {
return null;
}

View File

@ -106,7 +106,7 @@ CODE_SAMPLE
private function shouldSkip(ClassMethod $classMethod): bool
{
// skip class methods without args
if (count((array) $classMethod->params) === 0) {
if ((array) $classMethod->params === []) {
return true;
}

View File

@ -108,13 +108,13 @@ final class AssignToPropertyTypeInferer extends AbstractTypeInferer
$isAssignedInConstructor = $this->constructorAssignDetector->detect($classLike, $propertyName);
$shouldAddNullType = $this->nullTypeAssignDetector->detect($classLike, $propertyName);
if ((count($assignedExprTypes) === 0) && ($isAssignedInConstructor || $hasPropertyDefaultValue)) {
if (($assignedExprTypes === []) && ($isAssignedInConstructor || $hasPropertyDefaultValue)) {
return false;
}
if ($shouldAddNullType === true) {
return ! $isAssignedInConstructor && ! $hasPropertyDefaultValue;
}
return (count($assignedExprTypes) > 0) && (! $isAssignedInConstructor && ! $hasPropertyDefaultValue);
return ($assignedExprTypes !== []) && (! $isAssignedInConstructor && ! $hasPropertyDefaultValue);
}
}

View File

@ -121,7 +121,7 @@ final class PHPUnitDataProviderParamTypeInferer implements ParamTypeInfererInter
}
}
if (count($paramOnPositionTypes) === 0) {
if ($paramOnPositionTypes === []) {
return new MixedType();
}

View File

@ -74,7 +74,7 @@ final class PropertyTypeInferer extends AbstractPriorityAwareTypeInferer
}
// if nothing is clear from variable use, we use @var doc as fallback
if (count($resolvedTypes) > 0) {
if ($resolvedTypes !== []) {
$resolvedType = $this->typeFactory->createMixedPassedOrUnionType($resolvedTypes);
} else {
$resolvedType = $this->varDocPropertyTypeInferer->inferProperty($property);

View File

@ -43,7 +43,7 @@ final class YieldNodesReturnTypeInferer extends AbstractTypeInferer implements R
{
$yieldNodes = $this->findCurrentScopeYieldNodes($functionLike);
if (count($yieldNodes) === 0) {
if ($yieldNodes === []) {
return new MixedType();
}

View File

@ -89,7 +89,7 @@ final class AdditionalAutoloader
*/
private function autoloadDirectories(array $directories): void
{
if (count($directories) === 0) {
if ($directories === []) {
return;
}

View File

@ -139,7 +139,7 @@ final class Configuration
$commandLinePaths = (array) $input->getArgument(Option::SOURCE);
// manual command line value has priority
if (count($commandLinePaths) > 0) {
if ($commandLinePaths !== []) {
$this->paths = $commandLinePaths;
}
}

View File

@ -109,7 +109,7 @@ final class FilesFinder
*/
private function findInDirectories(array $directories, array $suffixes): array
{
if (count($directories) === 0) {
if ($directories === []) {
return [];
}

View File

@ -407,7 +407,7 @@ final class BetterStandardPrinter extends Standard
foreach ($class->stmts as $key => $stmt) {
// remove empty ones
if ($stmt instanceof TraitUse && count($stmt->traits) === 0) {
if ($stmt instanceof TraitUse && $stmt->traits === []) {
unset($class->stmts[$key]);
$shouldReindex = true;
}