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 |
||
10 | class ApiResponse implements \ArrayAccess |
||
11 | { |
||
12 | // HTTP response status code |
||
13 | protected $statusCode; |
||
14 | |||
15 | // response assoc array |
||
16 | protected $response; |
||
17 | |||
18 | public function __construct($statusCode, $responseBody = null) |
||
35 | |||
36 | /** |
||
37 | * Return HTTP response status code |
||
38 | * |
||
39 | * @return int |
||
40 | */ |
||
41 | public function getStatusCode() |
||
45 | |||
46 | /** |
||
47 | * HTTP request was successful |
||
48 | * |
||
49 | * @return bool |
||
50 | */ |
||
51 | public function isSuccessful() |
||
55 | |||
56 | /** |
||
57 | * Allow to access for the property throw class method |
||
58 | * |
||
59 | * @param string $name |
||
60 | * @return mixed |
||
61 | */ |
||
62 | public function __call($name, $arguments) |
||
73 | |||
74 | /** |
||
75 | * Allow to access for the property throw object property |
||
76 | * |
||
77 | * @param string $name |
||
78 | * @return mixed |
||
79 | */ |
||
80 | View Code Duplication | public function __get($name) |
|
88 | |||
89 | /** |
||
90 | * @param mixed $offset |
||
91 | * @param mixed $value |
||
92 | */ |
||
93 | public function offsetSet($offset, $value) |
||
97 | |||
98 | /** |
||
99 | * @param mixed $offset |
||
100 | */ |
||
101 | public function offsetUnset($offset) |
||
105 | |||
106 | /** |
||
107 | * @param mixed $offset |
||
108 | * @return bool |
||
109 | */ |
||
110 | public function offsetExists($offset) |
||
114 | |||
115 | /** |
||
116 | * @param mixed $offset |
||
117 | * @return mixed |
||
118 | */ |
||
119 | View Code Duplication | public function offsetGet($offset) |
|
127 | } |
||
128 |
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.