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 AuthController 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 AuthController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | abstract class AuthController extends ControllerBase{ |
||
| 21 | use AuthControllerVariablesTrait; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var AuthFiles |
||
| 25 | */ |
||
| 26 | protected $authFiles; |
||
| 27 | protected $_controller; |
||
| 28 | protected $_action; |
||
| 29 | protected $_actionParams; |
||
| 30 | protected $_noAccessMsg; |
||
| 31 | protected $_loginCaption; |
||
| 32 | protected $_attemptsSessionKey="_attempts"; |
||
| 33 | protected $_controllerInstance; |
||
| 34 | |||
| 35 | public function __construct($instance=null){ |
||
| 46 | |||
| 47 | public function index(){ |
||
| 61 | |||
| 62 | /** |
||
| 63 | * To override |
||
| 64 | * Return the base route for this Auth controller |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function _getBaseRoute(){ |
||
| 70 | |||
| 71 | private function getBaseUrl(){ |
||
| 74 | /** |
||
| 75 | * {@inheritDoc} |
||
| 76 | * @see \controllers\ControllerBase::isValid() |
||
| 77 | */ |
||
| 78 | public final function isValid($action) { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Action called when the user does not have access rights to a requested resource |
||
| 84 | * @param array|string $urlParts |
||
| 85 | */ |
||
| 86 | public function noAccess($urlParts){ |
||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | /** |
||
| 104 | * Override to implement the complete connection procedure |
||
| 105 | */ |
||
| 106 | public function connect(){ |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Processes the data posted by the login form |
||
| 124 | * Have to return the connected user instance |
||
| 125 | */ |
||
| 126 | abstract protected function _connect(); |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param object $connected |
||
| 130 | */ |
||
| 131 | abstract protected function onConnect($connected); |
||
| 132 | |||
| 133 | /** |
||
| 134 | * To override for defining a new action when creditentials are invalid |
||
| 135 | */ |
||
| 136 | protected function onBadCreditentials(){ |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Default Action for invalid creditentials |
||
| 142 | */ |
||
| 143 | public function badLogin(){ |
||
| 165 | |||
| 166 | protected function noAttempts(){ |
||
| 187 | |||
| 188 | |||
| 189 | private function authLoadView($viewName,$vars=[]){ |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Logout action |
||
| 203 | * Terminate the session and display a logout message |
||
| 204 | */ |
||
| 205 | public function terminate(){ |
||
| 212 | |||
| 213 | public function _disConnected(){ |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Action displaying the logged user information |
||
| 224 | * if _displayInfoAsString returns true, use _infoUser var in views to display user info |
||
| 225 | * @return string|null |
||
| 226 | */ |
||
| 227 | public function info($force=null){ |
||
| 235 | |||
| 236 | protected function fMessage(FlashMessage $fMessage,$id=null){ |
||
| 239 | |||
| 240 | public function message($type,$header,$body,$icon="info",$id=null){ |
||
| 243 | |||
| 244 | protected function getOriginalURL(){ |
||
| 247 | |||
| 248 | /** |
||
| 249 | * To override for defining user session key, default : "activeUser" |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public function _getUserSessionKey(){ |
||
| 255 | |||
| 256 | /** |
||
| 257 | * To override for getting active user, default : USession::get("activeUser") |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | public function _getActiveUser(){ |
||
| 263 | |||
| 264 | public function checkConnection(){ |
||
| 268 | |||
| 269 | /** |
||
| 270 | * return boolean true if activeUser is valid |
||
| 271 | */ |
||
| 272 | abstract public function _isValidUser(); |
||
| 273 | |||
| 274 | /** |
||
| 275 | * To override for changing view files |
||
| 276 | * @return AuthFiles |
||
| 277 | */ |
||
| 278 | protected function getFiles ():AuthFiles{ |
||
| 281 | |||
| 282 | private function _getFiles():AuthFiles{ |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Sets the default noAccess message |
||
| 291 | * Default : "You are not authorized to access the page <b>{url}</b> !" |
||
| 292 | * @param string $content |
||
| 293 | * @param string $title |
||
| 294 | * @param string $type |
||
| 295 | * @param string $icon |
||
| 296 | */ |
||
| 297 | public function _setNoAccessMsg($content,$title=NULL,$type=NULL,$icon=null) { |
||
| 300 | /** |
||
| 301 | * @param string $_loginCaption |
||
| 302 | */ |
||
| 303 | public function _setLoginCaption($_loginCaption) { |
||
| 306 | |||
| 307 | protected function getViewVars($viewname){ |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Saves the connected user identifier in a cookie |
||
| 313 | * @param object $connected |
||
| 314 | */ |
||
| 315 | protected function rememberMe($connected){ |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns the cookie for auto connection |
||
| 324 | * @return NULL|string |
||
| 325 | */ |
||
| 326 | protected function getCookieUser(){ |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Returns the value from connected user to save it in the cookie for auto connection |
||
| 332 | * @param object $connected |
||
| 333 | */ |
||
| 334 | protected function toCookie($connected){ |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Loads the user from database using the cookie value |
||
| 340 | * @param string $cookie |
||
| 341 | */ |
||
| 342 | protected function fromCookie($cookie){ |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Auto connect the user |
||
| 348 | */ |
||
| 349 | public function _autoConnect() { |
||
| 358 | /** |
||
| 359 | * Deletes the cookie for auto connection and returns to index |
||
| 360 | */ |
||
| 361 | public function forgetConnection(){ |
||
| 365 | /** |
||
| 366 | * {@inheritDoc} |
||
| 367 | * @see \Ubiquity\controllers\ControllerBase::finalize() |
||
| 368 | */ |
||
| 369 | public function finalize() { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * {@inheritDoc} |
||
| 385 | * @see \Ubiquity\controllers\ControllerBase::initialize() |
||
| 386 | */ |
||
| 387 | public function initialize() { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param string $url |
||
| 399 | */ |
||
| 400 | public function _forward($url){ |
||
| 407 | |||
| 408 | } |
||
| 409 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.