1.2.1234. Type Dodging¶
It is always possible to rewrite a parameter type by using union types. When the parent class or interface requires a type, the child class may create a union type with the required type, add a secondary type and ignore the first one.
This is part of the Liskov Substitution Principle, so the syntax is legit. When the union type is only used to circumvent the previous typing, it is now a violation, as such a typed data would be valid, but ignored.
<?php
interface i {
function foo(A $a) {}
}
class x implement i {
function foo(A | string $a) {
if ($a instanceof A) {
throw new Exception('Unused type.');
}
// ...
}
}
?>
1.2.1234.1. Connex PHP features¶
1.2.1234.1.1. Suggestions¶
Avoid using union type to enlarge types in parameters
1.2.1234.1.2. Specs¶
Short name |
Functions/TypeDodging |
Rulesets |
|
Exakat since |
2.5.0 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Medium |
Available in |