-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHP 8.1] Add NewInInitializerRector
- Loading branch information
1 parent
096817b
commit 2efc3bd
Showing
8 changed files
with
224 additions
and
4 deletions.
There are no files selected for viewing
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
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
31 changes: 31 additions & 0 deletions
31
rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/some_class.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,31 @@ | ||
<?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 | ||
{ | ||
private Logger $logger; | ||
|
||
public function __construct(Logger $logger = new NullLogger) | ||
{ | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/NewInInitializerRectorTest.php
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class NewInInitializerRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/config/configured_rule.php
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Php81\Rector\ClassMethod\NewInInitializerRector; | ||
|
||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
$services->set(NewInInitializerRector::class); | ||
}; |
112 changes: 112 additions & 0 deletions
112
rules/Php81/Rector/ClassMethod/NewInInitializerRector.php
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,112 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php81\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\BinaryOp\Coalesce; | ||
use PhpParser\Node\NullableType; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\ValueObject\MethodName; | ||
use Rector\Core\ValueObject\PhpVersionFeature; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\Php81\Rector\ClassMethod\NewInInitializerRector\NewInInitializerRectorTest | ||
*/ | ||
final class NewInInitializerRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Replace property declaration of new state with direct new', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
private Logger $logger; | ||
public function __construct( | ||
?Logger $logger = null, | ||
) { | ||
$this->logger = $logger ?? new NullLogger; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function __construct( | ||
private Logger $logger = new NullLogger, | ||
) { | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isName($node, MethodName::CONSTRUCT)) { | ||
return null; | ||
} | ||
|
||
if ($node->params === []) { | ||
return null; | ||
} | ||
|
||
if ($node->stmts === []) { | ||
return null; | ||
} | ||
|
||
foreach ($node->params as $param) { | ||
if (! $param->type instanceof NullableType) { | ||
continue; | ||
} | ||
|
||
/** @var string $paramName */ | ||
$paramName = $this->getName($param->var); | ||
|
||
$toPropertyAssigns = $this->betterNodeFinder->findClassMethodAssignsToLocalProperty($node, $paramName); | ||
|
||
foreach ($toPropertyAssigns as $toPropertyAssign) { | ||
if (! $toPropertyAssign->expr instanceof Coalesce) { | ||
continue; | ||
} | ||
|
||
/** @var NullableType $currentParamType */ | ||
$currentParamType = $param->type; | ||
$param->type = $currentParamType->type; | ||
|
||
$coalesce = $toPropertyAssign->expr; | ||
$param->default = $coalesce->right; | ||
|
||
$this->removeNode($toPropertyAssign); | ||
} | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::NEW_INITIALIZERS; | ||
} | ||
} |
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
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