Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
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
Original file line number Diff line number Diff line change
@@ -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))
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -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))
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

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

final class SomeValueObject
{
public const NOW = 'now';
}
21 changes: 21 additions & 0 deletions rules/Php81/NodeAnalyzer/ComplexNewAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
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;
Expand Down Expand Up @@ -42,12 +46,29 @@ public function isDynamic(New_ $new): bool
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_) {
Expand Down