1 | <?php |
||
8 | class ItemFactory |
||
9 | { |
||
10 | /** |
||
11 | * Allowed values for BundleItem target property. |
||
12 | * |
||
13 | * @var array |
||
14 | */ |
||
15 | const ALLOWEDTARGETS = [ |
||
16 | 'value', |
||
17 | 'key', |
||
18 | 'both', |
||
19 | ]; |
||
20 | |||
21 | /** |
||
22 | * Build a BundleItem instance. |
||
23 | * |
||
24 | * @param string $id |
||
25 | * @param string $type Filename_affected |
||
26 | * @param array $parameters |
||
27 | * |
||
28 | * @return BundleItem |
||
29 | */ |
||
30 | public static function build($id, $type = null, array $parameters = []) |
||
56 | |||
57 | /** |
||
58 | * Validate the target. |
||
59 | * |
||
60 | * @param string $target |
||
61 | * |
||
62 | * @throws InvalidModificationTarget |
||
63 | */ |
||
64 | protected static function validateTarget($target) |
||
70 | } |
||
71 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: