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 |
||
23 | View Code Duplication | class PersonalDataExporterManager |
|
24 | { |
||
25 | |||
26 | public function __construct() |
||
33 | |||
34 | |||
35 | /** |
||
36 | * Adds EE's exporters to the list of WP exporters |
||
37 | * |
||
38 | * @param array $exporters |
||
39 | * @return array |
||
40 | */ |
||
41 | public function hookInExporters($exporters) |
||
42 | { |
||
43 | // load all the privacy policy stuff |
||
44 | // add post policy text |
||
45 | foreach ($this->loadPrivateDataExporterCollection() as $exporter) { |
||
|
|||
46 | $exporters[ get_class($exporter) ] = array( |
||
47 | 'exporter_friendly_name' => $exporter->name(), |
||
48 | 'callback' => array($exporter, 'export'), |
||
49 | ); |
||
50 | } |
||
51 | return $exporters; |
||
52 | } |
||
53 | |||
54 | |||
55 | /** |
||
56 | * @return CollectionInterface|PersonalDataExporterInterface[] |
||
57 | * @throws InvalidIdentifierException |
||
58 | * @throws InvalidInterfaceException |
||
59 | * @throws InvalidFilePathException |
||
60 | * @throws InvalidEntityException |
||
61 | * @throws InvalidDataTypeException |
||
62 | * @throws InvalidClassException |
||
63 | */ |
||
64 | protected function loadPrivateDataExporterCollection() |
||
88 | } |
||
89 | |||
92 |
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.