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 |
||
| 10 | class TranslatorDataCollector extends AbstractDataCollector |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var DataCollectorTranslator |
||
| 14 | */ |
||
| 15 | private $translator; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var UrlGeneratorInterface |
||
| 19 | */ |
||
| 20 | private $urlGenerator; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * TranslatorDataCollector constructor. |
||
| 24 | * |
||
| 25 | * @param DataCollectorTranslator $translator |
||
| 26 | * @param UrlGeneratorInterface $urlGenerator |
||
| 27 | */ |
||
| 28 | public function __construct(DataCollectorTranslator $translator, UrlGeneratorInterface $urlGenerator) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @return array |
||
|
|
|||
| 36 | */ |
||
| 37 | public function getAccessRoles() |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return array |
||
| 44 | */ |
||
| 45 | public function collectData() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param Request $request |
||
| 83 | * @param Response $response |
||
| 84 | * @param \Exception|null $exception |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function collect(Request $request, Response $response, \Exception $exception = null) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Gets the data for template |
||
| 98 | * |
||
| 99 | * @return array The request events |
||
| 100 | */ |
||
| 101 | public function getTemplateData() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function getName() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | public function isEnabled() |
||
| 121 | |||
| 122 | public function reset() |
||
| 126 | } |
||
| 127 |
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.