-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Php81] Handle non-dynamic + dynamic args (non-array, non-scalar) pas…
…sed to New_ on NewInInitializerRector (#1737)
- Loading branch information
1 parent
79dd9bb
commit 73f5fc1
Showing
14 changed files
with
414 additions
and
22 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...ests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 DateTimeZone; | ||
|
||
class PassNonDynamicArg | ||
{ | ||
private DateTime $dateTime; | ||
|
||
public function __construct( | ||
?DateTime $dateTime = null | ||
) { | ||
$this->dateTime = $dateTime ?? new DateTime('now', new DateTimeZone('Asia/Jakarta')); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
use DateTime; | ||
use DateTimeZone; | ||
|
||
class PassNonDynamicArg | ||
{ | ||
public function __construct(private DateTime $dateTime = new DateTime('now', new DateTimeZone('Asia/Jakarta'))) | ||
{ | ||
} | ||
} | ||
|
||
?> |
29 changes: 29 additions & 0 deletions
29
...sts/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg2.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
private Logger $logger; | ||
|
||
public function __construct( | ||
?Logger $logger = null, | ||
) { | ||
$this->logger = $logger ?? new NullLogger(['a' => 'b']); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function __construct(private Logger $logger = new NullLogger(['a' => 'b'])) | ||
{ | ||
} | ||
} | ||
|
||
?> |
29 changes: 29 additions & 0 deletions
29
...sts/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg3.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
private Logger $logger; | ||
|
||
public function __construct( | ||
?Logger $logger = null, | ||
) { | ||
$this->logger = $logger ?? new NullLogger(['b']); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function __construct(private Logger $logger = new NullLogger(['b'])) | ||
{ | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...sts/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg4.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
use stdClass; | ||
|
||
class SomeClass | ||
{ | ||
private Logger $logger; | ||
|
||
public function __construct( | ||
?Logger $logger = null, | ||
) { | ||
$this->logger = $logger ?? new NullLogger([new stdClass]); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
use stdClass; | ||
|
||
class SomeClass | ||
{ | ||
public function __construct(private Logger $logger = new NullLogger([new stdClass])) | ||
{ | ||
} | ||
} | ||
|
||
?> |
29 changes: 29 additions & 0 deletions
29
...sts/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg5.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
private Logger $logger; | ||
|
||
public function __construct( | ||
?Logger $logger = null, | ||
) { | ||
$this->logger = $logger ?? new NullLogger([]); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function __construct(private Logger $logger = new NullLogger([])) | ||
{ | ||
} | ||
} | ||
|
||
?> |
29 changes: 29 additions & 0 deletions
29
...sts/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/pass_non_dynamic_arg6.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
private Logger $logger; | ||
|
||
public function __construct( | ||
?Logger $logger = null, | ||
) { | ||
$this->logger = $logger ?? new NullLogger(new Db()); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function __construct(private Logger $logger = new NullLogger(new Db())) | ||
{ | ||
} | ||
} | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...s/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_no_param_construct.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
class SkipNoParamConstruct | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...sts/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_pass_dynamic_arg.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
use DateTime; | ||
|
||
class SkipPassDynamicArg | ||
{ | ||
private DateTime $dateTime; | ||
|
||
public function __construct( | ||
?DateTime $dateTime = null | ||
) { | ||
$variable = 'now'; | ||
$this->dateTime = $dateTime ?? new DateTime($variable); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ts/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_pass_dynamic_arg2.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
use DateTime; | ||
|
||
class SkipPassDynamicArg2 | ||
{ | ||
private DateTime $dateTime; | ||
|
||
public function __construct( | ||
?DateTime $dateTime = null | ||
) { | ||
$timezone = 'Asia/Jakarta'; | ||
$this->dateTime = $dateTime ?? new DateTime('now', new DateTimeZone($timezone)); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ts/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_pass_dynamic_arg3.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
class SkipPassDynamicArg3 | ||
{ | ||
private Logger $logger; | ||
|
||
public function __construct( | ||
?Logger $logger = null, | ||
) { | ||
$x = 'b'; | ||
$this->logger = $logger ?? new NullLogger(['a' => $x]); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ts/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_pass_dynamic_arg4.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\Fixture; | ||
|
||
class SkipPassDynamicArg4 | ||
{ | ||
private Logger $logger; | ||
|
||
public function __construct( | ||
?Logger $logger = null, | ||
) { | ||
$x = 'b'; | ||
$this->logger = $logger ?? new NullLogger([$x => 'a']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php81\NodeAnalyzer; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
use PhpParser\Node\Expr\New_; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Scalar; | ||
use Rector\Core\NodeAnalyzer\ExprAnalyzer; | ||
|
||
final class ComplexNewAnalyzer | ||
{ | ||
public function __construct( | ||
private readonly ExprAnalyzer $exprAnalyzer | ||
) { | ||
} | ||
|
||
public function isDynamic(New_ $new): bool | ||
{ | ||
if (! $new->class instanceof FullyQualified) { | ||
return true; | ||
} | ||
|
||
$args = $new->getArgs(); | ||
|
||
foreach ($args as $arg) { | ||
$value = $arg->value; | ||
|
||
if ($this->isAllowedNew($value)) { | ||
continue; | ||
} | ||
|
||
if ($value instanceof Array_ && $this->isAllowedArray($value)) { | ||
continue; | ||
} | ||
|
||
if ($value instanceof Scalar) { | ||
continue; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function isAllowedNew(Expr $expr): bool | ||
{ | ||
if ($expr instanceof New_) { | ||
return ! $this->isDynamic($expr); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function isAllowedArray(Array_ $array): bool | ||
{ | ||
if (! $this->exprAnalyzer->isDynamicArray($array)) { | ||
return true; | ||
} | ||
|
||
$arrayItems = $array->items; | ||
foreach ($arrayItems as $arrayItem) { | ||
if (! $arrayItem instanceof ArrayItem) { | ||
continue; | ||
} | ||
|
||
if (! $arrayItem->value instanceof New_) { | ||
return false; | ||
} | ||
|
||
if ($this->isDynamic($arrayItem->value)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
Oops, something went wrong.