Skip to content

Commit 3e751ec

Browse files
authored
[CodeQuality] Add CoalesceToTernaryRector (#7960)
* [CodeQuality] Add CoalesceToTernaryRector * Fix phpstan * eol * final touch: fix fixture namespace * unregister from code quality set
1 parent b4e4c38 commit 3e751ec

File tree

11 files changed

+241
-0
lines changed

11 files changed

+241
-0
lines changed
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\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class CoalesceToTernaryRectorTest 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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\Fixture;
4+
5+
class NonNullableLeft
6+
{
7+
public function run(string $name)
8+
{
9+
return $name ?? 'tom';
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\Fixture;
18+
19+
class NonNullableLeft
20+
{
21+
public function run(string $name)
22+
{
23+
return $name ?: 'tom';
24+
}
25+
}
26+
27+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\Fixture;
4+
5+
final class SkipArrayDimFetch
6+
{
7+
public function run(array $data)
8+
{
9+
return $data['key'] ?? 'test';
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\Fixture;
4+
5+
class SkipDocblockBased
6+
{
7+
/**
8+
* @param string $name
9+
*/
10+
public function run($name)
11+
{
12+
return $name ?? 'tom';
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\Fixture;
4+
5+
class SkipMixedType
6+
{
7+
public function run(mixed $name)
8+
{
9+
return $name ?? 'tom';
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\Fixture;
4+
5+
class SkipNotDefined
6+
{
7+
public function run()
8+
{
9+
return $name ?? 'tom';
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\Fixture;
4+
5+
class SkipNullType
6+
{
7+
public function run(null $name)
8+
{
9+
return $name ?? 'tom';
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\Fixture;
4+
5+
class SkipNullableLeft
6+
{
7+
public function run(?string $name)
8+
{
9+
return $name ?? 'tom';
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\Fixture;
4+
5+
class SkipUnionNullableLeft
6+
{
7+
public function run(null|string $name)
8+
{
9+
return $name ?? 'tom';
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector;
6+
use Rector\Config\RectorConfig;
7+
8+
return RectorConfig::configure()
9+
->withRules([CoalesceToTernaryRector::class]);

0 commit comments

Comments
 (0)