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 |
||
7 | View Code Duplication | class LaravelLazyLocator implements LocatorInterface |
|
|
|||
8 | { |
||
9 | |||
10 | /** |
||
11 | * The handlers |
||
12 | * @var |
||
13 | */ |
||
14 | protected $handlers; |
||
15 | |||
16 | /** |
||
17 | * Bind a handler instance to receive all commands with a certain class |
||
18 | * |
||
19 | * @param string $handler Handler to receive class name |
||
20 | * @param string $commandClassName Command class e.g. "My\TaskAddedCommand" |
||
21 | */ |
||
22 | public function addHandler($handler, $commandClassName) |
||
28 | |||
29 | /** |
||
30 | * Allows you to add multiple handlers at once. |
||
31 | * |
||
32 | * The map should be an array in the format of: |
||
33 | * [ |
||
34 | * AddTaskCommand::class => $someHandlerClassName, |
||
35 | * CompleteTaskCommand::class => $someHandlerClassName, |
||
36 | * ] |
||
37 | * |
||
38 | * @param array $commandClassToHandlerMap |
||
39 | */ |
||
40 | public function addHandlers(array $commandClassToHandlerMap) |
||
46 | |||
47 | /** |
||
48 | * Retrieves the handler for a specified command |
||
49 | * |
||
50 | * @param string $commandName |
||
51 | * |
||
52 | * @return object |
||
53 | * |
||
54 | * @throws MissingHandlerException |
||
55 | */ |
||
56 | public function getHandlerForCommand($commandName) |
||
64 | } |
||
65 |
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.