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) { |
|
| 66 | $top = $this->getTop(); |
||
| 67 | |||
| 68 | $rows = $this->getRowData($request, $type); |
||
| 69 | $main = empty($rows) |
||
| 70 | ? [ |
||
| 71 | '#markup' => t('No "%type" message found', ['%type' => $type]), |
||
| 72 | '#prefix' => '<div class="mongodb_watchdog__message">', |
||
| 73 | '#suffix' => '</div>', |
||
| 74 | ] |
||
| 75 | : $this->buildMainTable($rows); |
||
| 76 | |||
| 77 | $ret = $this->buildDefaults($main, $top); |
||
| 78 | return $ret; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Build the main table. |
||
| 83 | * |
||
| 84 | * @param array $rows |
||
| 85 | * The event data. |
||
| 86 | * |
||
| 87 | * @return array<string,string|array> |
||
| 88 | * A render array for the main table. |
||
| 89 | */ |
||
| 90 | View Code Duplication | protected function buildMainTable(array $rows) { |
|
| 91 | $ret = [ |
||
| 92 | '#type' => 'table', |
||
| 93 | '#header' => $this->buildMainTableHeader(), |
||
| 94 | '#rows' => $this->buildMainTableRows($rows), |
||
| 95 | ]; |
||
| 96 | return $ret; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Build the main table header. |
||
| 101 | * |
||
| 102 | * @return \Drupal\Core\StringTranslation\TranslatableMarkup[] |
||
| 103 | * A table header array. |
||
| 104 | */ |
||
| 105 | protected function buildMainTableHeader() { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Build the main table rows. |
||
| 116 | * |
||
| 117 | * @param array[] $counts |
||
| 118 | * The array of counts per 403/404 page. |
||
| 119 | * |
||
| 120 | * @return array<string,array|string> |
||
| 121 | * A render array for a table. |
||
| 122 | */ |
||
| 123 | protected function buildMainTableRows($counts) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | View Code Duplication | public static function create(ContainerInterface $container) { |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Obtain the data from the logger. |
||
| 157 | * |
||
| 158 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 159 | * The current request. Needed for paging. |
||
| 160 | * @param string $type |
||
| 161 | * The type of top list to retrieve. |
||
| 162 | * |
||
| 163 | * @return array |
||
| 164 | * The data array. |
||
| 165 | */ |
||
| 166 | protected function getRowData(Request $request, $type) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Command wrapper for missing MongoDB group() implementation in PHPlib. |
||
| 204 | * |
||
| 205 | * @param \MongoDB\Collection $collection |
||
| 206 | * The collection on which to perform the command. |
||
| 207 | * @param object $key |
||
| 208 | * The grouping key. |
||
| 209 | * @param object $cond |
||
| 210 | * The condition. |
||
| 211 | * @param string $reduce |
||
| 212 | * The reducer function: must be valid JavaScript code. |
||
| 213 | * @param object $initial |
||
| 214 | * The initial document. |
||
| 215 | * |
||
| 216 | * @return array|void |
||
| 217 | * Void in case of error, otherwise an array with the following keys: |
||
| 218 | * - waitedMS: time spent waiting |
||
| 219 | * - retval: an array of command results, containing at least the key |
||
| 220 | * - count: the total number of documents matched |
||
| 221 | * - keys: the number of different keys, normally matching count(retval) |
||
| 222 | * - ok: 1.0 in case of success. |
||
| 223 | */ |
||
| 224 | public function group(Collection $collection, $key, $cond, $reduce, $initial) { |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Callback for usort() to sort top entries returned from a group query. |
||
| 242 | * |
||
| 243 | * @param array $first |
||
| 244 | * The first value to compare. |
||
| 245 | * @param array $second |
||
| 246 | * The second value to compare. |
||
| 247 | * |
||
| 248 | * @return int |
||
| 249 | * The comparison result. |
||
| 250 | * |
||
| 251 | * @see \Drupal\mongodb_watchdog\Controller\TopController::build() |
||
| 252 | */ |
||
| 253 | protected function topSort(array $first, array $second) { |
||
| 256 | |||
| 257 | } |
||
| 258 |
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.