2022-03-15 17:33:16 +00:00
< ? php
declare ( strict_types = 1 );
2022-06-06 17:12:56 +00:00
namespace Rector\CodeQuality\Rector\Class_ ;
2022-03-15 17:33:16 +00:00
2022-06-06 17:12:56 +00:00
use PhpParser\Node ;
use PhpParser\Node\Stmt\Class_ ;
use PhpParser\Node\Stmt\ClassMethod ;
use PhpParser\Node\Stmt\Property ;
use Rector\CodeQuality\NodeAnalyzer\ConstructorPropertyDefaultExprResolver ;
use Rector\Core\Rector\AbstractRector ;
use Rector\Core\ValueObject\MethodName ;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2022-03-15 17:33:16 +00:00
/**
* @ see \Rector\Tests\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector\InlineConstructorDefaultToPropertyRectorTest
*/
2022-06-06 17:12:56 +00:00
final class InlineConstructorDefaultToPropertyRector extends \Rector\Core\Rector\AbstractRector
2022-03-15 17:33:16 +00:00
{
/**
* @ readonly
* @ var \Rector\CodeQuality\NodeAnalyzer\ConstructorPropertyDefaultExprResolver
*/
private $constructorPropertyDefaultExprResolver ;
2022-06-06 17:12:56 +00:00
public function __construct ( \Rector\CodeQuality\NodeAnalyzer\ConstructorPropertyDefaultExprResolver $constructorPropertyDefaultExprResolver )
2022-03-15 17:33:16 +00:00
{
$this -> constructorPropertyDefaultExprResolver = $constructorPropertyDefaultExprResolver ;
}
2022-06-06 17:12:56 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2022-03-15 17:33:16 +00:00
{
2022-06-06 17:12:56 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Move property default from constructor to property default' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2022-03-15 17:33:16 +00:00
final class SomeClass
{
private $name ;
public function __construct ()
{
$this -> name = 'John' ;
}
}
CODE_SAMPLE
, <<< 'CODE_SAMPLE'
final class SomeClass
{
private $name = 'John' ;
}
CODE_SAMPLE
)]);
}
/**
* @ return array < class - string < Node >>
*/
public function getNodeTypes () : array
{
2022-06-06 17:12:56 +00:00
return [ \PhpParser\Node\Stmt\Class_ :: class ];
2022-03-15 17:33:16 +00:00
}
/**
* @ param Class_ $node
*/
2022-06-06 17:12:56 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2022-03-15 17:33:16 +00:00
{
2022-06-06 17:12:56 +00:00
$constructClassMethod = $node -> getMethod ( \Rector\Core\ValueObject\MethodName :: CONSTRUCT );
if ( ! $constructClassMethod instanceof \PhpParser\Node\Stmt\ClassMethod ) {
2022-03-15 17:33:16 +00:00
return null ;
}
// resolve property defaults
$defaultPropertyExprAssigns = $this -> constructorPropertyDefaultExprResolver -> resolve ( $constructClassMethod );
if ( $defaultPropertyExprAssigns === []) {
return null ;
}
$hasChanged = \false ;
foreach ( $defaultPropertyExprAssigns as $defaultPropertyExprAssign ) {
$property = $node -> getProperty ( $defaultPropertyExprAssign -> getPropertyName ());
2022-06-06 17:12:56 +00:00
if ( ! $property instanceof \PhpParser\Node\Stmt\Property ) {
2022-03-15 17:33:16 +00:00
continue ;
}
$propertyProperty = $property -> props [ 0 ];
$propertyProperty -> default = $defaultPropertyExprAssign -> getDefaultExpr ();
$hasChanged = \true ;
$this -> removeNode ( $defaultPropertyExprAssign -> getAssignExpression ());
}
if ( ! $hasChanged ) {
return null ;
}
return $node ;
}
}