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 |
||
| 23 | View Code Duplication | class CheckDefinitions extends Command |
|
| 24 | { |
||
| 25 | /** |
||
| 26 | * @see Console\Command\Command |
||
| 27 | */ |
||
| 28 | protected function configure() |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @see Console\Command\Command |
||
| 42 | * |
||
| 43 | * @param InputInterface $input |
||
| 44 | * @param OutputInterface $output |
||
| 45 | * @return void |
||
| 46 | */ |
||
| 47 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 59 | } |
||
| 60 |
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: