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:
Complex classes like SecurityComponent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SecurityComponent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class SecurityComponent extends Component{ |
||
|
|||
13 | |||
14 | /** |
||
15 | * Default configurations |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $config = [ |
||
20 | 'form' => [], |
||
21 | 'requireSecure' => [], |
||
22 | 'requirePost' => [], |
||
23 | 'requireAjax' => [], |
||
24 | 'requireGet' => [], |
||
25 | 'validateForm' => true, |
||
26 | 'validateCsrfToken' => false |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * Auth startup |
||
31 | * All security checking are done in this method |
||
32 | * |
||
33 | */ |
||
34 | public function startup(){ |
||
60 | |||
61 | /** |
||
62 | * Check & validate from the required HTTP methods, like: Post, Ajax, Get |
||
63 | * |
||
64 | * @return bool |
||
65 | */ |
||
66 | private function requestRequired(){ |
||
79 | |||
80 | /** |
||
81 | * Check & validate if secured connection is required. |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | private function secureRequired(){ |
||
96 | |||
97 | /** |
||
98 | * Check & validate if request is coming from the same domain; if equals to $this->request->host() |
||
99 | * HTTP referer tells the domain where the request came from. |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | private function validateDomain(){ |
||
124 | |||
125 | /** |
||
126 | * Handles invalid request with a 400 Bad Request Error If no callback is specified. |
||
127 | * |
||
128 | * @param string|null $callback |
||
129 | * @return mixed |
||
130 | * @throws Exception |
||
131 | */ |
||
132 | private function invalidRequest($callback = null){ |
||
138 | |||
139 | /** |
||
140 | * Sets the actions that require secured connection(SSL) |
||
141 | * |
||
142 | * @param array $actions |
||
143 | */ |
||
144 | public function requireSecure($actions = []){ |
||
147 | |||
148 | /** |
||
149 | * Sets the actions that require a POST request |
||
150 | * |
||
151 | * @param array $actions |
||
152 | */ |
||
153 | public function requirePost($actions = []){ |
||
156 | |||
157 | /** |
||
158 | * Sets the actions that require a Ajax request |
||
159 | * |
||
160 | * @param array $actions |
||
161 | */ |
||
162 | public function requireAjax($actions = []){ |
||
165 | |||
166 | /** |
||
167 | * Sets the actions that require a GET request |
||
168 | * |
||
169 | * @param array $actions |
||
170 | */ |
||
171 | public function requireGet($actions = []){ |
||
174 | |||
175 | /** |
||
176 | * validate submitted form |
||
177 | * - Unknown fields cannot be added to the form. |
||
178 | * - Fields cannot be removed from the form. |
||
179 | * |
||
180 | * Use $exclude to exclude anything mightn't be sent with the form, like possible empty arrays, checkboxes, radio buttons, ...etc. |
||
181 | * By default, the submit field will be excluded. |
||
182 | * |
||
183 | * @param array $config configuration data |
||
184 | * @return boolean |
||
185 | */ |
||
186 | public function form($config){ |
||
218 | |||
219 | /** |
||
220 | * validate CSRF token |
||
221 | * CSRF token can be passed with submitted forms and links associated with sensitive server-side operations. |
||
222 | * |
||
223 | * In case of GET request, you need to set 'validateCsrfToken' in $config to true. |
||
224 | * |
||
225 | * @param array $config configuration data |
||
226 | * @return boolean |
||
227 | */ |
||
228 | public function CsrfToken($config = []){ |
||
244 | |||
245 | } |
||
246 |
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.