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 |
||
13 | abstract class AbstractCheckCommandFactory implements FactoryInterface |
||
14 | { |
||
15 | /** |
||
16 | * Create service |
||
17 | * |
||
18 | * @param ServiceLocatorInterface $serviceLocator |
||
19 | * @return CheckCommand |
||
20 | * @throws \UnexpectedValueException |
||
21 | * @throws \Zend\Console\Exception\RuntimeException |
||
22 | * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
||
23 | */ |
||
24 | 10 | public function createService(ServiceLocatorInterface $serviceLocator) |
|
25 | 1 | { |
|
26 | 10 | $cli = $serviceLocator->get('doctrine.cli'); |
|
27 | 10 | View Code Duplication | if (!$cli instanceof Application) { |
|
|||
28 | 1 | throw new \UnexpectedValueException(sprintf( |
|
29 | 1 | 'Expected type [%s]. Actual type [%s].', |
|
30 | 1 | Application::class, |
|
31 | 1 | is_object($cli) ? get_class($cli) : gettype($cli) |
|
32 | 1 | )); |
|
33 | } |
||
34 | |||
35 | 9 | $command = $serviceLocator->get($this->getCommandServiceName()); |
|
36 | 9 | View Code Duplication | if (!$command instanceof Command) { |
37 | 1 | throw new \UnexpectedValueException(sprintf( |
|
38 | 1 | 'Expected type [%s]. Actual type [%s].', |
|
39 | 1 | Command::class, |
|
40 | 1 | is_object($command) ? get_class($command) : gettype($command) |
|
41 | 1 | )); |
|
42 | } |
||
43 | |||
44 | 8 | $input = new RequestInput(new Request(array())); |
|
45 | |||
46 | 8 | $output = new PropertyOutput(); |
|
47 | |||
48 | 8 | return new CheckCommand($command, $input, $output); |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * @return string |
||
53 | */ |
||
54 | abstract public function getCommandServiceName(); |
||
55 | } |
||
56 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.