2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2022-06-06 17:12:56 +00:00
namespace Rector\CodeQuality\Rector\FuncCall ;
2019-05-19 08:19:35 +00:00
2022-06-06 17:12:56 +00:00
use PhpParser\Node ;
use PhpParser\Node\Arg ;
use PhpParser\Node\Expr ;
use PhpParser\Node\Expr\Array_ ;
use PhpParser\Node\Expr\Assign ;
use PhpParser\Node\Expr\FuncCall ;
use PhpParser\Node\Stmt ;
use PHPStan\Analyser\Scope ;
use PHPStan\Type\Constant\ConstantArrayType ;
use PHPStan\Type\MixedType ;
use Rector\CodeQuality\CompactConverter ;
use Rector\CodeQuality\NodeAnalyzer\ArrayCompacter ;
use Rector\CodeQuality\NodeAnalyzer\ArrayItemsAnalyzer ;
use Rector\Core\Rector\AbstractRector ;
use Rector\NodeTypeResolver\Node\AttributeKey ;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2019-05-19 08:19:35 +00:00
/**
2021-04-10 18:47:17 +00:00
* @ changelog https :// stackoverflow . com / a / 16319909 / 1348344
2022-05-12 19:17:07 +00:00
* @ changelog https :// 3 v4l . org / 8 GJEs
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\CodeQuality\Rector\FuncCall\CompactToVariablesRector\CompactToVariablesRectorTest
2019-05-19 08:19:35 +00:00
*/
2022-06-06 17:12:56 +00:00
final class CompactToVariablesRector extends \Rector\Core\Rector\AbstractRector
2019-05-19 08:19:35 +00:00
{
2020-01-17 16:29:48 +00:00
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-05-10 23:39:21 +00:00
* @ var \Rector\CodeQuality\CompactConverter
2020-01-17 16:29:48 +00:00
*/
private $compactConverter ;
2021-02-05 14:15:51 +00:00
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-05-10 23:39:21 +00:00
* @ var \Rector\CodeQuality\NodeAnalyzer\ArrayItemsAnalyzer
2021-02-05 14:15:51 +00:00
*/
2021-02-06 14:20:09 +00:00
private $arrayItemsAnalyzer ;
2021-02-05 14:15:51 +00:00
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-05-10 23:39:21 +00:00
* @ var \Rector\CodeQuality\NodeAnalyzer\ArrayCompacter
2021-02-05 14:15:51 +00:00
*/
private $arrayCompacter ;
2022-06-06 17:12:56 +00:00
public function __construct ( \Rector\CodeQuality\CompactConverter $compactConverter , \Rector\CodeQuality\NodeAnalyzer\ArrayItemsAnalyzer $arrayItemsAnalyzer , \Rector\CodeQuality\NodeAnalyzer\ArrayCompacter $arrayCompacter )
2021-05-09 20:15:43 +00:00
{
2020-01-17 16:29:48 +00:00
$this -> compactConverter = $compactConverter ;
2021-02-06 14:20:09 +00:00
$this -> arrayItemsAnalyzer = $arrayItemsAnalyzer ;
2021-02-05 14:15:51 +00:00
$this -> arrayCompacter = $arrayCompacter ;
2020-01-17 16:29:48 +00:00
}
2022-06-06 17:12:56 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2019-05-19 08:19:35 +00:00
{
2022-06-06 17:12:56 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Change compact() call to own array' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2019-05-19 08:19:35 +00:00
class SomeClass
{
public function run ()
{
$checkout = 'one' ;
$form = 'two' ;
return compact ( 'checkout' , 'form' );
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2019-05-19 08:19:35 +00:00
class SomeClass
{
public function run ()
{
$checkout = 'one' ;
$form = 'two' ;
return [ 'checkout' => $checkout , 'form' => $form ];
}
}
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2019-05-19 08:19:35 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2019-05-19 08:19:35 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2019-05-19 08:19:35 +00:00
{
2022-06-06 17:12:56 +00:00
return [ \PhpParser\Node\Expr\FuncCall :: class ];
2019-05-19 08:19:35 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param FuncCall $node
2019-05-19 08:19:35 +00:00
*/
2022-06-06 17:12:56 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2019-05-19 08:19:35 +00:00
{
2021-05-09 20:15:43 +00:00
if ( ! $this -> isName ( $node , 'compact' )) {
2019-05-19 08:19:35 +00:00
return null ;
}
2021-02-05 12:40:28 +00:00
if ( $this -> compactConverter -> hasAllArgumentsNamed ( $node )) {
return $this -> compactConverter -> convertToArray ( $node );
}
2021-09-27 15:43:15 +00:00
/** @var Arg $firstArg */
$firstArg = $node -> args [ 0 ];
$firstValue = $firstArg -> value ;
2021-10-07 17:46:41 +00:00
$firstValueStaticType = $this -> getType ( $firstValue );
2022-06-06 17:12:56 +00:00
if ( ! $firstValueStaticType instanceof \PHPStan\Type\Constant\ConstantArrayType ) {
2021-02-21 09:32:45 +00:00
return null ;
2021-02-05 12:40:28 +00:00
}
2022-06-06 17:12:56 +00:00
if ( $firstValueStaticType -> getItemType () instanceof \PHPStan\Type\MixedType ) {
2021-02-21 09:32:45 +00:00
return null ;
}
return $this -> refactorAssignArray ( $firstValue , $node );
2021-02-05 12:40:28 +00:00
}
2022-06-06 17:12:56 +00:00
private function refactorAssignedArray ( \PhpParser\Node\Expr\Assign $assign , \PhpParser\Node\Expr\FuncCall $funcCall , \PhpParser\Node\Expr $expr ) : ? \PhpParser\Node\Expr
2021-02-05 12:40:28 +00:00
{
2022-06-06 17:12:56 +00:00
if ( ! $assign -> expr instanceof \PhpParser\Node\Expr\Array_ ) {
2021-02-06 14:20:09 +00:00
return null ;
2021-02-05 14:15:51 +00:00
}
$array = $assign -> expr ;
2022-06-06 17:12:56 +00:00
$assignScope = $assign -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: SCOPE );
if ( ! $assignScope instanceof \PHPStan\Analyser\Scope ) {
2021-02-06 14:20:09 +00:00
return null ;
2021-02-05 14:15:51 +00:00
}
2022-05-11 17:53:44 +00:00
$currentStmt = $this -> betterNodeFinder -> resolveCurrentStatement ( $funcCall );
2022-06-06 17:12:56 +00:00
if ( ! $currentStmt instanceof \PhpParser\Node\Stmt ) {
2022-04-24 17:30:55 +00:00
return null ;
}
2021-05-09 20:15:43 +00:00
$isCompactOfUndefinedVariables = $this -> arrayItemsAnalyzer -> hasArrayExclusiveDefinedVariableNames ( $array , $assignScope );
2021-02-06 14:20:09 +00:00
if ( $isCompactOfUndefinedVariables ) {
2022-06-06 17:12:56 +00:00
$funcCallScope = $funcCall -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: SCOPE );
if ( ! $funcCallScope instanceof \PHPStan\Analyser\Scope ) {
2021-02-06 14:20:09 +00:00
return null ;
}
2021-05-09 20:15:43 +00:00
$isCompactOfDefinedVariables = $this -> arrayItemsAnalyzer -> hasArrayExclusiveUndefinedVariableNames ( $array , $funcCallScope );
2021-02-06 14:20:09 +00:00
if ( $isCompactOfDefinedVariables ) {
$this -> arrayCompacter -> compactStringToVariableArray ( $array );
return $expr ;
}
2021-02-05 12:40:28 +00:00
}
2021-02-05 14:15:51 +00:00
$this -> removeNode ( $assign );
$this -> arrayCompacter -> compactStringToVariableArray ( $array );
2021-09-27 15:43:15 +00:00
/** @var Arg $firstArg */
$firstArg = $funcCall -> args [ 0 ];
$assignVariable = $firstArg -> value ;
2022-06-06 17:12:56 +00:00
$preAssign = new \PhpParser\Node\Expr\Assign ( $assignVariable , $array );
2022-05-12 09:11:03 +00:00
$this -> nodesToAddCollector -> addNodeBeforeNode ( $preAssign , $currentStmt , $this -> file -> getSmartFileInfo ());
2021-02-06 14:20:09 +00:00
return $expr ;
2021-02-05 12:40:28 +00:00
}
2022-06-06 17:12:56 +00:00
private function refactorAssignArray ( \PhpParser\Node\Expr $expr , \PhpParser\Node\Expr\FuncCall $funcCall ) : ? \PhpParser\Node\Expr
2021-02-05 12:40:28 +00:00
{
$previousAssign = $this -> betterNodeFinder -> findPreviousAssignToExpr ( $expr );
2022-06-06 17:12:56 +00:00
if ( ! $previousAssign instanceof \PhpParser\Node\Expr\Assign ) {
2020-04-24 21:52:35 +00:00
return null ;
}
2021-02-06 14:20:09 +00:00
return $this -> refactorAssignedArray ( $previousAssign , $funcCall , $expr );
2019-05-19 08:19:35 +00:00
}
}