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 |
||
| 16 | class Dispatcher implements DispatcherInterface |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Dispatcher driver |
||
| 20 | * |
||
| 21 | * @var DriverInterface |
||
| 22 | */ |
||
| 23 | private $driver; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Logger |
||
| 27 | * |
||
| 28 | * @var LoggerInterface |
||
| 29 | */ |
||
| 30 | private $logger; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Restart |
||
| 34 | * |
||
| 35 | * @var RestartInterface |
||
| 36 | */ |
||
| 37 | private $restart; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * All registered handlers |
||
| 41 | * |
||
| 42 | * @var HandlerInterface[][] |
||
| 43 | */ |
||
| 44 | private $handlers = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var DateTime |
||
| 48 | */ |
||
| 49 | private $startTime; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Create new Dispatcher |
||
| 53 | * |
||
| 54 | * @param DriverInterface $driver |
||
| 55 | * @param LoggerInterface $logger |
||
| 56 | * @param RestartInterface $restart |
||
| 57 | 18 | */ |
|
| 58 | public function __construct(DriverInterface $driver, LoggerInterface $logger = null, RestartInterface $restart = null) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @deprecated - use Emitter::emit method intead |
||
| 68 | */ |
||
| 69 | View Code Duplication | public function emit(MessageInterface $message): DispatcherInterface |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Basic method for background job to star listening. |
||
| 83 | * |
||
| 84 | * This method hook to driver wait() method and start listening events. |
||
| 85 | * Method is blocking, so when you call it all processing will stop. |
||
| 86 | * WARNING! Don't use it on web server calls. Run it only with cli. |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | public function handle(): void |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Dispatch message |
||
| 119 | * |
||
| 120 | * @param MessageInterface $message |
||
| 121 | 18 | * |
|
| 122 | * @return bool |
||
| 123 | 18 | */ |
|
| 124 | private function dispatch(MessageInterface $message): bool |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Handle given message with given handler |
||
| 147 | * |
||
| 148 | * @param HandlerInterface $handler |
||
| 149 | * @param MessageInterface $message |
||
| 150 | 15 | * |
|
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | 15 | private function handleMessage(HandlerInterface $handler, MessageInterface $message): bool |
|
| 190 | |||
| 191 | /** |
||
| 192 | 18 | * Calculate next retry |
|
| 193 | * |
||
| 194 | 18 | * Inspired by ruby sidekiq (https://github.com/mperham/sidekiq/wiki/Error-Handling#automatic-job-retry) |
|
| 195 | 18 | * |
|
| 196 | 12 | * @param MessageInterface $message |
|
| 197 | * @return float |
||
| 198 | 18 | */ |
|
| 199 | 18 | private function nextRetry(MessageInterface $message): float |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Check if actual dispatcher has handler for given type |
||
| 206 | * |
||
| 207 | * @param string $type |
||
| 208 | 18 | * |
|
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | 18 | private function hasHandlers(string $type): bool |
|
| 215 | 12 | ||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | */ |
||
| 219 | public function registerHandler(string $type, HandlerInterface $handler): DispatcherInterface |
||
| 228 | |||
| 229 | 18 | /** |
|
| 230 | 3 | * Serialize message to logger context |
|
| 231 | 2 | * |
|
| 232 | 18 | * @param MessageInterface $message |
|
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | View Code Duplication | private function messageLoggerContext(MessageInterface $message): array |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Interal log method wrapper |
||
| 248 | * |
||
| 249 | * @param mixed $level |
||
| 250 | * @param string $message |
||
| 251 | * @param array $context |
||
| 252 | * |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | private function log($level, string $message, array $context = array()): void |
||
| 261 | } |
||
| 262 |
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.