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 RequestController implements ContainerInjectionInterface { |
||
18 | |||
19 | /** |
||
20 | * The core date.formatter service. |
||
21 | * |
||
22 | * @var \Drupal\Core\Datetime\DateFormatterInterface |
||
23 | */ |
||
24 | protected $dateFormatter; |
||
25 | |||
26 | /** |
||
27 | * The items_per_page configuration value. |
||
28 | * |
||
29 | * @var int |
||
30 | */ |
||
31 | protected $itemsPerPage; |
||
32 | |||
33 | /** |
||
34 | * The length of the absolute path to the site root, in runes. |
||
35 | * |
||
36 | * @var int |
||
37 | */ |
||
38 | protected $rootLength; |
||
39 | |||
40 | /** |
||
41 | * The mongodb_watchdog logger, to access events. |
||
42 | * |
||
43 | * @var \Drupal\mongodb_watchdog\Logger |
||
44 | */ |
||
45 | protected $watchdog; |
||
46 | |||
47 | /** |
||
48 | * RequestController constructor. |
||
49 | * |
||
50 | * @param \Drupal\mongodb_watchdog\Logger $watchdog |
||
51 | * The MongoDB logger service, to load event data. |
||
52 | * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
||
53 | * The core date.formatter service. |
||
54 | * @param int $items_per_page |
||
55 | * The items_per_page configuration value. |
||
56 | */ |
||
57 | public function __construct(Logger $watchdog, DateFormatterInterface $date_formatter, $items_per_page) { |
||
65 | |||
66 | /** |
||
67 | * Build the top part of the page, about the request. |
||
68 | * |
||
69 | * @param string $unique_id |
||
70 | * The unique request id. |
||
71 | * @param array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]> $events |
||
72 | * A fully loaded array of events and their templates. |
||
73 | * |
||
74 | * @return array |
||
|
|||
75 | * A render array for a table. |
||
76 | */ |
||
77 | public function buildTrackRequest($unique_id, array $events) { |
||
111 | |||
112 | /** |
||
113 | * Build the bottom part of the page, about the events during the request. |
||
114 | * |
||
115 | * @param array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]> $events |
||
116 | * A fully loaded array of events and their templates. |
||
117 | * |
||
118 | * @return array |
||
119 | * A render array for a table. |
||
120 | */ |
||
121 | public function buildTrackRows(array $events) { |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | View Code Duplication | public static function create(ContainerInterface $container) { |
|
177 | |||
178 | /** |
||
179 | * Set up the templates pager. |
||
180 | * |
||
181 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
182 | * The current request. |
||
183 | * @param int $uniqueId |
||
184 | * The uniqueId of the current request. |
||
185 | * |
||
186 | * @return int |
||
187 | * The number of the page to display, starting at 0. |
||
188 | */ |
||
189 | View Code Duplication | public function setupPager(Request $request, $uniqueId) { |
|
207 | |||
208 | /** |
||
209 | * Convert an absolute path to a relative one if below the site root. |
||
210 | * |
||
211 | * @param string $path |
||
212 | * An absolute path on the filesystem. |
||
213 | * |
||
214 | * @return string |
||
215 | * A relative path if possible, otherwise the input path. |
||
216 | */ |
||
217 | public function simplifyPath($path) { |
||
223 | |||
224 | /** |
||
225 | * Controller. |
||
226 | * |
||
227 | * @param string $uniqueId |
||
228 | * The unique request id from mod_unique_id. Unsafe. |
||
229 | * |
||
230 | * @return array |
||
231 | * A render array. |
||
232 | */ |
||
233 | public function track(Request $request, $uniqueId) { |
||
255 | |||
256 | } |
||
257 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.