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 | * The length of the absolute path to the site root, in runes. |
||
| 31 | * |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $rootLength; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * A Unicode instance, to avoid static access. |
||
| 38 | * |
||
| 39 | * @var \Drupal\Component\Utility\Unicode |
||
| 40 | */ |
||
| 41 | protected $unicode; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Controller constructor. |
||
| 45 | * |
||
| 46 | * @param \Psr\Log\LoggerInterface $logger |
||
| 47 | * The logger service, to log intervening events. |
||
| 48 | * @param \Drupal\mongodb_watchdog\Logger $watchdog |
||
| 49 | * The MongoDB logger, to load stored events. |
||
| 50 | * @param \Drupal\Core\Config\ImmutableConfig $config |
||
| 51 | * The module configuration. |
||
| 52 | * @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter |
||
| 53 | * The core date.formatter service. |
||
| 54 | * @param \Drupal\Component\Utility\Unicode $unicode |
||
| 55 | * A Unicode instance, to avoid static access. |
||
| 56 | */ |
||
| 57 | public function __construct( |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Controller. |
||
| 78 | * |
||
| 79 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 80 | * The current request. |
||
| 81 | * @param string $uniqueId |
||
| 82 | * The unique request id from mod_unique_id. Unsafe. |
||
| 83 | * |
||
| 84 | * @return array<string,string|array> |
||
| 85 | * A render array. |
||
| 86 | */ |
||
| 87 | public function build(Request $request, $uniqueId) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Build the main table. |
||
| 110 | * |
||
| 111 | * @param array $rows |
||
| 112 | * The event data. |
||
| 113 | * |
||
| 114 | * @return array<string,string|array> |
||
|
|
|||
| 115 | * A render array for the main table. |
||
| 116 | */ |
||
| 117 | View Code Duplication | protected function buildMainTable(array $rows) { |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Build the main table header. |
||
| 128 | * |
||
| 129 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup[] |
||
| 130 | * A table header array. |
||
| 131 | */ |
||
| 132 | protected function buildMainTableHeader() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Build the main table rows. |
||
| 147 | * |
||
| 148 | * @param array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]> $events |
||
| 149 | * A fully loaded array of events and their templates. |
||
| 150 | * |
||
| 151 | * @return array<string,array|string> |
||
| 152 | * A render array for a table. |
||
| 153 | */ |
||
| 154 | protected function buildMainTableRows(array $events) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritdoc} |
||
| 183 | */ |
||
| 184 | public static function create(ContainerInterface $container) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Obtain the data from the logger. |
||
| 206 | * |
||
| 207 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 208 | * The current request. Needed for paging. |
||
| 209 | * @param string $uniqueId |
||
| 210 | * The request for which to build the detail page. |
||
| 211 | * |
||
| 212 | * @return \Drupal\mongodb_watchdog\Event[] |
||
| 213 | * The data array. |
||
| 214 | */ |
||
| 215 | View Code Duplication | protected function getRowData(Request $request, $uniqueId) { |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Build the heading rows on the per-request event occurrences page. |
||
| 227 | * |
||
| 228 | * @param string $uniqueId |
||
| 229 | * The unique request id. |
||
| 230 | * @param \Drupal\mongodb_watchdog\Event $first |
||
| 231 | * A fully loaded array of events and their templates. |
||
| 232 | * |
||
| 233 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup[] |
||
| 234 | * A render array for a table. |
||
| 235 | */ |
||
| 236 | protected function getTop($uniqueId = NULL, Event $first = NULL) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Convert an absolute path to a relative one if below the site root. |
||
| 266 | * |
||
| 267 | * @param string $path |
||
| 268 | * An absolute path on the filesystem. |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | * A relative path if possible, otherwise the input path. |
||
| 272 | */ |
||
| 273 | public function simplifyPath($path) { |
||
| 280 | |||
| 281 | } |
||
| 282 |
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.