Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/set/php-version-based.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
use Rector\Php80\Rector\NotIdentical\StrContainsRector;
use Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector;
use Rector\Php80\Rector\Ternary\GetDebugTypeRector;
use Rector\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector;
use Rector\Php81\Rector\Array_\ArrayToFirstClassCallableRector;
use Rector\Php81\Rector\Class_\MyCLabsClassToEnumRector;
use Rector\Php81\Rector\Class_\SpatieEnumClassToEnumRector;
Expand Down Expand Up @@ -257,6 +258,7 @@
StringableForToStringRector::class,
ClassOnObjectRector::class,
GetDebugTypeRector::class,
TernaryToNullsafeCoalesceRector::class,
RemoveUnusedVariableInCatchRector::class,
ClassPropertyAssignToConstructorPromotionRector::class,
ChangeSwitchToMatchRector::class,
Expand Down
2 changes: 2 additions & 0 deletions config/set/php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Rector\Php80\Rector\NotIdentical\StrContainsRector;
use Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector;
use Rector\Php80\Rector\Ternary\GetDebugTypeRector;
use Rector\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector;
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
use Rector\Transform\Rector\StaticCall\StaticCallToFuncCallRector;
use Rector\Transform\ValueObject\StaticCallToFuncCall;
Expand All @@ -35,6 +36,7 @@
StringableForToStringRector::class,
ClassOnObjectRector::class,
GetDebugTypeRector::class,
TernaryToNullsafeCoalesceRector::class,
RemoveUnusedVariableInCatchRector::class,
ClassPropertyAssignToConstructorPromotionRector::class,
ChangeSwitchToMatchRector::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Fixture;

use Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Source\SomeObject;

final class IdenticalNullFallback
{
public function run(?SomeObject $someObject): array
{
$name = null === $someObject ? null : $someObject->getName();
$property = $someObject === null ? null : $someObject->name;

return [$name, $property];
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Fixture;

use Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Source\SomeObject;

final class IdenticalNullFallback
{
public function run(?SomeObject $someObject): array
{
$name = $someObject?->getName();
$property = $someObject?->name;

return [$name, $property];
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Fixture;

use Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Source\SomeObject;

final class NullFallback
{
public function run(?SomeObject $someObject): array
{
$name = null !== $someObject ? $someObject->getName() : null;
$property = $someObject !== null ? $someObject->name : null;

return [$name, $property];
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Fixture;

use Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Source\SomeObject;

final class NullFallback
{
public function run(?SomeObject $someObject): array
{
$name = $someObject?->getName();
$property = $someObject?->name;

return [$name, $property];
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Fixture;

use Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Source\SomeObject;

final class SkipNullableReturn
{
public function run(?SomeObject $someObject): ?string
{
return null !== $someObject ? $someObject->findName() : '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Fixture;

use Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Source\SomeObject;

final class SkipSideEffectCheckedExpr
{
public function run(): ?string
{
return null !== $this->resolve() ? $this->resolve()->getName() : null;
}

private function resolve(): ?SomeObject
{
return new SomeObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Fixture;

use Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Source\SomeObject;

final class SkipWrappedCall
{
public function run(?SomeObject $someObject): string
{
return null !== $someObject ? strtoupper($someObject->getName()) : '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Fixture;

final class StringFallback
{
public function run(?\DateTimeImmutable $dateObso): string
{
return null !== $dateObso ? $dateObso->format('d/m/Y h:i:s') : '';
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Fixture;

final class StringFallback
{
public function run(?\DateTimeImmutable $dateObso): string
{
return $dateObso?->format('d/m/Y h:i:s') ?? '';
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector\Source;

final class SomeObject
{
public string $name = 'name';

public function getName(): string
{
return $this->name;
}

public function findName(): ?string
{
return $this->name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class TernaryToNullsafeCoalesceRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php80\Rector\Ternary\TernaryToNullsafeCoalesceRector;
use Rector\ValueObject\PhpVersion;

return RectorConfig::configure()
->withPhpVersion(PhpVersion::PHP_80)
->withRules([TernaryToNullsafeCoalesceRector::class]);
Loading
Loading