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 |
||
| 9 | class MessageHandler implements MessageHandlerInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var LoggerInterface |
||
| 13 | */ |
||
| 14 | protected $logger; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var Session |
||
| 18 | */ |
||
| 19 | protected $session; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var TranslatorInterface |
||
| 23 | */ |
||
| 24 | protected $translator; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * ErrorHandler constructor |
||
| 28 | * |
||
| 29 | * @param LoggerInterface $logger |
||
| 30 | * @param Session $session |
||
| 31 | * @param TranslatorInterface $translator |
||
| 32 | */ |
||
| 33 | 2 | public function __construct(LoggerInterface $logger, Session $session, TranslatorInterface $translator) |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Create an flash message into the session. It will be translated. If $logMessage is defined, it will be logged |
||
| 42 | * using the logger using the error channel |
||
| 43 | * |
||
| 44 | * @param string $flashMessage |
||
| 45 | * @param string|null $logMessage |
||
| 46 | */ |
||
| 47 | 1 | View Code Duplication | public function handleError($flashMessage, $logMessage = null) |
| 60 | |||
| 61 | /** |
||
| 62 | * Create an flash message into the session. It will be translated. If $logMessage is defined, it will be logged |
||
| 63 | * using the logger using info channel |
||
| 64 | * |
||
| 65 | * @param $flashMessage |
||
| 66 | * @param null $logMessage |
||
| 67 | */ |
||
| 68 | 1 | View Code Duplication | public function handleSuccess($flashMessage, $logMessage = null) |
| 81 | } |
||
| 82 |
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.