Skip to content

Commit 0079a9c

Browse files
committed
Updated Rector to commit 3e751eca2d58330e8528e12056172508fc314a4b
rectorphp/rector-src@3e751ec [CodeQuality] Add CoalesceToTernaryRector (#7960)
1 parent 84bfd7c commit 0079a9c

4 files changed

Lines changed: 84 additions & 2 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\CodeQuality\Rector\Coalesce;
5+
6+
use PHPStan\Type\UnionType;
7+
use PHPStan\Type\NullType;
8+
use PhpParser\Node;
9+
use PhpParser\Node\Expr\ArrayDimFetch;
10+
use PhpParser\Node\Expr\BinaryOp\Coalesce;
11+
use PhpParser\Node\Expr\Ternary;
12+
use PHPStan\Type\ErrorType;
13+
use PHPStan\Type\MixedType;
14+
use Rector\Rector\AbstractRector;
15+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
16+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
17+
use Rector\PHPStan\ScopeFetcher;
18+
/**
19+
* @see \Rector\Tests\CodeQuality\Rector\Coalesce\CoalesceToTernaryRector\CoalesceToTernaryRectorTest
20+
*/
21+
final class CoalesceToTernaryRector extends AbstractRector
22+
{
23+
public function getRuleDefinition(): RuleDefinition
24+
{
25+
return new RuleDefinition('Replace coalesce to ternary when left side is non nullable', [new CodeSample(<<<'CODE_SAMPLE'
26+
function run(string $a)
27+
{
28+
return $a ?? 'foo';
29+
}
30+
CODE_SAMPLE
31+
, <<<'CODE_SAMPLE'
32+
function run(string $a)
33+
{
34+
return $a ?: 'foo';
35+
}
36+
CODE_SAMPLE
37+
)]);
38+
}
39+
/**
40+
* @return array<class-string<Node>>
41+
*/
42+
public function getNodeTypes(): array
43+
{
44+
return [Coalesce::class];
45+
}
46+
/**
47+
* @param Coalesce $node
48+
*/
49+
public function refactor(Node $node): ?Node
50+
{
51+
/**
52+
* indexed data maybe false positive
53+
*/
54+
if ($node->left instanceof ArrayDimFetch) {
55+
return null;
56+
}
57+
/**
58+
* Scope needs to use parent Coalesce to properly get type from left side of coalesce
59+
*/
60+
$scope = ScopeFetcher::fetch($node);
61+
$nativeType = $scope->getNativeType($node->left);
62+
if ($nativeType instanceof ErrorType) {
63+
return null;
64+
}
65+
if ($nativeType instanceof MixedType) {
66+
return null;
67+
}
68+
if ($nativeType instanceof NullType) {
69+
return null;
70+
}
71+
if ($nativeType instanceof UnionType) {
72+
foreach ($nativeType->getTypes() as $unionedType) {
73+
if ($unionedType instanceof NullType) {
74+
return null;
75+
}
76+
}
77+
}
78+
return new Ternary($node->left, null, $node->right);
79+
}
80+
}

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = 'b4e4c3814ab45e1b7e85b46b2ad8f4c90e8da4b3';
22+
public const PACKAGE_VERSION = '3e751eca2d58330e8528e12056172508fc314a4b';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2026-04-06 16:35:53';
27+
public const RELEASE_DATE = '2026-04-06 19:56:56';
2828
/**
2929
* @var int
3030
*/

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,7 @@
11921192
'Rector\\CodeQuality\\Rector\\Class_\\InlineConstructorDefaultToPropertyRector' => $baseDir . '/rules/CodeQuality/Rector/Class_/InlineConstructorDefaultToPropertyRector.php',
11931193
'Rector\\CodeQuality\\Rector\\Class_\\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector' => $baseDir . '/rules/CodeQuality/Rector/Class_/RemoveReadonlyPropertyVisibilityOnReadonlyClassRector.php',
11941194
'Rector\\CodeQuality\\Rector\\Class_\\ReturnIteratorInDataProviderRector' => $baseDir . '/rules/CodeQuality/Rector/Class_/ReturnIteratorInDataProviderRector.php',
1195+
'Rector\\CodeQuality\\Rector\\Coalesce\\CoalesceToTernaryRector' => $baseDir . '/rules/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector.php',
11951196
'Rector\\CodeQuality\\Rector\\Concat\\DirnameDirConcatStringToDirectStringPathRector' => $baseDir . '/rules/CodeQuality/Rector/Concat/DirnameDirConcatStringToDirectStringPathRector.php',
11961197
'Rector\\CodeQuality\\Rector\\Concat\\JoinStringConcatRector' => $baseDir . '/rules/CodeQuality/Rector/Concat/JoinStringConcatRector.php',
11971198
'Rector\\CodeQuality\\Rector\\Empty_\\SimplifyEmptyCheckOnEmptyArrayRector' => $baseDir . '/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php',

vendor/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,7 @@ class ComposerStaticInitc9819cb6f85619d8e1cbb4426044dd33
14521452
'Rector\\CodeQuality\\Rector\\Class_\\InlineConstructorDefaultToPropertyRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Class_/InlineConstructorDefaultToPropertyRector.php',
14531453
'Rector\\CodeQuality\\Rector\\Class_\\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Class_/RemoveReadonlyPropertyVisibilityOnReadonlyClassRector.php',
14541454
'Rector\\CodeQuality\\Rector\\Class_\\ReturnIteratorInDataProviderRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Class_/ReturnIteratorInDataProviderRector.php',
1455+
'Rector\\CodeQuality\\Rector\\Coalesce\\CoalesceToTernaryRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Coalesce/CoalesceToTernaryRector.php',
14551456
'Rector\\CodeQuality\\Rector\\Concat\\DirnameDirConcatStringToDirectStringPathRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Concat/DirnameDirConcatStringToDirectStringPathRector.php',
14561457
'Rector\\CodeQuality\\Rector\\Concat\\JoinStringConcatRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Concat/JoinStringConcatRector.php',
14571458
'Rector\\CodeQuality\\Rector\\Empty_\\SimplifyEmptyCheckOnEmptyArrayRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php',

0 commit comments

Comments
 (0)