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 |
||
| 18 | class RequestController extends ControllerBase implements ContainerInjectionInterface { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The core date.formatter service. |
||
| 22 | * |
||
| 23 | * @var \Drupal\Core\Datetime\DateFormatterInterface |
||
| 24 | */ |
||
| 25 | protected $dateFormatter; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The length of the absolute path to the site root, in runes. |
||
| 29 | * |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | protected $rootLength; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The MongoDB logger, to load events. |
||
| 36 | * |
||
| 37 | * @var \Drupal\mongodb_watchdog\Logger |
||
| 38 | */ |
||
| 39 | protected $watchdog; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Controller constructor. |
||
| 43 | * |
||
| 44 | * @param \Drupal\mongodb_watchdog\Logger $watchdog |
||
| 45 | * The MongoDB logger service, to load event data. |
||
| 46 | * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
||
| 47 | * The core date.formatter service. |
||
| 48 | * @param \Drupal\Core\Config\ImmutableConfig $config |
||
| 49 | * The module configuration. |
||
| 50 | */ |
||
| 51 | public function __construct( |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Controller. |
||
| 65 | * |
||
| 66 | * @param string $uniqueId |
||
| 67 | * The unique request id from mod_unique_id. Unsafe. |
||
| 68 | * |
||
| 69 | * @return array |
||
|
|
|||
| 70 | * A render array. |
||
| 71 | */ |
||
| 72 | public function build(Request $request, $uniqueId) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Build the top part of the page, about the request. |
||
| 98 | * |
||
| 99 | * @param string $unique_id |
||
| 100 | * The unique request id. |
||
| 101 | * @param array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]> $events |
||
| 102 | * A fully loaded array of events and their templates. |
||
| 103 | * |
||
| 104 | * @return array |
||
| 105 | * A render array for a table. |
||
| 106 | */ |
||
| 107 | public function buildRequest($unique_id, array $events) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Build the bottom part of the page, about the events during the request. |
||
| 144 | * |
||
| 145 | * @param array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]> $events |
||
| 146 | * A fully loaded array of events and their templates. |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | * A render array for a table. |
||
| 150 | */ |
||
| 151 | public function buildRows(array $events) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritdoc} |
||
| 194 | */ |
||
| 195 | public static function create(ContainerInterface $container) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Set up the templates pager. |
||
| 210 | * |
||
| 211 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 212 | * The current request. |
||
| 213 | * @param int $uniqueId |
||
| 214 | * The uniqueId of the current request. |
||
| 215 | * |
||
| 216 | * @return int |
||
| 217 | * The number of the page to display, starting at 0. |
||
| 218 | */ |
||
| 219 | View Code Duplication | public function setupPager(Request $request, $uniqueId) { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Convert an absolute path to a relative one if below the site root. |
||
| 240 | * |
||
| 241 | * @param string $path |
||
| 242 | * An absolute path on the filesystem. |
||
| 243 | * |
||
| 244 | * @return string |
||
| 245 | * A relative path if possible, otherwise the input path. |
||
| 246 | */ |
||
| 247 | public function simplifyPath($path) { |
||
| 253 | |||
| 254 | } |
||
| 255 |
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.