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,string|array> |
||
| 57 | * A render array. |
||
| 58 | */ |
||
| 59 | public function build(Request $request, EventTemplate $eventTemplate) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Build the main table. |
||
| 73 | * |
||
| 74 | * @param \Drupal\mongodb_watchdog\Event[] $events |
||
| 75 | * The event data. |
||
| 76 | * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate |
||
| 77 | * The template for which to built the detail lines. |
||
| 78 | * |
||
| 79 | * @return array<string,string|array> |
||
| 80 | * A render array for the main table. |
||
| 81 | */ |
||
| 82 | protected function buildMainTable(array $events, EventTemplate $eventTemplate) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Build the main table header. |
||
| 96 | * |
||
| 97 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup[] |
||
| 98 | * A table header array. |
||
| 99 | */ |
||
| 100 | protected function buildMainTableHeader() { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Build the main table rows. |
||
| 116 | * |
||
| 117 | * @param \Drupal\mongodb_watchdog\Event[] $events |
||
| 118 | * The event row data. |
||
| 119 | * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate |
||
| 120 | * The template for these events. |
||
| 121 | * |
||
| 122 | * @return array<string,array|string> |
||
| 123 | * A render array for a table. |
||
| 124 | */ |
||
| 125 | protected function buildMainTableRows(array $events, EventTemplate $eventTemplate) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Title callback for mongodb_watchdog.detail. |
||
| 138 | * |
||
| 139 | * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate |
||
| 140 | * The event template for which the title is built. |
||
| 141 | * |
||
| 142 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup |
||
| 143 | * The page title. |
||
| 144 | */ |
||
| 145 | public function buildTitle(EventTemplate $eventTemplate) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | View Code Duplication | public static function create(ContainerInterface $container) { |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Obtain the data from the logger. |
||
| 170 | * |
||
| 171 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 172 | * The current request. Needed for paging. |
||
| 173 | * @param \Drupal\mongodb_watchdog\EventTemplate $eventTemplate |
||
| 174 | * The template for which to build the detail page. |
||
| 175 | * |
||
| 176 | * @return \Drupal\mongodb_watchdog\Event[] |
||
| 177 | * The data array. |
||
| 178 | */ |
||
| 179 | View Code Duplication | protected function getRowData(Request $request, EventTemplate $eventTemplate) { |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Build the heading rows on the per-template event occurrences page. |
||
| 194 | * |
||
| 195 | * @param \Drupal\mongodb_watchdog\EventTemplate|null $eventTemplate |
||
| 196 | * The template for which to provide details. Not actually expected to be |
||
| 197 | * NULL, but this is needed to remain compatible with parent class. |
||
| 198 | * |
||
| 199 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup[] |
||
| 200 | * A render array for a table. |
||
| 201 | */ |
||
| 202 | protected function getTop(EventTemplate $eventTemplate = NULL) { |
||
| 223 | |||
| 224 | } |
||
| 225 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.