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 |
||
| 18 | View Code Duplication | class AdapterFactory |
|
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | protected static $defaultAdapterClasses = [ |
||
| 24 | '\Crossjoin\Browscap\Parser\Sqlite\Adapter\Pdo', |
||
| 25 | '\Crossjoin\Browscap\Parser\Sqlite\Adapter\Sqlite3', |
||
| 26 | ]; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array|null |
||
| 30 | */ |
||
| 31 | protected static $adapterClasses; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param string $fileName |
||
| 35 | * |
||
| 36 | * @return AdapterInterface |
||
| 37 | * @throws ParserConditionNotSatisfiedException |
||
| 38 | * @throws UnexpectedValueException |
||
| 39 | */ |
||
| 40 | public static function getInstance(string $fileName) : AdapterInterface |
||
| 58 | |||
| 59 | public static function setDefaultAdapterClasses() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param array $fullyQualifiedClassNames |
||
| 66 | * |
||
| 67 | * @throws InvalidArgumentException |
||
| 68 | */ |
||
| 69 | public static function setAdapterClasses(array $fullyQualifiedClassNames) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $className |
||
| 90 | * @param string $fileName |
||
| 91 | * |
||
| 92 | * @return AdapterInterface|null |
||
| 93 | * @throws UnexpectedValueException |
||
| 94 | */ |
||
| 95 | protected static function getInstanceByClassName(string $className, string $fileName) |
||
| 118 | } |
||
| 119 |
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.