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 items_per_page configuration value. |
||
| 61 | * |
||
| 62 | * @var int |
||
| 63 | */ |
||
| 64 | protected $itemsPerPage; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The core logger channel, to log intervening events. |
||
| 68 | * |
||
| 69 | * @var \Psr\Log\LoggerInterface |
||
| 70 | */ |
||
| 71 | protected $logger; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The module handler service. |
||
| 75 | * |
||
| 76 | * @var \Drupal\Core\Extension\ModuleHandlerInterface |
||
| 77 | */ |
||
| 78 | protected $moduleHandler; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The length of the disk path to DRUPAL_ROOT. |
||
| 82 | * |
||
| 83 | * @var int |
||
| 84 | * |
||
| 85 | * @see \Drupal\mongodb_watchdog\Controller\OverviewController::getEventSource() |
||
| 86 | */ |
||
| 87 | protected $rootLength; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The MongoDB logger, to load events. |
||
| 91 | * |
||
| 92 | * @var \Drupal\mongodb_watchdog\Logger |
||
| 93 | */ |
||
| 94 | protected $watchdog; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Constructor. |
||
| 98 | * |
||
| 99 | * @param \Psr\Log\LoggerInterface $logger |
||
| 100 | * The logger service, to log intervening events. |
||
| 101 | * @param \Drupal\mongodb_watchdog\Logger $watchdog |
||
| 102 | * The MongoDB logger, to load stored events. |
||
| 103 | * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler |
||
| 104 | * A module handler. |
||
| 105 | * @param \Drupal\Core\Form\FormBuilderInterface $form_builder |
||
| 106 | * The form builder service. |
||
| 107 | * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
||
| 108 | * The core date_formatter service. |
||
| 109 | * @param int $items_per_page |
||
| 110 | * The items_per_page configuration value. |
||
| 111 | */ |
||
| 112 | public function __construct( |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public static function create(ContainerInterface $container) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Build the link to the event or top report for the event template. |
||
| 160 | * |
||
| 161 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 162 | * The event template for which to buildl the link. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | * An internal link in string form. |
||
| 166 | */ |
||
| 167 | protected function getEventLink(EventTemplate $template) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get the location in source code where the event was logged. |
||
| 191 | * |
||
| 192 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 193 | * The template for which to find a source location. |
||
| 194 | * |
||
| 195 | * @return array |
||
|
|
|||
| 196 | * A render array for the source location, possibly empty or wrong. |
||
| 197 | */ |
||
| 198 | protected function getEventSource(EventTemplate $template) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Controller for mongodb_watchdog.overview. |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | * A render array. |
||
| 240 | */ |
||
| 241 | public function overview(Request $request) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Build a table from the event rows. |
||
| 260 | * |
||
| 261 | * @param int $page |
||
| 262 | * The number of the page to display. |
||
| 263 | * |
||
| 264 | * @return array |
||
| 265 | * A render array. |
||
| 266 | */ |
||
| 267 | public function overviewRows($page) { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Set up the templates pager. |
||
| 310 | * |
||
| 311 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 312 | * The current request. |
||
| 313 | * |
||
| 314 | * @return int |
||
| 315 | * The number of the page to display, starting at 0. |
||
| 316 | */ |
||
| 317 | View Code Duplication | public function setupPager(Request $request) { |
|
| 335 | |||
| 336 | } |
||
| 337 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.