reservedKeywordAnalyzer = $reservedKeywordAnalyzer;
$this->sideEffectNodeDetector = $sideEffectNodeDetector;
$this->variableAnalyzer = $variableAnalyzer;
$this->betterNodeFinder = $betterNodeFinder;
$this->stmtsManipulator = $stmtsManipulator;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Remove unused assigns to variables', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$value = 5;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
}
}
CODE_SAMPLE
)]);
}
/**
* @return array>
*/
public function getNodeTypes() : array
{
return [ClassMethod::class, Function_::class];
}
/**
* @param ClassMethod|Function_ $node
* @return null|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_
*/
public function refactorWithScope(Node $node, Scope $scope)
{
$stmts = $node->stmts;
if ($stmts === null || $stmts === []) {
return null;
}
// we cannot be sure here
if ($this->shouldSkip($stmts)) {
return null;
}
$assignedVariableNamesByStmtPosition = $this->resolvedAssignedVariablesByStmtPosition($stmts);
$hasChanged = \false;
foreach ($assignedVariableNamesByStmtPosition as $stmtPosition => $variableName) {
if ($this->stmtsManipulator->isVariableUsedInNextStmt($stmts, $stmtPosition + 1, $variableName)) {
continue;
}
/** @var Expression $currentStmt */
$currentStmt = $stmts[$stmtPosition];
/** @var Assign $assign */
$assign = $currentStmt->expr;
if ($this->hasCallLikeInAssignExpr($assign, $scope)) {
// clean safely
$cleanAssignedExpr = $this->cleanCastedExpr($assign->expr);
$newExpression = new Expression($cleanAssignedExpr);
$this->mirrorComments($newExpression, $currentStmt);
$node->stmts[$stmtPosition] = $newExpression;
} else {
unset($node->stmts[$stmtPosition]);
}
$hasChanged = \true;
}
if ($hasChanged) {
return $node;
}
return null;
}
private function cleanCastedExpr(Expr $expr) : Expr
{
if (!$expr instanceof Cast) {
return $expr;
}
return $this->cleanCastedExpr($expr->expr);
}
private function hasCallLikeInAssignExpr(Expr $expr, Scope $scope) : bool
{
return (bool) $this->betterNodeFinder->findFirst($expr, function (Node $subNode) use($scope) : bool {
return $this->sideEffectNodeDetector->detectCallExpr($subNode, $scope);
});
}
/**
* @param Stmt[] $stmts
*/
private function shouldSkip(array $stmts) : bool
{
return (bool) $this->betterNodeFinder->findFirst($stmts, function (Node $node) : bool {
if ($node instanceof Include_) {
return \true;
}
if (!$node instanceof FuncCall) {
return \false;
}
return $this->isName($node, 'compact');
});
}
/**
* @param array $stmts
* @return array
*/
private function resolvedAssignedVariablesByStmtPosition(array $stmts) : array
{
$assignedVariableNamesByStmtPosition = [];
$refVariableNames = [];
foreach ($stmts as $key => $stmt) {
if (!$stmt instanceof Expression) {
continue;
}
if ($stmt->expr instanceof AssignRef && $stmt->expr->var instanceof Variable) {
$refVariableNames[] = (string) $this->getName($stmt->expr->var);
}
if (!$stmt->expr instanceof Assign) {
continue;
}
$assign = $stmt->expr;
if (!$assign->var instanceof Variable) {
continue;
}
$variableName = $this->getName($assign->var);
if (!\is_string($variableName)) {
continue;
}
if ($this->reservedKeywordAnalyzer->isNativeVariable($variableName)) {
continue;
}
if ($this->shouldSkipVariable($assign->var, $variableName, $refVariableNames)) {
continue;
}
$assignedVariableNamesByStmtPosition[$key] = $variableName;
}
return $assignedVariableNamesByStmtPosition;
}
/**
* @param string[] $refVariableNames
*/
private function shouldSkipVariable(Variable $variable, string $variableName, array $refVariableNames) : bool
{
if ($this->variableAnalyzer->isStaticOrGlobal($variable)) {
return \true;
}
if ($this->variableAnalyzer->isUsedByReference($variable)) {
return \true;
}
return \in_array($variableName, $refVariableNames, \true);
}
}