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 |
||
| 21 | class Importer |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var Extractor |
||
| 25 | */ |
||
| 26 | private $extractor; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $config; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param Extractor $extractor |
||
| 35 | */ |
||
| 36 | public function __construct(Extractor $extractor) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param Finder $finder |
||
| 43 | * @param MessageCatalogue[] $catalogue |
||
|
|
|||
| 44 | * @param array $config { |
||
| 45 | * |
||
| 46 | * @var array $blacklist_domains Blacklist the domains we should exclude. Cannot be used with whitelist. |
||
| 47 | * @var array $whitelist_domains Whitlelist the domains we should include. Cannot be used with blacklist. |
||
| 48 | * @var string project_root The project root will be removed from the source location. |
||
| 49 | * } |
||
| 50 | * |
||
| 51 | * @return MessageCatalogue[] |
||
| 52 | */ |
||
| 53 | public function extractToCatalogues(Finder $finder, array $catalogues, array $config = []) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * See docs for extractToCatalogues. |
||
| 83 | * |
||
| 84 | * @return MessageCatalogue |
||
| 85 | */ |
||
| 86 | public function extractToCatalogue(Finder $finder, MessageCatalogue $catalogue, array $config = []) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param MessageCatalogue $catalogue |
||
| 95 | * @param SourceCollection $collection |
||
| 96 | */ |
||
| 97 | private function convertSourceLocationsToMessages(MessageCatalogue $catalogue, SourceCollection $collection) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param MessageCatalogue $catalogue |
||
| 123 | * @param $sourceLocation |
||
| 124 | * @param $domain |
||
| 125 | * @param $type |
||
| 126 | * @param $value |
||
| 127 | */ |
||
| 128 | private function addMetadata(MessageCatalogue $catalogue, $key, $domain, $type, $value) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $domain |
||
| 141 | * |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | private function isValidDomain($domain) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Make sure the configuration is valid. |
||
| 158 | * |
||
| 159 | * @param array $config |
||
| 160 | */ |
||
| 161 | private function processConfig($config) |
||
| 185 | } |
||
| 186 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.