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 $eventController |
||
| 36 | * The event controller service. |
||
| 37 | */ |
||
| 38 | public function __construct( |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Controller. |
||
| 50 | * |
||
| 51 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 52 | * The current request. |
||
| 53 | * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate |
||
| 54 | * The event template. |
||
| 55 | * |
||
| 56 | * @return array<string,array> |
||
|
|
|||
| 57 | * A render array. |
||
| 58 | */ |
||
| 59 | public function build(Request $request, EventTemplate $eventTemplate) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Build the main table. |
||
| 77 | * |
||
| 78 | * @param \Drupal\mongodb_watchdog\Event[] $events |
||
| 79 | * The event data. |
||
| 80 | * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate |
||
| 81 | * The template for which to built the detail lines. |
||
| 82 | * |
||
| 83 | * @return array<string,string|array> |
||
| 84 | * A render array for the main table. |
||
| 85 | */ |
||
| 86 | protected function buildMainTable(array $events, EventTemplate $eventTemplate) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Build the main table header. |
||
| 100 | * |
||
| 101 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup[] |
||
| 102 | * A table header array. |
||
| 103 | */ |
||
| 104 | protected function buildMainTableHeader() { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Build the main table rows. |
||
| 120 | * |
||
| 121 | * @param \Drupal\mongodb_watchdog\Event[] $events |
||
| 122 | * The event row data. |
||
| 123 | * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate |
||
| 124 | * The template for these events. |
||
| 125 | * |
||
| 126 | * @return array<string,array|string> |
||
| 127 | * A render array for a table. |
||
| 128 | */ |
||
| 129 | protected function buildMainTableRows(array $events, EventTemplate $eventTemplate) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Title callback for mongodb_watchdog.detail. |
||
| 142 | * |
||
| 143 | * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate |
||
| 144 | * The event template for which the title is built. |
||
| 145 | * |
||
| 146 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup |
||
| 147 | * The page title. |
||
| 148 | */ |
||
| 149 | public function buildTitle(EventTemplate $eventTemplate) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | View Code Duplication | public static function create(ContainerInterface $container) { |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Obtain the data from the logger. |
||
| 174 | * |
||
| 175 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 176 | * The current request. Needed for paging. |
||
| 177 | * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate |
||
| 178 | * The template for which to build the detail page. |
||
| 179 | * |
||
| 180 | * @return \Drupal\mongodb_watchdog\Event[] |
||
| 181 | * The data array. |
||
| 182 | */ |
||
| 183 | protected function getRowData(Request $request, EventTemplate $eventTemplate) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Build the heading rows on the event occurrences page. |
||
| 198 | * |
||
| 199 | * @param \Drupal\mongodb_watchdog\EventTemplate|null $eventTemplate |
||
| 200 | * The template for which to provide details. Not actually expected to be |
||
| 201 | * NULL, but this is needed to remain compatible with parent class. |
||
| 202 | * |
||
| 203 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup[] |
||
| 204 | * A render array for a table. |
||
| 205 | */ |
||
| 206 | protected function getTop(EventTemplate $eventTemplate = NULL) { |
||
| 227 | |||
| 228 | } |
||
| 229 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.