1.2.1410. Useless Type Casting¶
There is no need to cast values that are already well typed.
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.1410.1. Connex PHP features¶
1.2.1410.1.1. Suggestions¶
Remove the type cast
1.2.1410.1.2. Specs¶
Short name |
Structures/UselessCasting |
Rulesets |
All, Analyze, CE, CI-checks, Changed Behavior, PHP recommendations |
Exakat since |
0.8.7 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Instant (5 mins) |
Precision |
Very high |
Examples |
|
Available in |