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 core logger channel, to log intervening events. |
||
| 61 | * |
||
| 62 | * @var \Psr\Log\LoggerInterface |
||
| 63 | */ |
||
| 64 | protected $logger; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The module handler service. |
||
| 68 | * |
||
| 69 | * @var \Drupal\Core\Extension\ModuleHandlerInterface |
||
| 70 | */ |
||
| 71 | protected $moduleHandler; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The length of the disk path to DRUPAL_ROOT. |
||
| 75 | * |
||
| 76 | * @var int |
||
| 77 | * |
||
| 78 | * @see \Drupal\mongodb_watchdog\Controller\OverviewController::getEventSource() |
||
| 79 | */ |
||
| 80 | protected $rootLength; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The MongoDB logger, to load events. |
||
| 84 | * |
||
| 85 | * @var \Drupal\mongodb_watchdog\Logger |
||
| 86 | */ |
||
| 87 | protected $watchdog; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Controller constructor. |
||
| 91 | * |
||
| 92 | * @param \Psr\Log\LoggerInterface $logger |
||
| 93 | * The logger service, to log intervening events. |
||
| 94 | * @param \Drupal\mongodb_watchdog\Logger $watchdog |
||
| 95 | * The MongoDB logger, to load stored events. |
||
| 96 | * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler |
||
| 97 | * A module handler. |
||
| 98 | * @param \Drupal\Core\Form\FormBuilderInterface $form_builder |
||
| 99 | * The form builder service. |
||
| 100 | * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
||
| 101 | * The core date_formatter service. |
||
| 102 | * @param \Drupal\Core\Config\ImmutableConfig $config |
||
| 103 | * The module configuration. |
||
| 104 | */ |
||
| 105 | public function __construct( |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Controller. |
||
| 126 | * |
||
| 127 | * @return array |
||
| 128 | * A render array. |
||
| 129 | */ |
||
| 130 | public function build(Request $request) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Build a table from the event rows. |
||
| 149 | * |
||
| 150 | * @param int $page |
||
| 151 | * The number of the page to display. |
||
| 152 | * |
||
| 153 | * @return array |
||
|
|
|||
| 154 | * A render array. |
||
| 155 | */ |
||
| 156 | public function buildRows($page) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | public static function create(ContainerInterface $container) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Build the link to the event or top report for the event template. |
||
| 224 | * |
||
| 225 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 226 | * The event template for which to buildl the link. |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | * An internal link in string form. |
||
| 230 | */ |
||
| 231 | protected function getEventLink(EventTemplate $template) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the location in source code where the event was logged. |
||
| 255 | * |
||
| 256 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 257 | * The template for which to find a source location. |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | * A render array for the source location, possibly empty or wrong. |
||
| 261 | */ |
||
| 262 | protected function getEventSource(EventTemplate $template) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Set up the templates pager. |
||
| 301 | * |
||
| 302 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 303 | * The current request. |
||
| 304 | * |
||
| 305 | * @return int |
||
| 306 | * The number of the page to display, starting at 0. |
||
| 307 | */ |
||
| 308 | View Code Duplication | public function setupPager(Request $request) { |
|
| 326 | |||
| 327 | } |
||
| 328 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.