Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
17 | class OutputSign extends PluginAbstract |
||
18 | { |
||
19 | |||
20 | public static $hook = array('response', 'early'); |
||
21 | |||
22 | protected $options = array( |
||
23 | 'enable' => true, // wether to enable or not |
||
24 | 'name' => 'signature', // the header name |
||
25 | 'prepend' => false, // wether to prepend the signature |
||
26 | 'extras' => null, // extras to inject, string or array |
||
27 | ); |
||
28 | |||
29 | public function update(\SplSubject $response) |
||
65 | |||
66 | } |
||
67 |
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 implementation 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 interface: