Skip to content

Commit 1925031

Browse files
[PHPUnit] Handle mix AssertCountWithZeroToAssertEmptyRector + AssertEmptyNullableObjectToAssertInstanceofRector cause invalid assertNotInstanceof() (#463)
* [PHPUnit] Handle mix AssertCountWithZeroToAssertEmptyRector + AssertEmptyNullableObjectToAssertInstanceofRector cause invalid assertNotInstanceof() * Fix * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 6b34ad2 commit 1925031

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

rules/CodeQuality/Rector/MethodCall/AssertCountWithZeroToAssertEmptyRector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public function refactor(Node $node): MethodCall|StaticCall|null
7575
return null;
7676
}
7777

78+
$secondType = $this->getType($node->getArgs()[1]->value);
79+
if ($secondType instanceof UnionType) {
80+
return null;
81+
}
82+
7883
$value = ($type->getConstantScalarValues()[0] ?? null);
7984
if ($value === 0) {
8085
$args = $node->getArgs();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\Issues\EmptyUnion;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class EmptyUnionTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\Issues\EmptyUnion\Fixture;
4+
5+
use ArrayIterator;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class SomeTest extends TestCase
9+
{
10+
public function testSomething()
11+
{
12+
$this->assertCount(0, $this->someCall());
13+
}
14+
15+
public function someCall(): ?ArrayIterator
16+
{
17+
return new ArrayIterator([]);
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertCountWithZeroToAssertEmptyRector;
7+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertEmptyNullableObjectToAssertInstanceofRector;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->rules([
11+
AssertCountWithZeroToAssertEmptyRector::class,
12+
AssertEmptyNullableObjectToAssertInstanceofRector::class,
13+
]);
14+
};

0 commit comments

Comments
 (0)