2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-05-19 08:19:35 +00:00
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\FuncCall;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2020-01-17 16:29:48 +00:00
|
|
|
use Rector\CodeQuality\CompactConverter;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\RectorDefinition\CodeSample;
|
|
|
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
2019-05-19 08:19:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://stackoverflow.com/a/16319909/1348344
|
|
|
|
* @see https://3v4l.org/8GJEs
|
2019-09-03 09:11:45 +00:00
|
|
|
* @see \Rector\CodeQuality\Tests\Rector\FuncCall\CompactToVariablesRector\CompactToVariablesRectorTest
|
2019-05-19 08:19:35 +00:00
|
|
|
*/
|
|
|
|
final class CompactToVariablesRector extends AbstractRector
|
|
|
|
{
|
2020-01-17 16:29:48 +00:00
|
|
|
/**
|
|
|
|
* @var CompactConverter
|
|
|
|
*/
|
|
|
|
private $compactConverter;
|
|
|
|
|
|
|
|
public function __construct(CompactConverter $compactConverter)
|
|
|
|
{
|
|
|
|
$this->compactConverter = $compactConverter;
|
|
|
|
}
|
|
|
|
|
2019-05-19 08:19:35 +00:00
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition('Change compact() call to own array', [
|
|
|
|
new CodeSample(
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2019-05-19 08:19:35 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$checkout = 'one';
|
|
|
|
$form = 'two';
|
|
|
|
|
|
|
|
return compact('checkout', 'form');
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2019-05-19 08:19:35 +00:00
|
|
|
,
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2019-05-19 08:19:35 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$checkout = 'one';
|
|
|
|
$form = 'two';
|
|
|
|
|
|
|
|
return ['checkout' => $checkout, 'form' => $form];
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2019-05-19 08:19:35 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [FuncCall::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if (! $this->isName($node, 'compact')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-04-24 21:52:35 +00:00
|
|
|
if (! $this->compactConverter->hasAllArgumentsNamed($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-17 16:29:48 +00:00
|
|
|
return $this->compactConverter->convertToArray($node);
|
2019-05-19 08:19:35 +00:00
|
|
|
}
|
|
|
|
}
|