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 | View Code Duplication | class Intraface_XMLRPC_Server0100 |
|
|
|||
28 | { |
||
29 | /** |
||
30 | * @var struct $credentials |
||
31 | */ |
||
32 | protected $credentials; |
||
33 | |||
34 | /** |
||
35 | * @var object $kernel intraface kernel |
||
36 | */ |
||
37 | protected $kernel; |
||
38 | |||
39 | /** |
||
40 | * @var array with valid encodings |
||
41 | */ |
||
42 | protected $valid_encodings = array('utf-8', 'iso-8859-1'); |
||
43 | |||
44 | /** |
||
45 | * Constructor |
||
46 | * |
||
47 | * @param string $encoding The encoding wich the server recieves and returns data in |
||
48 | * |
||
49 | * @return void |
||
50 | */ |
||
51 | public function __construct($encoding = 'utf-8') |
||
58 | |||
59 | /** |
||
60 | * Checks credentials |
||
61 | * |
||
62 | * @param struct $credentials Credentials to use the server |
||
63 | * |
||
64 | * @return array |
||
65 | */ |
||
66 | protected function checkCredentials($credentials) |
||
101 | |||
102 | /** |
||
103 | * Prepares response to be sent with the correct UTF-8 encoding. |
||
104 | * |
||
105 | * @param mixed $values Array or string to decode |
||
106 | * |
||
107 | * @return mixed UTF8 decoded request |
||
108 | */ |
||
109 | protected function prepareResponseData($values) |
||
119 | |||
120 | /** |
||
121 | * Process data from client, so that data is returned with the correct encoding. |
||
122 | * |
||
123 | * @param mixed $values Array or string to decode |
||
124 | * |
||
125 | * @return mixed correct encoded response |
||
126 | */ |
||
127 | protected function processRequestData($values) |
||
138 | |||
139 | function handleNull($value) |
||
146 | |||
147 | protected function recursiveMap($function, $values) |
||
162 | } |
||
163 |
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.