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 |
||
| 26 | class Dispatcher implements DispatcherInterface |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * |
||
| 30 | * @var $eventDispatcherInterface |
||
| 31 | */ |
||
| 32 | private $eventDispatcher; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * |
||
| 36 | * @var UseCaseInterface[] |
||
| 37 | */ |
||
| 38 | private $useCases = array(); |
||
| 39 | |||
| 40 | /** @var CommandStack */ |
||
| 41 | private $commandStack; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * |
||
| 45 | * @param EventDispatcherInterface $eventDispatcher |
||
| 46 | * @param CommandStack $commandStack |
||
| 47 | */ |
||
| 48 | public function __construct(EventDispatcherInterface $eventDispatcher, CommandStack $commandStack = null) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * |
||
| 56 | * @param string $commandClass |
||
| 57 | * @param UseCaseInterface $useCase |
||
| 58 | */ |
||
| 59 | public function registerCommand($commandClass, UseCaseInterface $useCase) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * |
||
| 70 | * @param UseCaseCollectionInterface $useCase |
||
| 71 | */ |
||
| 72 | public function registerUseCaseCollection(UseCaseCollectionInterface $useCase) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * @param CommandInterface $command |
||
| 82 | * @return ResponseInterface |
||
| 83 | * @throws \Exception |
||
| 84 | */ |
||
| 85 | public function execute(CommandInterface $command) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * |
||
| 106 | * @param CommandInterface $command |
||
| 107 | * @return ResponseInterface |
||
| 108 | */ |
||
| 109 | protected function doExecute(CommandInterface $command) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * |
||
| 123 | * @param CommandInterface $command |
||
| 124 | * @return CommandInterface |
||
| 125 | */ |
||
| 126 | protected function preCommand(CommandInterface $command) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * |
||
| 136 | * @param CommandInterface $command |
||
| 137 | * @param ResponseInterface $response |
||
| 138 | * @return ResponseInterface |
||
| 139 | */ |
||
| 140 | View Code Duplication | protected function postCommand(CommandInterface $command, ResponseInterface $response) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * |
||
| 150 | * @param CommandInterface $command |
||
| 151 | * @param \Exception $exception |
||
| 152 | * @return ResponseInterface |
||
| 153 | */ |
||
| 154 | protected function exception(CommandInterface $command, \Exception $exception) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * |
||
| 168 | * @param CommandInterface $command |
||
| 169 | * @param ResponseInterface $response |
||
| 170 | * @return ResponseInterface |
||
| 171 | */ |
||
| 172 | View Code Duplication | protected function terminate(CommandInterface $command, ResponseInterface $response) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * |
||
| 184 | * @param CommandInterface $command |
||
| 185 | * @return UseCaseInterface |
||
| 186 | * @throws Exception\UseCaseNotFoundException |
||
| 187 | */ |
||
| 188 | protected function resolveUseCase(CommandInterface $command) |
||
| 198 | } |
||
| 199 |