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 Security 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 Security, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Security { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The base Uri to construct the route |
||
| 36 | * |
||
| 37 | * @access private |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | |||
| 41 | private $_sBaseUri = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Actual user |
||
| 45 | * |
||
| 46 | * @access private |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | |||
| 50 | private static $_sLogin = ''; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Actual user |
||
| 54 | * |
||
| 55 | * @access private |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | |||
| 59 | private static $_sPassword = ''; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * check security of access |
||
| 63 | * |
||
| 64 | * @access public |
||
| 65 | * @return null|boolean |
||
| 66 | */ |
||
| 67 | |||
| 68 | public function checkSecurity() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * check access |
||
| 163 | * |
||
| 164 | * @access private |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | |||
| 168 | private function _checkAccess() : bool { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * check if the ips is not in the blacklist |
||
| 188 | * |
||
| 189 | * @access private |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | |||
| 193 | private function _checkBlackListIps() : bool { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * check if the password is good |
||
| 210 | * |
||
| 211 | * @access private |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | |||
| 215 | private function _checkPasswordIsGood() : bool { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * get the user roles |
||
| 227 | * |
||
| 228 | * @access public |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | |||
| 232 | public function getUserRole() : string { |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * get the user roles |
||
| 247 | * |
||
| 248 | * @access public |
||
| 249 | * @param string $sRole role to test |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | |||
| 253 | public function isGranted(string $sRole) : bool { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * set base uri |
||
| 260 | * |
||
| 261 | * @access public |
||
| 262 | * @param string $sBaseUri |
||
| 263 | * @return \Venus\core\Security |
||
| 264 | */ |
||
| 265 | |||
| 266 | public function setBaseUri($sBaseUri) { |
||
| 270 | } |
||
| 271 |