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 |
||
| 17 | class DetailController extends ControllerBase { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The mongodb.watchdog_event_controller service. |
||
| 21 | * |
||
| 22 | * @var \Drupal\mongodb_watchdog\EventController |
||
| 23 | */ |
||
| 24 | protected $eventController; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Controller constructor. |
||
| 28 | * |
||
| 29 | * @param \Psr\Log\LoggerInterface $logger |
||
| 30 | * The logger service, to log intervening events. |
||
| 31 | * @param \Drupal\mongodb_watchdog\Logger $watchdog |
||
| 32 | * The MongoDB logger, to load stored events. |
||
| 33 | * @param \Drupal\Core\Config\ImmutableConfig $config |
||
| 34 | * The module configuration. |
||
| 35 | * @param \Drupal\mongodb_watchdog\EventController $event_controller |
||
| 36 | * The event controller service. |
||
| 37 | */ |
||
| 38 | public function __construct( |
||
| 39 | LoggerInterface $logger, |
||
| 40 | Logger $watchdog, |
||
| 41 | ImmutableConfig $config, |
||
| 42 | EventController $event_controller) { |
||
| 43 | parent::__construct($logger, $watchdog, $config); |
||
| 44 | |||
| 45 | $this->eventController = $event_controller; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Controller. |
||
| 50 | * |
||
| 51 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 52 | * The current request. |
||
| 53 | * @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
||
| 54 | * The event template. |
||
| 55 | * |
||
| 56 | * @return array A render array. |
||
|
|
|||
| 57 | * A render array. |
||
| 58 | */ |
||
| 59 | public function build(Request $request, EventTemplate $event_template) { |
||
| 60 | $count = $this->watchdog->eventCount($event_template); |
||
| 61 | $page = $this->setupPager($request, $count); |
||
| 62 | $template_rows = $this->buildHeader($event_template); |
||
| 63 | $event_rows = $this->buildRows($event_template, $page); |
||
| 64 | |||
| 65 | $base = [ |
||
| 66 | '#attributes' => new Attribute(['class' => 'mongodb_watchdog-detail']), |
||
| 67 | '#type' => 'table', |
||
| 68 | ]; |
||
| 69 | |||
| 70 | $event_header = [ |
||
| 71 | t('Date'), |
||
| 72 | t('User'), |
||
| 73 | t('Message'), |
||
| 74 | t('Location'), |
||
| 75 | t('Referrer'), |
||
| 76 | t('Hostname'), |
||
| 77 | t('Operations'), |
||
| 78 | ]; |
||
| 79 | |||
| 80 | $ret = [ |
||
| 81 | 'template' => $base + [ |
||
| 82 | '#caption' => t('Event template'), |
||
| 83 | '#rows' => $template_rows, |
||
| 84 | ], |
||
| 85 | 'events' => $base + [ |
||
| 86 | '#caption' => t('Event occurrences'), |
||
| 87 | '#header' => $event_header, |
||
| 88 | '#rows' => $event_rows, |
||
| 89 | ], |
||
| 90 | 'pager' => [ |
||
| 91 | '#type' => 'pager', |
||
| 92 | ], |
||
| 93 | '#attached' => [ |
||
| 94 | 'library' => ['mongodb_watchdog/styling'], |
||
| 95 | ], |
||
| 96 | ]; |
||
| 97 | |||
| 98 | return $ret; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Build the heading rows on the event occurrences page. |
||
| 103 | * |
||
| 104 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 105 | * The event template. |
||
| 106 | * |
||
| 107 | * @return array |
||
| 108 | * A table render array. |
||
| 109 | */ |
||
| 110 | protected function buildHeader(EventTemplate $template) { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Build the occurrence rows on the event occurrences page. |
||
| 128 | * |
||
| 129 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 130 | * The event template. |
||
| 131 | * @param int $page |
||
| 132 | * The page number, starting at 0. |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | * A table render array. |
||
| 136 | */ |
||
| 137 | protected function buildRows(EventTemplate $template, $page) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Title callback for mongodb_watchdog.detail. |
||
| 152 | * |
||
| 153 | * @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
||
| 154 | * The event template for which the title is built. |
||
| 155 | * |
||
| 156 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup |
||
| 157 | * The page title. |
||
| 158 | */ |
||
| 159 | public function buildTitle(EventTemplate $event_template) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | View Code Duplication | public static function create(ContainerInterface $container) { |
|
| 181 | |||
| 182 | } |
||
| 183 |
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.