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 Form |
||
11 | { |
||
12 | /** |
||
13 | * @var Mautic |
||
14 | */ |
||
15 | protected $mautic; |
||
16 | |||
17 | /** |
||
18 | * Form ID |
||
19 | * |
||
20 | * @var int |
||
21 | */ |
||
22 | protected $id; |
||
23 | |||
24 | /** |
||
25 | * Constructor |
||
26 | * |
||
27 | * @param Mautic $mautic |
||
28 | * @param int $id |
||
29 | */ |
||
30 | 14 | public function __construct(Mautic $mautic, $id) |
|
35 | |||
36 | /** |
||
37 | * Submit the $data array to the Mautic form |
||
38 | * Returns array containing info about the request, response and cookie |
||
39 | * |
||
40 | * @param array $data |
||
41 | * |
||
42 | * @return array |
||
43 | */ |
||
44 | public function submit(array $data) |
||
93 | |||
94 | /** |
||
95 | * Finds the session ID hash in the response header |
||
96 | * |
||
97 | * @param string $headers |
||
98 | * |
||
99 | * @return string|null |
||
100 | */ |
||
101 | 2 | View Code Duplication | public function getSessionIdFromHeader($headers) |
115 | |||
116 | /** |
||
117 | * Finds the Mautic Contact ID hash in the response header |
||
118 | * |
||
119 | * @param string $headers |
||
120 | * @param string $sessionId |
||
121 | * |
||
122 | * @return string|null |
||
123 | */ |
||
124 | 2 | View Code Duplication | public function getContactIdFromHeader($headers, $sessionId) |
138 | |||
139 | /** |
||
140 | * Prepares data for CURL request based on provided form data, $_COOKIE and $_SERVER |
||
141 | * |
||
142 | * @param array $data |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | 2 | public function prepareRequest(array $data) |
|
181 | |||
182 | /** |
||
183 | * Builds the form URL |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | 4 | public function getUrl() |
|
191 | |||
192 | /** |
||
193 | * Returns the Form ID |
||
194 | * |
||
195 | * @return int |
||
196 | */ |
||
197 | 6 | public function getId() |
|
201 | } |
||
202 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: