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 implements ContainerInjectionInterface { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The mongodb.watchdog_event_controller service. |
||
| 22 | * |
||
| 23 | * @var \Drupal\mongodb_watchdog\EventController |
||
| 24 | */ |
||
| 25 | protected $eventController; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The logger.mongodb_watchdog logger channel, to log events. |
||
| 29 | * |
||
| 30 | * @var \Psr\Log\LoggerInterface |
||
| 31 | */ |
||
| 32 | protected $logger; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The MongoDB logger, to load events. |
||
| 36 | * |
||
| 37 | * @var \Drupal\mongodb_watchdog\Logger |
||
| 38 | */ |
||
| 39 | protected $watchdog; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Controller constructor. |
||
| 43 | * |
||
| 44 | * @param \Psr\Log\LoggerInterface $logger |
||
| 45 | * The logger service, to log intervening events. |
||
| 46 | * @param \Drupal\mongodb_watchdog\EventController $event_controller |
||
| 47 | * The event controller service. |
||
| 48 | * @param \Drupal\Core\Config\ImmutableConfig $config |
||
| 49 | * The module configuration. |
||
| 50 | */ |
||
| 51 | public function __construct( |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Controller. |
||
| 65 | * |
||
| 66 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 67 | * The current request. |
||
| 68 | * @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
||
| 69 | * The event template. |
||
| 70 | * |
||
| 71 | * @return array A render array. |
||
|
|
|||
| 72 | * A render array. |
||
| 73 | */ |
||
| 74 | public function build(Request $request, EventTemplate $event_template) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Build the heading rows on the event occurrences page. |
||
| 117 | * |
||
| 118 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 119 | * The event template. |
||
| 120 | * |
||
| 121 | * @return array |
||
| 122 | * A table render array. |
||
| 123 | */ |
||
| 124 | protected function buildHeader(EventTemplate $template) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Build the occurrence rows on the event occurrences page. |
||
| 142 | * |
||
| 143 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 144 | * The event template. |
||
| 145 | * @param int $page |
||
| 146 | * The page number, starting at 0. |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | * A table render array. |
||
| 150 | */ |
||
| 151 | protected function buildRows(EventTemplate $template, $page) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Title callback for mongodb_watchdog.detail. |
||
| 166 | * |
||
| 167 | * @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
||
| 168 | * The event template for which the title is built. |
||
| 169 | * |
||
| 170 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup |
||
| 171 | * The page title. |
||
| 172 | */ |
||
| 173 | public function buildTitle(EventTemplate $event_template) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | */ |
||
| 180 | public static function create(ContainerInterface $container) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Set up the events pager. |
||
| 198 | * |
||
| 199 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 200 | * The current request. |
||
| 201 | * |
||
| 202 | * @return int |
||
| 203 | * The number of the page to display, starting at 0. |
||
| 204 | */ |
||
| 205 | View Code Duplication | public function setupPager(EventTemplate $template, Request $request) { |
|
| 223 | |||
| 224 | } |
||
| 225 |
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.