[Php81] Add ConstFetch and ClassConstFetch arg support on NewInInitializerRector (#1848)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2022-02-21 18:23:33 +07:00 committed by GitHub
parent 234721afaf
commit 88d7be3d93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,35 @@
<?php
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture;
use DateTime;
use Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Source\SomeValueObject;
class PassClassConstFetchArg
{
private DateTime $dateTime;
public function __construct(
?DateTime $dateTime = null
) {
$this->dateTime = $dateTime ?? new DateTime(SomeValueObject::NOW);
}
}
?>
-----
<?php
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture;
use DateTime;
use Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Source\SomeValueObject;
class PassClassConstFetchArg
{
public function __construct(private DateTime $dateTime = new DateTime(SomeValueObject::NOW))
{
}
}
?>

View File

@ -0,0 +1,37 @@
<?php
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture;
use DateTime;
const NOW = 'now';
class PassConstFetchArg
{
private DateTime $dateTime;
public function __construct(
?DateTime $dateTime = null
) {
$this->dateTime = $dateTime ?? new DateTime(NOW);
}
}
?>
-----
<?php
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture;
use DateTime;
const NOW = 'now';
class PassConstFetchArg
{
public function __construct(private DateTime $dateTime = new DateTime(NOW))
{
}
}
?>

View File

@ -0,0 +1,18 @@
<?php
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture;
use DateTime;
use Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Source\SomeValueObject;
class SkipPassClassConstFetchArgDynamicClass
{
private DateTime $dateTime;
public function __construct(
?DateTime $dateTime = null
) {
$class = SomeValueObject::class;
$this->dateTime = $dateTime ?? new DateTime($class::NOW);
}
}

View File

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Source;
final class SomeValueObject
{
public const NOW = 'now';
}

View File

@ -7,7 +7,11 @@ namespace Rector\Php81\NodeAnalyzer;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar;
use Rector\Core\NodeAnalyzer\ExprAnalyzer;
@ -42,12 +46,29 @@ final class ComplexNewAnalyzer
continue;
}
if ($this->isAllowedConstFetchOrClassConstFeth($value)) {
continue;
}
return true;
}
return false;
}
private function isAllowedConstFetchOrClassConstFeth(Expr $expr): bool
{
if (! in_array($expr::class, [ConstFetch::class, ClassConstFetch::class], true)) {
return false;
}
if ($expr instanceof ClassConstFetch) {
return $expr->class instanceof Name && $expr->name instanceof Identifier;
}
return true;
}
private function isAllowedNew(Expr $expr): bool
{
if ($expr instanceof New_) {