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 |
||
| 28 | final class ProcedureAwareLogger implements LoggerInterface |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var string|null |
||
| 32 | */ |
||
| 33 | private $procedure; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var LoggerInterface |
||
| 37 | */ |
||
| 38 | private $logger; |
||
| 39 | |||
| 40 | public function __construct(LoggerInterface $logger) |
||
| 44 | |||
| 45 | View Code Duplication | public function forProcedure($procedure) |
|
| 56 | |||
| 57 | public function emergency($message, array $context = []) |
||
| 61 | |||
| 62 | public function alert($message, array $context = []) |
||
| 66 | |||
| 67 | public function critical($message, array $context = []) |
||
| 71 | |||
| 72 | public function error($message, array $context = []) |
||
| 76 | |||
| 77 | public function warning($message, array $context = []) |
||
| 81 | |||
| 82 | public function notice($message, array $context = []) |
||
| 86 | |||
| 87 | public function info($message, array $context = []) |
||
| 91 | |||
| 92 | public function debug($message, array $context = []) |
||
| 96 | |||
| 97 | public function log($level, $message, array $context = []) |
||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Adds the procedure to the log context. |
||
| 105 | * |
||
| 106 | * @param array $context |
||
| 107 | * @return array |
||
| 108 | * @throws RuntimeException |
||
| 109 | */ |
||
| 110 | private function enrichContext(array $context) |
||
| 120 | } |
||
| 121 |
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.