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 |
||
27 | final class Importer |
||
28 | { |
||
29 | /** |
||
30 | * @var Extractor |
||
31 | */ |
||
32 | private $extractor; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $config; |
||
38 | |||
39 | /** |
||
40 | * @var MetadataWriter |
||
41 | */ |
||
42 | private $metadataWriter; |
||
43 | |||
44 | /** |
||
45 | * @param Extractor $extractor |
||
46 | * @param MetadataWriter $metadataWriter |
||
47 | */ |
||
48 | public function __construct(Extractor $extractor, MetadataWriter $metadataWriter) |
||
53 | |||
54 | /** |
||
55 | * @param Finder $finder |
||
56 | * @param MessageCatalogue[] $catalogues |
||
57 | * @param array $config { |
||
58 | * |
||
59 | * @var array $blacklist_domains Blacklist the domains we should exclude. Cannot be used with whitelist. |
||
60 | * @var array $whitelist_domains Whitlelist the domains we should include. Cannot be used with blacklist. |
||
61 | * @var string project_root The project root will be removed from the source location. |
||
62 | * } |
||
63 | * |
||
64 | * @param array $errors from extractor, by reference. |
||
65 | * |
||
66 | * @return MessageCatalogue[] |
||
67 | */ |
||
68 | public function extractToCatalogues(Finder $finder, array $catalogues, array $config = [], &$errors = []) |
||
96 | |||
97 | /** |
||
98 | * See docs for extractToCatalogues. |
||
99 | * |
||
100 | * @return MessageCatalogue |
||
101 | */ |
||
102 | public function extractToCatalogue(Finder $finder, MessageCatalogue $catalogue, array $config = []) |
||
108 | |||
109 | /** |
||
110 | * @param MessageCatalogue $catalogue |
||
111 | * @param SourceCollection $collection |
||
112 | */ |
||
113 | private function convertSourceLocationsToMessages(MessageCatalogue $catalogue, SourceCollection $collection) |
||
136 | |||
137 | /** |
||
138 | * @param string $domain |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | private function isValidDomain($domain) |
||
153 | |||
154 | /** |
||
155 | * Make sure the configuration is valid. |
||
156 | * |
||
157 | * @param array $config |
||
158 | */ |
||
159 | private function processConfig($config) |
||
183 | } |
||
184 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.