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 | class ManagerFactory |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var MetadataCollector |
||
| 31 | */ |
||
| 32 | private $metadataCollector; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Converter |
||
| 36 | */ |
||
| 37 | private $converter; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var LoggerInterface |
||
| 41 | */ |
||
| 42 | private $logger; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var LoggerInterface |
||
| 46 | */ |
||
| 47 | private $tracer; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var EventDispatcherInterface |
||
| 51 | */ |
||
| 52 | private $eventDispatcher; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Stopwatch |
||
| 56 | */ |
||
| 57 | private $stopwatch; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param MetadataCollector $metadataCollector Metadata collector service. |
||
| 61 | * @param Converter $converter Converter service to transform arrays to objects and visa versa. |
||
| 62 | * @param LoggerInterface $tracer |
||
| 63 | * @param LoggerInterface $logger |
||
| 64 | */ |
||
| 65 | public function __construct($metadataCollector, $converter, $tracer = null, $logger = null) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param EventDispatcherInterface $eventDispatcher |
||
| 75 | */ |
||
| 76 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param Stopwatch $stopwatch |
||
| 83 | */ |
||
| 84 | public function setStopwatch(Stopwatch $stopwatch) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Factory function to create a manager instance. |
||
| 91 | * |
||
| 92 | * @param string $managerName Manager name. |
||
| 93 | * @param array $connection Connection configuration. |
||
| 94 | * @param array $analysis Analyzers, filters and tokenizers config. |
||
| 95 | * @param array $managerConfig Manager configuration. |
||
| 96 | * |
||
| 97 | * @return Manager |
||
| 98 | */ |
||
| 99 | public function createManager($managerName, $connection, $analysis, $managerConfig) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Extracts analysis configuration from all the documents |
||
| 169 | * |
||
| 170 | * @param array $properties Properties of a type or an object |
||
| 171 | * @param array $analysis The full analysis node from configuration |
||
| 172 | * @param array $managerAnalysis The data that is being formed for the manager |
||
| 173 | */ |
||
| 174 | private function extractAnalysisFromProperties($properties, $analysis, &$managerAnalysis) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Extracts tokenizers and filters from analysis configuration |
||
| 194 | * |
||
| 195 | * @param string $type Either filter or tokenizer |
||
| 196 | * @param array $analyzer The current analyzer |
||
| 197 | * @param array $analysis The full analysis node from configuration |
||
| 198 | * @param array $data The data that is being formed for the manager |
||
| 199 | */ |
||
| 200 | private function extractSubData($type, $analyzer, $analysis, &$data) |
||
| 220 | } |
||
| 221 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.