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\Expr\Array_;
|
2022-07-03 20:44:15 +00:00
|
|
|
use PhpParser\Node\Expr\ArrayItem;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2022-07-03 20:44:15 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Type\Constant\ConstantArrayType;
|
2022-07-03 20:44:15 +00:00
|
|
|
use PHPStan\Type\Constant\ConstantStringType;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PHPStan\Type\MixedType;
|
|
|
|
use Rector\CodeQuality\CompactConverter;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
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://3v4l.org/8GJEs
|
2022-07-03 20:44:15 +00:00
|
|
|
*
|
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-07 08:22:29 +00:00
|
|
|
final class CompactToVariablesRector extends AbstractRector
|
2019-05-19 08:19:35 +00:00
|
|
|
{
|
2020-01-17 16:29:48 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +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;
|
2022-07-03 20:44:15 +00:00
|
|
|
public function __construct(CompactConverter $compactConverter)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2020-01-17 16:29:48 +00:00
|
|
|
$this->compactConverter = $compactConverter;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-05-19 08:19:35 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change compact() call to own array', [new 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-07 08:22:29 +00:00
|
|
|
return [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-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?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);
|
|
|
|
}
|
2022-11-07 17:28:06 +00:00
|
|
|
if ($node->isFirstClassCallable()) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-07-03 20:44:15 +00:00
|
|
|
$firstArg = $node->getArgs()[0];
|
2021-09-27 15:43:15 +00:00
|
|
|
$firstValue = $firstArg->value;
|
2021-10-07 17:46:41 +00:00
|
|
|
$firstValueStaticType = $this->getType($firstValue);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$firstValueStaticType instanceof ConstantArrayType) {
|
2021-02-21 09:32:45 +00:00
|
|
|
return null;
|
2021-02-05 12:40:28 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($firstValueStaticType->getItemType() instanceof MixedType) {
|
2021-02-21 09:32:45 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-07-03 20:44:15 +00:00
|
|
|
return $this->refactorAssignArray($firstValueStaticType);
|
2021-02-05 12:40:28 +00:00
|
|
|
}
|
2022-07-03 20:44:15 +00:00
|
|
|
private function refactorAssignArray(ConstantArrayType $constantArrayType) : ?Array_
|
2021-02-05 12:40:28 +00:00
|
|
|
{
|
2022-07-03 20:44:15 +00:00
|
|
|
$arrayItems = [];
|
|
|
|
foreach ($constantArrayType->getValueTypes() as $valueType) {
|
|
|
|
if (!$valueType instanceof ConstantStringType) {
|
2021-02-06 14:20:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-07-03 20:44:15 +00:00
|
|
|
$variableName = $valueType->getValue();
|
|
|
|
$variable = new Variable($variableName);
|
|
|
|
$arrayItems[] = new ArrayItem($variable, new String_($variableName));
|
2020-04-24 21:52:35 +00:00
|
|
|
}
|
2022-07-03 20:44:15 +00:00
|
|
|
return new Array_($arrayItems);
|
2019-05-19 08:19:35 +00:00
|
|
|
}
|
|
|
|
}
|