Skip to content

Commit

Permalink
[CodeQuality] Handle both ExplicitBoolCompareRector + CountArrayToEmp…
Browse files Browse the repository at this point in the history
…tyArrayComparisonRector usage (#474)
  • Loading branch information
samsonasik committed Jul 21, 2021
1 parent 692e9e3 commit 83419c4
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
3 changes: 2 additions & 1 deletion rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -107,7 +108,7 @@ public function refactor(Node $node): ?Node
}

$conditionStaticType = $this->getStaticType($conditionNode);
if ($conditionStaticType instanceof BooleanType) {
if ($conditionStaticType instanceof BooleanType || $conditionStaticType instanceof ConstantIntegerType) {
return null;
}

Expand Down
33 changes: 33 additions & 0 deletions tests/Issues/Issue6561/CountArrayOnTruthyEmptyArrayCompareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\Issue6561;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class CountArrayOnTruthyEmptyArrayCompareTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
33 changes: 33 additions & 0 deletions tests/Issues/Issue6561/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Core\Tests\Issues\Issue6561\Fixture;

final class Fixture
{
public function run()
{
$array = [];

if (count($array)) {
}
}
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\Issue6561\Fixture;

final class Fixture
{
public function run()
{
$array = [];

if ($array !== []) {
}
}
}

?>
13 changes: 13 additions & 0 deletions tests/Issues/Issue6561/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ExplicitBoolCompareRector::class);
$services->set(CountArrayToEmptyArrayComparisonRector::class);
};

0 comments on commit 83419c4

Please sign in to comment.