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 |
||
20 | class ProfilingStorageDriverDecorator implements StorageDriver |
||
21 | { |
||
22 | /** @var StorageProfiler */ |
||
23 | private $profiler; |
||
24 | /** @var StorageDriver */ |
||
25 | private $driver; |
||
26 | |||
27 | /** |
||
28 | * ProfilingDriverDecorator constructor. |
||
29 | * |
||
30 | * @param StorageProfiler $profiler |
||
31 | * @param StorageDriver $driver |
||
32 | */ |
||
33 | public function __construct(StorageProfiler $profiler, StorageDriver $driver) |
||
38 | |||
39 | /** |
||
40 | * @return \ReflectionClass |
||
41 | */ |
||
42 | public function getEntityBaseClass() |
||
46 | |||
47 | /** |
||
48 | * @return mixed // TODO: better return type |
||
49 | */ |
||
50 | public function buildIndexes() |
||
60 | |||
61 | /** |
||
62 | * @param mixed $item |
||
63 | * |
||
64 | * @return Result\InsertOneResult |
||
65 | * |
||
66 | * @throws DuplicateError |
||
67 | */ |
||
68 | View Code Duplication | public function insert($item) |
|
80 | |||
81 | /** |
||
82 | * Insert ore update an item |
||
83 | * |
||
84 | * @param mixed $item |
||
85 | * |
||
86 | * @return Result\SaveOneResult |
||
87 | * |
||
88 | * @throws DuplicateError |
||
89 | */ |
||
90 | View Code Duplication | public function save($item) |
|
102 | |||
103 | /** |
||
104 | * @param $item |
||
105 | * |
||
106 | * @return Result\RemoveResult |
||
107 | */ |
||
108 | View Code Duplication | public function remove($item) |
|
120 | |||
121 | /** |
||
122 | * Remove all from this collection |
||
123 | * |
||
124 | * @param array|null $query |
||
125 | * |
||
126 | * @return Result\RemoveResult |
||
127 | */ |
||
128 | public function removeAll(array $query = null) |
||
140 | |||
141 | /** |
||
142 | * @param $query |
||
143 | * |
||
144 | * @return Cursor |
||
145 | */ |
||
146 | View Code Duplication | public function find(array $query = null) |
|
158 | |||
159 | /** |
||
160 | * @param array|null $query |
||
161 | * |
||
162 | * @return mixed|null |
||
163 | */ |
||
164 | View Code Duplication | public function findOne(array $query = null) |
|
176 | |||
177 | /** |
||
178 | * @param string $name |
||
179 | * @param array $data |
||
180 | * @param callable $inner |
||
181 | * |
||
182 | * @return mixed |
||
183 | */ |
||
184 | private function profile($name, array $data, callable $inner) |
||
194 | } |
||
195 |
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.