mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-29 14:13:30 +00:00
[Php81] Add ConstFetch and ClassConstFetch arg support on NewInInitializerRector (#1848)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
234721afaf
commit
88d7be3d93
|
@ -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))
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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))
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Source;
|
||||
|
||||
final class SomeValueObject
|
||||
{
|
||||
public const NOW = 'now';
|
||||
}
|
|
@ -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_) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user