Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHP 7.3] Move RemoveMissingCompactVariableRector #5373

Merged
merged 2 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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