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 | public function __construct( |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Controller. |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | * A render array. |
||
| 114 | */ |
||
| 115 | public function build(Request $request) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Build a table from the event rows. |
||
| 134 | * |
||
| 135 | * @param int $page |
||
| 136 | * The number of the page to display. |
||
| 137 | * |
||
| 138 | * @return array |
||
|
|
|||
| 139 | * A render array. |
||
| 140 | */ |
||
| 141 | public function buildRows($page) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | public static function create(ContainerInterface $container) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Build the link to the event or top report for the event template. |
||
| 211 | * |
||
| 212 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 213 | * The event template for which to buildl the link. |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | * An internal link in string form. |
||
| 217 | */ |
||
| 218 | protected function getEventLink(EventTemplate $template) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Get the location in source code where the event was logged. |
||
| 242 | * |
||
| 243 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 244 | * The template for which to find a source location. |
||
| 245 | * |
||
| 246 | * @return array |
||
| 247 | * A render array for the source location, possibly empty or wrong. |
||
| 248 | */ |
||
| 249 | protected function getEventSource(EventTemplate $template) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Set up the pager. |
||
| 288 | * |
||
| 289 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 290 | * The current request. |
||
| 291 | * |
||
| 292 | * @return int |
||
| 293 | * The number of the page to display, starting at 0. |
||
| 294 | */ |
||
| 295 | View Code Duplication | public function setupPager(Request $request) { |
|
| 313 | |||
| 314 | } |
||
| 315 |
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.