Skip to content

Commit

Permalink
[PHP 7.3] Move RemoveMissingCompactVariableRector (#5373)
Browse files Browse the repository at this point in the history
* [PHP 7.3] Move RemoveMissingCompactVariableRector

* [ci-review] Rector Rectify

Co-authored-by: rector-bot <tomas@getrector.org>
  • Loading branch information
TomasVotruba and rector-bot authored Jan 30, 2021
1 parent fcc6666 commit 1fc272e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Php73\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\FuncCall;
Expand Down Expand Up @@ -114,16 +115,35 @@ private function unsetUnusedArrayElements(FuncCall $funcCall, Scope $scope): voi
private function unsetUnusedArguments(FuncCall $funcCall, Scope $scope): void
{
foreach ($funcCall->args as $key => $arg) {
if ($arg->value instanceof Array_) {
continue;
}
$variablesNames = $this->resolveVariableNames($arg);
foreach ($variablesNames as $variablesName) {
if (! $scope->hasVariableType($variablesName)->no()) {
continue;
}

$argValue = $this->getValue($arg->value);
if (! $scope->hasVariableType($argValue)->no()) {
continue;
unset($funcCall->args[$key]);
}
}

unset($funcCall->args[$key]);
if ($funcCall->args === []) {
$this->removeNode($funcCall);
}
}

/**
* @return string[]|mixed[]
*/
private function resolveVariableNames(Arg $arg): array
{
$argValue = $this->getValue($arg->value);
if (is_string($argValue)) {
return [$argValue];
}

if (is_array($argValue)) {
return $argValue;
}

return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\Php73\Tests\Rector\FuncCall\RemoveMissingCompactVariableRector\Fixture;

final class MultiTypes
{
public function run()
{
$variables = ['foo', 'bar'];
compact($variables);
}
}

?>
-----
<?php

namespace Rector\Php73\Tests\Rector\FuncCall\RemoveMissingCompactVariableRector\Fixture;

final class MultiTypes
{
public function run()
{
$variables = ['foo', 'bar'];
}
}

?>
7 changes: 2 additions & 5 deletions src/PhpParser/Parser/FunctionLikeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ final class FunctionLikeParser
*/
private $nodeFinder;

public function __construct(
Parser $parser,
SmartFileSystem $smartFileSystem,
NodeFinder $nodeFinder
) {
public function __construct(Parser $parser, SmartFileSystem $smartFileSystem, NodeFinder $nodeFinder)
{
$this->parser = $parser;
$this->smartFileSystem = $smartFileSystem;
$this->nodeFinder = $nodeFinder;
Expand Down

0 comments on commit 1fc272e

Please sign in to comment.