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 |
||
| 20 | class RequestController extends ControllerBase { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The core date.formatter service. |
||
| 24 | * |
||
| 25 | * @var \Drupal\Core\Datetime\DateFormatterInterface |
||
| 26 | */ |
||
| 27 | protected $dateFormatter; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * A RfcLogLevel instance, to avoid static access. |
||
| 31 | * |
||
| 32 | * @var \Drupal\Core\Logger\RfcLogLevel |
||
| 33 | */ |
||
| 34 | protected $rfcLogLevel; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The length of the absolute path to the site root, in runes. |
||
| 38 | * |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | protected $rootLength; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * A Unicode instance, to avoid static access. |
||
| 45 | * |
||
| 46 | * @var \Drupal\Component\Utility\Unicode |
||
| 47 | */ |
||
| 48 | protected $unicode; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Controller constructor. |
||
| 52 | * |
||
| 53 | * @param \Psr\Log\LoggerInterface $logger |
||
| 54 | * The logger service, to log intervening events. |
||
| 55 | * @param \Drupal\mongodb_watchdog\Logger $watchdog |
||
| 56 | * The MongoDB logger, to load stored events. |
||
| 57 | * @param \Drupal\Core\Config\ImmutableConfig $config |
||
| 58 | * The module configuration. |
||
| 59 | * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter |
||
| 60 | * The core date.formatter service. |
||
| 61 | * @param \Drupal\Component\Utility\Unicode $unicode |
||
| 62 | * A Unicode instance, to avoid static access. |
||
| 63 | * @param \Drupal\Core\Logger\RfcLogLevel $rfcLogLevel |
||
| 64 | * A RfcLogLevel instance, to avoid static access. |
||
| 65 | */ |
||
| 66 | View Code Duplication | public function __construct( |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Controller. |
||
| 85 | * |
||
| 86 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 87 | * The current request. |
||
| 88 | * @param string $uniqueId |
||
| 89 | * The unique request id from mod_unique_id. Unsafe. |
||
| 90 | * |
||
| 91 | * @return array<string,string|array> |
||
| 92 | * A render array. |
||
| 93 | */ |
||
| 94 | public function build(Request $request, $uniqueId) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Build the main table. |
||
| 117 | * |
||
| 118 | * @param array $rows |
||
| 119 | * The event data. |
||
| 120 | * |
||
| 121 | * @return array<string,string|array> |
||
| 122 | * A render array for the main table. |
||
| 123 | */ |
||
| 124 | View Code Duplication | protected function buildMainTable(array $rows) { |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Build the main table header. |
||
| 135 | * |
||
| 136 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup[] |
||
| 137 | * A table header array. |
||
| 138 | */ |
||
| 139 | protected function buildMainTableHeader() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Build the main table rows. |
||
| 154 | * |
||
| 155 | * @param array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]> $events |
||
| 156 | * A fully loaded array of events and their templates. |
||
| 157 | * |
||
| 158 | * @return array<string,array|string> |
||
| 159 | * A render array for a table. |
||
| 160 | */ |
||
| 161 | protected function buildMainTableRows(array $events) { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | public static function create(ContainerInterface $container) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Obtain the data from the logger. |
||
| 212 | * |
||
| 213 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 214 | * The current request. Needed for paging. |
||
| 215 | * @param string $uniqueId |
||
| 216 | * The request for which to build the detail page. |
||
| 217 | * |
||
| 218 | * @return \Drupal\mongodb_watchdog\Event[] |
||
| 219 | * The data array. |
||
| 220 | */ |
||
| 221 | View Code Duplication | protected function getRowData(Request $request, $uniqueId) { |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Build the heading rows on the per-request event occurrences page. |
||
| 233 | * |
||
| 234 | * @param string $uniqueId |
||
| 235 | * The unique request id. |
||
| 236 | * @param \Drupal\mongodb_watchdog\Event $first |
||
| 237 | * A fully loaded array of events and their templates. |
||
| 238 | * |
||
| 239 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup[] |
||
| 240 | * A render array for a table. |
||
| 241 | */ |
||
| 242 | protected function getTop($uniqueId = NULL, Event $first = NULL) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Convert an absolute path to a relative one if below the site root. |
||
| 272 | * |
||
| 273 | * @param string $path |
||
| 274 | * An absolute path on the filesystem. |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | * A relative path if possible, otherwise the input path. |
||
| 278 | */ |
||
| 279 | public function simplifyPath($path) { |
||
| 286 | |||
| 287 | } |
||
| 288 |
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.