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 TopController extends ControllerBase { |
||
| 18 | const TYPES = [ |
||
| 19 | 'page not found', |
||
| 20 | 'access denied', |
||
| 21 | ]; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The database holding the logger collections. |
||
| 25 | * |
||
| 26 | * @var \MongoDB\Database |
||
| 27 | */ |
||
| 28 | protected $database; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * TopController constructor. |
||
| 32 | * |
||
| 33 | * @param \Psr\Log\LoggerInterface $logger |
||
| 34 | * The logger service, to log intervening events. |
||
| 35 | * @param \Drupal\mongodb_watchdog\Logger $watchdog |
||
| 36 | * The MongoDB logger, to load stored events. |
||
| 37 | * @param \Drupal\Core\Config\ImmutableConfig $config |
||
| 38 | * The module configuration. |
||
| 39 | * @param \MongoDB\Database $database |
||
| 40 | * Needed because there is no group() command in phplib yet. |
||
| 41 | * |
||
| 42 | * @see https://jira.mongodb.org/browse/PHPLIB-177 |
||
| 43 | */ |
||
| 44 | public function __construct( |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Controller. |
||
| 56 | * |
||
| 57 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 58 | * The current request. |
||
| 59 | * @param string $type |
||
| 60 | * The type of top report to produce. |
||
| 61 | * |
||
| 62 | * @return array |
||
|
|
|||
| 63 | * A render array. |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function build(Request $request, $type) { |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Build the main table. |
||
| 79 | * |
||
| 80 | * @param array $rows |
||
| 81 | * The event data. |
||
| 82 | * |
||
| 83 | * @return array<string,string|array> |
||
| 84 | * A render array for the main table. |
||
| 85 | */ |
||
| 86 | View Code Duplication | protected function buildMainTable(array $rows) { |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Build the main table header. |
||
| 97 | * |
||
| 98 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup[] |
||
| 99 | * A table header array. |
||
| 100 | */ |
||
| 101 | protected function buildMainTableHeader() { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Build the main table rows. |
||
| 112 | * |
||
| 113 | * @param array[] $counts |
||
| 114 | * The array of counts per 403/404 page. |
||
| 115 | * |
||
| 116 | * @return array<string,array|string> |
||
| 117 | * A render array for a table. |
||
| 118 | */ |
||
| 119 | protected function buildMainTableRows($counts) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | View Code Duplication | public static function create(ContainerInterface $container) { |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Obtain the data from the logger. |
||
| 153 | * |
||
| 154 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 155 | * The current request. Needed for paging. |
||
| 156 | * @param string $type |
||
| 157 | * The type of top list to retrieve. |
||
| 158 | * |
||
| 159 | * @return array |
||
| 160 | * The data array. |
||
| 161 | */ |
||
| 162 | protected function getRowData(Request $request, $type) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Command wrapper for missing MongoDB group() implementation in PHPlib. |
||
| 200 | * |
||
| 201 | * @param \MongoDB\Collection $collection |
||
| 202 | * The collection on which to perform the command. |
||
| 203 | * @param object $key |
||
| 204 | * The grouping key. |
||
| 205 | * @param object $cond |
||
| 206 | * The condition. |
||
| 207 | * @param string $reduce |
||
| 208 | * The reducer function: must be valid JavaScript code. |
||
| 209 | * @param object $initial |
||
| 210 | * The initial document. |
||
| 211 | * |
||
| 212 | * @return array|void |
||
| 213 | * Void in case of error, otherwise an array with the following keys: |
||
| 214 | * - waitedMS: time spent waiting |
||
| 215 | * - retval: an array of command results, containing at least the key |
||
| 216 | * - count: the total number of documents matched |
||
| 217 | * - keys: the number of different keys, normally matching count(retval) |
||
| 218 | * - ok: 1.0 in case of success. |
||
| 219 | */ |
||
| 220 | public function group(Collection $collection, $key, $cond, $reduce, $initial) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Callback for usort() to sort top entries returned from a group query. |
||
| 238 | * |
||
| 239 | * @param array $first |
||
| 240 | * The first value to compare. |
||
| 241 | * @param array $second |
||
| 242 | * The second value to compare. |
||
| 243 | * |
||
| 244 | * @return int |
||
| 245 | * The comparison result. |
||
| 246 | * |
||
| 247 | * @see \Drupal\mongodb_watchdog\Controller\TopController::build() |
||
| 248 | */ |
||
| 249 | protected function topSort(array $first, array $second) { |
||
| 252 | |||
| 253 | } |
||
| 254 |
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.