Skip to content

Commit

Permalink
[CodingStyle] Handle NotIdentical on CountArrayToEmptyArrayComparison…
Browse files Browse the repository at this point in the history
…Rector (#144)

Co-authored-by: kaizen-ci <info@kaizen-ci.org>
  • Loading branch information
samsonasik and kaizen-ci committed Jun 3, 2021
1 parent 4fb6b2a commit 1913747
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector\Fixture;

class FixturNotIdentical
{
public function run()
{
function ($value, array $keys) {
return 0 !== count($keys) && is_array($value);
};
}

public function run2()
{
function ($value, array $keys) {
return count($keys) !== 0 && is_array($value);
};
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector\Fixture;

class FixturNotIdentical
{
public function run()
{
function ($value, array $keys) {
return [] !== $keys && is_array($value);
};
}

public function run2()
{
function ($value, array $keys) {
return $keys !== [] && is_array($value);
};
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private function isAlwaysTruableNode(Node $node): bool
}

// just one if
if (count($node->elseifs) !== 0) {
if ($node->elseifs !== []) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function refactor(Node $node): ?Node
return null;
}

$processIdentical = $this->processIdentical($parent, $node, $expr);
$processIdentical = $this->processIdenticalOrNotIdentical($parent, $node, $expr);
if ($processIdentical !== null) {
return $processIdentical;
}
Expand Down Expand Up @@ -131,16 +131,16 @@ private function isArray(Expr $expr): bool
return $scope->getType($expr) instanceof ArrayType;
}

private function processIdentical(Node $node, FuncCall $funcCall, Expr $expr): ?Expr
private function processIdenticalOrNotIdentical(Node $node, FuncCall $funcCall, Expr $expr): ?Expr
{
if ($node instanceof Identical && $node->right instanceof LNumber && $node->right->value === 0) {
if (($node instanceof Identical || $node instanceof NotIdentical) && $node->right instanceof LNumber && $node->right->value === 0) {
$this->removeNode($funcCall);
$node->right = new Array_([]);

return $expr;
}

if ($node instanceof Identical && $node->left instanceof LNumber && $node->left->value === 0) {
if (($node instanceof Identical || $node instanceof NotIdentical) && $node->left instanceof LNumber && $node->left->value === 0) {
$this->removeNode($funcCall);
$node->left = new Array_([]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function refactor(Node $node): ?Node
}

// just one if
if (count($node->elseifs) !== 0) {
if ($node->elseifs !== []) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private function shouldSkipClassMethod(?Class_ $class, MethodCall $methodCall):
return true;
}

return count((array) $classMethod->stmts) !== 0;
return (array) $classMethod->stmts !== [];
}

private function processArrowFunction(ArrowFunction $arrowFunction, MethodCall $methodCall): Expr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function configure(array $configuration): void
private function cleanupEmptyAttrGroups(ClassMethod | Property | Class_ $node): void
{
foreach ($node->attrGroups as $key => $attrGroup) {
if (count($attrGroup->attrs) !== 0) {
if ($attrGroup->attrs !== []) {
continue;
}

Expand Down

0 comments on commit 1913747

Please sign in to comment.