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 |
||
| 25 | class OverviewController extends ControllerBase { |
||
| 26 | const EVENT_TYPE_MAP = [ |
||
| 27 | 'typeMap' => [ |
||
| 28 | 'array' => 'array', |
||
| 29 | 'document' => 'array', |
||
| 30 | 'root' => 'Drupal\mongodb_watchdog\Event', |
||
| 31 | ], |
||
| 32 | ]; |
||
| 33 | const SEVERITY_PREFIX = 'mongodb_watchdog__severity_'; |
||
| 34 | const SEVERITY_CLASSES = [ |
||
| 35 | RfcLogLevel::DEBUG => self::SEVERITY_PREFIX . LogLevel::DEBUG, |
||
| 36 | RfcLogLevel::INFO => self::SEVERITY_PREFIX . LogLevel::INFO, |
||
| 37 | RfcLogLevel::NOTICE => self::SEVERITY_PREFIX . LogLevel::NOTICE, |
||
| 38 | RfcLogLevel::WARNING => self::SEVERITY_PREFIX . LogLevel::WARNING, |
||
| 39 | RfcLogLevel::ERROR => self::SEVERITY_PREFIX . LogLevel::ERROR, |
||
| 40 | RfcLogLevel::CRITICAL => self::SEVERITY_PREFIX . LogLevel::CRITICAL, |
||
| 41 | RfcLogLevel::ALERT => self::SEVERITY_PREFIX . LogLevel::ALERT, |
||
| 42 | RfcLogLevel::EMERGENCY => self::SEVERITY_PREFIX . LogLevel::EMERGENCY, |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The core date.formatter service. |
||
| 47 | * |
||
| 48 | * @var \Drupal\Core\Datetime\DateFormatterInterface |
||
| 49 | */ |
||
| 50 | protected $dateFormatter; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The form builder service. |
||
| 54 | * |
||
| 55 | * @var \Drupal\Core\Form\FormBuilderInterface |
||
| 56 | */ |
||
| 57 | protected $formBuilder; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The module handler service. |
||
| 61 | * |
||
| 62 | * @var \Drupal\Core\Extension\ModuleHandlerInterface |
||
| 63 | */ |
||
| 64 | protected $moduleHandler; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The length of the disk path to DRUPAL_ROOT. |
||
| 68 | * |
||
| 69 | * @var int |
||
| 70 | * |
||
| 71 | * @see \Drupal\mongodb_watchdog\Controller\OverviewController::getEventSource() |
||
| 72 | */ |
||
| 73 | protected $rootLength; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Controller constructor. |
||
| 77 | * |
||
| 78 | * @param \Psr\Log\LoggerInterface $logger |
||
| 79 | * The logger service, to log intervening events. |
||
| 80 | * @param \Drupal\mongodb_watchdog\Logger $watchdog |
||
| 81 | * The MongoDB logger, to load stored events. |
||
| 82 | * @param \Drupal\Core\Config\ImmutableConfig $config |
||
| 83 | * The module configuration. |
||
| 84 | * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler |
||
| 85 | * A module handler. |
||
| 86 | * @param \Drupal\Core\Form\FormBuilderInterface $form_builder |
||
| 87 | * The form builder service. |
||
| 88 | * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
||
| 89 | * The core date_formatter service. |
||
| 90 | */ |
||
| 91 | View Code Duplication | public function __construct( |
|
|
|
|||
| 92 | LoggerInterface $logger, |
||
| 93 | Logger $watchdog, |
||
| 94 | ImmutableConfig $config, |
||
| 95 | ModuleHandlerInterface $module_handler, |
||
| 96 | FormBuilderInterface $form_builder, |
||
| 97 | DateFormatterInterface $date_formatter) { |
||
| 98 | parent::__construct($logger, $watchdog, $config); |
||
| 99 | |||
| 100 | $this->dateFormatter = $date_formatter; |
||
| 101 | $this->formBuilder = $form_builder; |
||
| 102 | $this->moduleHandler = $module_handler; |
||
| 103 | |||
| 104 | // Add terminal "/". |
||
| 105 | $this->rootLength = Unicode::strlen(DRUPAL_ROOT); |
||
| 106 | |||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Controller. |
||
| 111 | * |
||
| 112 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 113 | * The current request. |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | * A render array. |
||
| 117 | */ |
||
| 118 | View Code Duplication | public function build(Request $request) { |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Build the main table. |
||
| 132 | * |
||
| 133 | * @param \Drupal\mongodb_watchdog\EventTemplate[] $rows |
||
| 134 | * The template data. |
||
| 135 | * |
||
| 136 | * @return array<string,string|array> |
||
| 137 | * A render array for the main table. |
||
| 138 | */ |
||
| 139 | View Code Duplication | protected function buildMainTable(array $rows) { |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Build the main table header. |
||
| 151 | * |
||
| 152 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup[] |
||
| 153 | * A table header array. |
||
| 154 | */ |
||
| 155 | protected function buildMainTableHeader() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Build the main table rows. |
||
| 170 | * |
||
| 171 | * @param \Drupal\mongodb_watchdog\EventTemplate[] $templates |
||
| 172 | * The event template data. |
||
| 173 | * |
||
| 174 | * @return array<string,array|string> |
||
| 175 | * A render array for a table. |
||
| 176 | */ |
||
| 177 | protected function buildMainTableRows(array $templates) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritdoc} |
||
| 203 | */ |
||
| 204 | public static function create(ContainerInterface $container) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Build the link to the event or top report for the event template. |
||
| 228 | * |
||
| 229 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 230 | * The event template for which to buildl the link. |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | * An internal link in string form. |
||
| 234 | */ |
||
| 235 | protected function getEventLink(EventTemplate $template) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Get the location in source code where the event was logged. |
||
| 259 | * |
||
| 260 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 261 | * The template for which to find a source location. |
||
| 262 | * |
||
| 263 | * @return array |
||
| 264 | * A render array for the source location, possibly empty or wrong. |
||
| 265 | */ |
||
| 266 | protected function getEventSource(EventTemplate $template) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Obtain the data from the logger. |
||
| 307 | * |
||
| 308 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 309 | * The current request. Needed for paging. |
||
| 310 | * |
||
| 311 | * @return \Drupal\mongodb_watchdog\EventTemplate[] |
||
| 312 | * The data array. |
||
| 313 | */ |
||
| 314 | protected function getRowData(Request $request) { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Return the top element. |
||
| 331 | * |
||
| 332 | * @return array |
||
| 333 | * A render array for the top filter form. |
||
| 334 | */ |
||
| 335 | protected function getTop() { |
||
| 339 | |||
| 340 | } |
||
| 341 |
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.