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 InMemoryFileRepository implements FileRepository |
||
28 | { |
||
29 | /** |
||
30 | * File collection. |
||
31 | * |
||
32 | * @var File[] |
||
33 | */ |
||
34 | private $files; |
||
35 | |||
36 | /** |
||
37 | * The file event bus, it can be null. |
||
38 | * |
||
39 | * @var FileEventBus|null |
||
40 | */ |
||
41 | private $eventBus; |
||
42 | |||
43 | /** |
||
44 | * Constructor. |
||
45 | * |
||
46 | * @param FileEventBus|null $anEventBus The file event bus, it can be null |
||
47 | */ |
||
48 | public function __construct(FileEventBus $anEventBus = null) |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function fileOfId(FileId $anId) |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | public function query($aSpecification) |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function singleResultQuery($aSpecification) |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function count($aSpecification) |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | public function fileOfName(FileName $aName) |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function all() |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | View Code Duplication | public function persist(File $aFile) |
|
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | View Code Duplication | public function remove(File $aFile) |
|
131 | } |
||
132 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.