Skip to content

Commit

Permalink
[Php81] Skip static call in right expr of Coalesce on NewInInitialize…
Browse files Browse the repository at this point in the history
…rRector (#1670)

* [Php81] Skip static call on NewInInitializerRector

* Fixed 🎉

* final touch: restructure fixtures

* final touch: eol

* use not instanceof New_ check
  • Loading branch information
samsonasik committed Jan 14, 2022
1 parent 6ea0032 commit c931954
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture;

use Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Source\InstantiableViaNamedConstructor;

final class SkipStaticCallNullCoalesceOperator
{
public function __construct(private ?InstantiableViaNamedConstructor $foo = null)
{
$this->foo ??= InstantiableViaNamedConstructor::make(100);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture;

use Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Source\InstantiableViaNamedConstructor;

final class SkipStaticCallRightCoalesce
{
private InstantiableViaNamedConstructor $foo;

public function __construct(?InstantiableViaNamedConstructor $foo = null)
{
$this->foo = $foo ?? InstantiableViaNamedConstructor::make(100);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Source;

final class InstantiableViaNamedConstructor
{
public static function make(int $value): InstantiableViaNamedConstructor
{
return new InstantiableViaNamedConstructor($value);
}

public function __construct(public int $value)
{
}
}
5 changes: 5 additions & 0 deletions rules/Php81/Rector/ClassMethod/NewInInitializerRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -95,6 +96,10 @@ public function refactor(Node $node): ?Node
continue;
}

if (! $toPropertyAssign->expr->right instanceof New_) {
continue;
}

/** @var NullableType $currentParamType */
$currentParamType = $param->type;
$param->type = $currentParamType->type;
Expand Down

0 comments on commit c931954

Please sign in to comment.