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 |
||
| 18 | class DetailController extends ControllerBase { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The mongodb.watchdog_event_controller service. |
||
| 22 | * |
||
| 23 | * @var \Drupal\mongodb_watchdog\EventController |
||
| 24 | */ |
||
| 25 | protected $eventController; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Controller constructor. |
||
| 29 | * |
||
| 30 | * @param \Psr\Log\LoggerInterface $logger |
||
| 31 | * The logger service, to log intervening events. |
||
| 32 | * @param \Drupal\mongodb_watchdog\Logger $watchdog |
||
| 33 | * The MongoDB logger, to load stored events. |
||
| 34 | * @param \Drupal\Core\Config\ImmutableConfig $config |
||
| 35 | * The module configuration. |
||
| 36 | * @param \Drupal\mongodb_watchdog\EventController $event_controller |
||
| 37 | * The event controller service. |
||
| 38 | */ |
||
| 39 | public function __construct( |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Controller. |
||
| 51 | * |
||
| 52 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 53 | * The current request. |
||
| 54 | * @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
||
| 55 | * The event template. |
||
| 56 | * |
||
| 57 | * @return array A render array. |
||
|
|
|||
| 58 | * A render array. |
||
| 59 | */ |
||
| 60 | public function build(Request $request, EventTemplate $event_template) { |
||
| 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 | * Set up the pager. |
||
| 184 | * |
||
| 185 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 186 | * The current request. |
||
| 187 | * |
||
| 188 | * @return int |
||
| 189 | * The number of the page to display, starting at 0. |
||
| 190 | */ |
||
| 191 | View Code Duplication | public function setupPager(Request $request, EventTemplate $template) { |
|
| 209 | |||
| 210 | } |
||
| 211 |
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.