1.2.1399. Useless Type Casting

There is no need to cast already typed values.

Typed values, such as properties, do not have to be cast again, as the engine always ensures their type.

Typed arguments are variables : after the initial check at method call time, they might change value and type. Those extra cast may then carry some value, although changing the type of an incoming value is not recommended.

<?php

// trim always returns a string : cast is useless
$a = (string) trim($b);

// strpos doesn't always returns an integer : cast is useful
$a = (boolean) strpos($b, $c);

// comparison don't need casting, nor parenthesis
$c = (bool) ($b > 2);

function foo(array $a) {
    foreach((array) $a as $b) {
        // doSomething
    }
}
?>

See also Type juggling and Multiple Type Variable.

1.2.1399.1. Suggestions

  • Remove the type cast

1.2.1399.2. Specs

Short name

Structures/UselessCasting

Rulesets

All, Analyze, CE, CI-checks, PHP recommendations

Exakat since

0.8.7

PHP Version

All

Severity

Minor

Time To Fix

Instant (5 mins)

Precision

Very high

Features

type, cast

Examples

FuelCMS, ThinkPHP

Available in

Entreprise Edition, Community Edition, Exakat Cloud