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 |
||
16 | class Users_Register_Controller extends Controller |
||
|
|||
17 | { |
||
18 | |||
19 | /** |
||
20 | * URL That you can access this from |
||
21 | * |
||
22 | * @config |
||
23 | */ |
||
24 | private static $url_segment = "users/register"; |
||
25 | |||
26 | /** |
||
27 | * Current actions available to this controller |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private static $allowed_actions = array( |
||
32 | "index", |
||
33 | "sendverification", |
||
34 | "verify", |
||
35 | "RegisterForm" |
||
36 | ); |
||
37 | |||
38 | /** |
||
39 | * Internal function designed to allow us to send a verification |
||
40 | * email from multiple locations |
||
41 | * |
||
42 | * @param $member Member object to send email to |
||
43 | * |
||
44 | * @return boolean |
||
45 | */ |
||
46 | protected function send_verification_email(Member $member) |
||
54 | |||
55 | /** |
||
56 | * Get the link to this controller |
||
57 | * |
||
58 | * @param string $action The URL endpoint for this controller |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | public function Link($action = null) |
||
69 | |||
70 | /** |
||
71 | * Get an absolute link to this controller |
||
72 | * |
||
73 | * @param string $action The URL endpoint for this controller |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | public function AbsoluteLink($action = null) |
||
81 | |||
82 | /** |
||
83 | * Get a relative (to the root url of the site) link to this |
||
84 | * controller |
||
85 | * |
||
86 | * @param string $action The URL endpoint for this controller |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | public function RelativeLink($action = null) |
||
96 | |||
97 | /** |
||
98 | * If content controller exists, return it's menu function |
||
99 | * |
||
100 | * @param int $level Menu level to return. |
||
101 | * |
||
102 | * @return ArrayList |
||
103 | */ |
||
104 | View Code Duplication | public function getMenu($level = 1) |
|
111 | |||
112 | /** |
||
113 | * Shortcut for getMenu |
||
114 | * |
||
115 | * @param int $level Menu level to return. |
||
116 | * |
||
117 | * @return ArrayList |
||
118 | */ |
||
119 | public function Menu($level) |
||
123 | |||
124 | /** |
||
125 | * Default action this controller will deal with |
||
126 | * |
||
127 | * @param SS_HTTPRequest $request Current request |
||
128 | * |
||
129 | * @return HTMLText |
||
130 | */ |
||
131 | public function index(SS_HTTPRequest $request) |
||
151 | |||
152 | |||
153 | /** |
||
154 | * Send a verification email to the user provided (if verification |
||
155 | * emails are enabled and account is not already verified) |
||
156 | * |
||
157 | * @param SS_HTTPRequest $request Current request |
||
158 | * |
||
159 | * @return HTMLText |
||
160 | */ |
||
161 | public function sendverification(SS_HTTPRequest $request) |
||
202 | |||
203 | /** |
||
204 | * Verify the provided user (ID) using the verification code (Other |
||
205 | * ID) provided |
||
206 | * |
||
207 | * @param SS_HTTPRequest $request Current Request |
||
208 | * |
||
209 | * @return HTMLText |
||
210 | */ |
||
211 | public function verify(SS_HTTPRequest $request) |
||
254 | |||
255 | /** |
||
256 | * Registration form |
||
257 | * |
||
258 | * @return Form |
||
259 | */ |
||
260 | public function RegisterForm() |
||
319 | |||
320 | /** |
||
321 | * Register a new member. This action is deigned to be intercepted at 2 |
||
322 | * points: |
||
323 | * |
||
324 | * - Modify the initial member filter (so that you can perfom bespoke |
||
325 | * member filtering |
||
326 | * |
||
327 | * - Modify the member user before saving (so we can add extra permissions |
||
328 | * etc) |
||
329 | * |
||
330 | * @param array $data User submitted data |
||
331 | * @param Form $form Registration form |
||
332 | * |
||
333 | * @return SS_HTTPResponse |
||
334 | */ |
||
335 | public function doRegister($data, $form) |
||
382 | } |
||
383 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.