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 |
||
| 19 | class DetailController implements ContainerInjectionInterface { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The mongodb.watchdog_event_controller service. |
||
| 23 | * |
||
| 24 | * @var \Drupal\mongodb_watchdog\EventController |
||
| 25 | */ |
||
| 26 | protected $eventController; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The items_per_page configuration value. |
||
| 30 | * |
||
| 31 | * @var int |
||
| 32 | */ |
||
| 33 | protected $itemsPerPage; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The logger.mongodb_watchdog logger channel, to log events. |
||
| 37 | * |
||
| 38 | * @var \Psr\Log\LoggerInterface |
||
| 39 | */ |
||
| 40 | protected $logger; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The MongoDB logger service, to load events. |
||
| 44 | * |
||
| 45 | * @var \Drupal\mongodb_watchdog\Logger |
||
| 46 | */ |
||
| 47 | protected $watchdog; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Constructor. |
||
| 51 | * |
||
| 52 | * @param \Psr\Log\LoggerInterface $logger |
||
| 53 | * The logger service, to log intervening events. |
||
| 54 | * @param \Drupal\mongodb_watchdog\EventController $event_controller |
||
| 55 | * The event controller service. |
||
| 56 | * @param \Drupal\Core\Config\ImmutableConfig $config |
||
| 57 | * The module configuration. |
||
| 58 | */ |
||
| 59 | public function __construct(Logger $watchdog, LoggerInterface $logger, EventController $event_controller, ImmutableConfig $config) { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | public static function create(ContainerInterface $container) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Controller for mongodb_watchdog.detail. |
||
| 89 | * |
||
| 90 | * @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
||
| 91 | * The event template. |
||
| 92 | * |
||
| 93 | * @return array |
||
| 94 | * A render array. |
||
| 95 | */ |
||
| 96 | public function detail(EventTemplate $event_template, Request $request) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Build the heading rows on the event occurrences page. |
||
| 139 | * |
||
| 140 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 141 | * The event template. |
||
| 142 | * |
||
| 143 | * @return array |
||
| 144 | * A table render array. |
||
| 145 | */ |
||
| 146 | protected function detailHeader(EventTemplate $template) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Build the occurrence rows on the event occurrences page. |
||
| 164 | * |
||
| 165 | * @param \Drupal\mongodb_watchdog\EventTemplate $template |
||
| 166 | * The event template. |
||
| 167 | * @param int $page |
||
| 168 | * The page number, starting at 0. |
||
| 169 | * |
||
| 170 | * @return array |
||
| 171 | * A table render array. |
||
| 172 | */ |
||
| 173 | protected function detailRows(EventTemplate $template, $page) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Title callback for mongodb_watchdog.detail. |
||
| 188 | * |
||
| 189 | * @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
||
| 190 | * The event template for which the title is built. |
||
| 191 | * |
||
| 192 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup |
||
| 193 | * The page title. |
||
| 194 | */ |
||
| 195 | public function detailTitle(EventTemplate $event_template) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Set up the events pager. |
||
| 201 | * |
||
| 202 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 203 | * The current request. |
||
| 204 | * |
||
| 205 | * @return int |
||
| 206 | * The number of the page to display, starting at 0. |
||
| 207 | */ |
||
| 208 | View Code Duplication | public function setupPager(EventTemplate $template, Request $request) { |
|
| 226 | |||
| 227 | } |
||
| 228 |