[Downgrade PHP 8.0] Fix DowngradeNullsafeToTernaryOperatorRector for trait (#412)

This commit is contained in:
Tomas Votruba 2021-07-10 02:35:36 +02:00 committed by GitHub
parent 3eeae8f598
commit a49c8612af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 3 deletions

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DowngradePhp80\Rector\Catch_\DowngradeNonCapturingCatchesRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionRector;
@ -39,6 +38,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
]]);
$services->set(DowngradeNamedArgumentRector::class);
$services->set(DowngradeAttributeToAnnotationRector::class)
->call('configure', [[
DowngradeAttributeToAnnotationRector::ATTRIBUTE_TO_ANNOTATION => ValueObjectInliner::inline([

View File

@ -0,0 +1,41 @@
<?php
namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;
use PhpParser\Node\Expr\ArrayItem;
final class GetNullable
{
public function run($value)
{
return $this->extractArrayItemByKey($value)?->value;
}
protected function extractArrayItemByKey($value): ?ArrayItem
{
return null;
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;
use PhpParser\Node\Expr\ArrayItem;
final class GetNullable
{
public function run($value)
{
return ($extractArrayItemByKey = $this->extractArrayItemByKey($value)) ? $extractArrayItemByKey->value : null;
}
protected function extractArrayItemByKey($value): ?ArrayItem
{
return null;
}
}
?>

View File

@ -0,0 +1,41 @@
<?php
namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;
use PhpParser\Node\Expr\ArrayItem;
trait GetNullableInTrait
{
public function run($value)
{
return $this->extractArrayItemByKey($value)?->value;
}
protected function extractArrayItemByKey($value): ?ArrayItem
{
return null;
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp80\Rector\NullsafeMethodCall\DowngradeNullsafeToTernaryOperatorRector\Fixture;
use PhpParser\Node\Expr\ArrayItem;
trait GetNullableInTrait
{
public function run($value)
{
return ($extractArrayItemByKey = $this->extractArrayItemByKey($value)) ? $extractArrayItemByKey->value : null;
}
protected function extractArrayItemByKey($value): ?ArrayItem
{
return null;
}
}
?>

View File

@ -58,9 +58,11 @@ CODE_SAMPLE
*/
public function refactor(Node $node): Ternary
{
$scope = $node->getAttribute(AttributeKey::SCOPE);
$tempVarName = $this->variableNaming->resolveFromNodeWithScopeCountAndFallbackName(
$node->var,
$node->getAttribute(AttributeKey::SCOPE),
$scope,
'_'
);

View File

@ -63,7 +63,7 @@ final class VariableNaming
public function resolveFromNodeWithScopeCountAndFallbackName(
Expr $expr,
Scope $scope,
?Scope $scope,
string $fallbackName
): string {
$name = $this->resolveFromNode($expr);