Return Without Parens

ID

php.return_without_parens

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Php

Tags

code_smell, redundant-code

Description

Reports a return whose entire value is wrapped in a single pair of parentheses, such as return ($x); or return ($a + $b);. The parentheses surround the whole expression and serve no purpose.

Rationale

return is a language statement, not a function. Wrapping its value in parentheses makes the code read as though return were being called like return(…​), which is misleading, and the extra characters add noise without changing precedence. Dropping them yields cleaner, more idiomatic code. Parentheses that actually do something — a cast, a call’s argument list, or grouping inside a larger expression — are left untouched.

<?php
function a(int $x): int
{
    return ($x);        // FLAW — redundant wrap
}

function b(int $x, int $y): int
{
    return $x + $y;     // OK — no parentheses
}

function c($a, $b): bool
{
    return ($a) && ($b); // OK — grouping inside a larger expression
}

Remediation

Remove the outer parentheses: write return $x; and return $a + $b;. Keep parentheses only where they change precedence or form part of a cast or call.