Self Assignment

ID

php.self_assignment

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Php

Tags

CWE:480, reliability, suspicious-comparison

Description

Reports plain assignments whose left-hand side and assigned value are the same expression, for example $x = $x;. The assignment is a no-op and almost always signals a copy-paste mistake or a missing $this→ qualifier on one side.

Rationale

A statement like $count = $count; does nothing, yet it reads as if it should. The most common real-world cause is a constructor or setter that means to copy a parameter into a property but forgets the $this→ qualifier, e.g. writing $name = $name; instead of $this→name = $name; — leaving the property unassigned. Flagging the no-op surfaces the bug.

<?php
class Account
{
    private int $balance;

    public function __construct(int $balance)
    {
        $balance = $balance;        // FLAW — assigns the parameter to itself; property stays unset
    }

    public function reset(int $balance): void
    {
        $this->balance = $balance;  // OK — qualified, copies parameter into the property
        $this->balance += $balance; // OK — augmented assignment is not a no-op
    }
}

Remediation

Decide which side was intended. If a property should receive a parameter, qualify it with $this→ ($this→balance = $balance;). If the statement is genuinely redundant, delete it.