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 |
||
7 | class _Object { |
||
8 | |||
9 | # File name interface |
||
10 | |||
11 | protected static $file_name = ''; |
||
12 | |||
13 | # Properties list |
||
14 | |||
15 | protected $properties = []; |
||
16 | |||
17 | # Add array property |
||
18 | |||
19 | protected function addArray(string $name) { |
||
23 | |||
24 | # Add boolean property |
||
25 | |||
26 | protected function addBoolean(string $name) { |
||
30 | |||
31 | # Add integer property |
||
32 | |||
33 | protected function addInteger(string $name) { |
||
37 | |||
38 | # Add object property |
||
39 | |||
40 | protected function addObject(string $name) { |
||
44 | |||
45 | # Add string property |
||
46 | |||
47 | protected function addString(string $name) { |
||
51 | |||
52 | # Validate data |
||
53 | |||
54 | public function validate($data) { |
||
71 | |||
72 | # Load JSON file |
||
73 | |||
74 | View Code Duplication | public function load() { |
|
88 | |||
89 | # Save JSON file |
||
90 | |||
91 | View Code Duplication | public function save(array $data) { |
|
105 | |||
106 | # Remove JSON file |
||
107 | |||
108 | public function remove() { |
||
116 | } |
||
117 | } |
||
118 |
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.