Skip to content

Commit

Permalink
[Php81] Add ConstFetch and ClassConstFetch arg support on NewInInitia…
Browse files Browse the repository at this point in the history
…lizerRector (#1848)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Feb 21, 2022
1 parent 234721a commit 88d7be3
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
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

0 comments on commit 88d7be3

Please sign in to comment.