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 |
||
17 | View Code Duplication | class SourceFactory |
|
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected static $defaultSourceClasses = [ |
||
23 | '\Crossjoin\Browscap\Source\Ini\BrowscapOrg', |
||
24 | '\Crossjoin\Browscap\Source\Ini\PhpSetting', |
||
25 | ]; |
||
26 | |||
27 | /** |
||
28 | * @var array|null |
||
29 | */ |
||
30 | protected static $sourceClasses; |
||
31 | |||
32 | /** |
||
33 | * @return SourceInterface |
||
34 | * @throws UnexpectedValueException |
||
35 | */ |
||
36 | public static function getInstance() : SourceInterface |
||
52 | |||
53 | public static function setDefaultSourceClasses() |
||
57 | |||
58 | /** |
||
59 | * @param array $fullyQualifiedClassNames |
||
60 | * |
||
61 | * @throws InvalidArgumentException |
||
62 | */ |
||
63 | public static function setSourceClasses(array $fullyQualifiedClassNames) |
||
81 | |||
82 | /** |
||
83 | * @param string $className |
||
84 | * |
||
85 | * @return SourceInterface|null |
||
86 | * @throws UnexpectedValueException |
||
87 | */ |
||
88 | protected static function getInstanceByClassName(string $className) |
||
111 | } |
||
112 |
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.