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 |
||
11 | class Hydrate extends ConfigureActionCommand |
||
12 | { |
||
13 | 36 | public function __construct(Application $app) |
|
22 | |||
23 | 10 | protected function getProcessor(): ConfigurableProcessor |
|
27 | |||
28 | 10 | protected function launchConfigurationAction(ConfigurableProcessor $processor): void |
|
34 | |||
35 | 10 | View Code Duplication | private function warnForUnusedVariables(Hydrator $processor) |
51 | |||
52 | 10 | View Code Duplication | private function warnForUnvaluedVariables(Hydrator $processor): void |
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: