1.2.1224. 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.1224.1. Suggestions

  • Avoid using union type to enlarge types in parameters

1.2.1224.2. Specs

Short name

Functions/TypeDodging

Rulesets

All, Changed Behavior, Class Review

Exakat since

2.5.0

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Medium

Features

liskov

Available in

Entreprise Edition, Exakat Cloud