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 |
||
| 17 | class AuthenticatorService implements ServiceInterface |
||
| 18 | { |
||
| 19 | |||
| 20 | /** @var AbstractController */ |
||
| 21 | protected $controller; |
||
| 22 | |||
| 23 | /** @var DbService */ |
||
| 24 | protected $orm; |
||
| 25 | |||
| 26 | /** @var Config */ |
||
| 27 | protected $config; |
||
| 28 | |||
| 29 | /** @var string */ |
||
| 30 | protected $redirectAfterAuth; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Authenticator constructor. |
||
| 34 | * @param AbstractController $controller |
||
| 35 | * @param Config $config |
||
| 36 | */ |
||
| 37 | public function __construct(AbstractController $controller, Config $config) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param Entity $user |
||
| 45 | * @param bool $shouldBeActive |
||
| 46 | * @param string $redirectUrl |
||
| 47 | * @return bool |
||
| 48 | * @codeCoverageIgnore |
||
| 49 | */ |
||
| 50 | public function loginUser(Entity $user, $shouldBeActive = true, $redirectUrl = '') |
||
| 51 | { |
||
| 52 | /** @var Entity $userData */ |
||
| 53 | $userData = $this->controller |
||
|
|
|||
| 54 | ->getDb() |
||
| 55 | ->fetch(get_class($user)) |
||
| 56 | ->where('login', '=', $user->login) |
||
| 57 | ->orWhere('email', '=', $user->login) |
||
| 58 | ->one(); |
||
| 59 | |||
| 60 | if (empty($userData)) { |
||
| 61 | $this->controller->setFlashMessage('error.login', 'invalid_username_or_password'); |
||
| 62 | return $this->redirectToAuthentication(); |
||
| 63 | } |
||
| 64 | |||
| 65 | if ($shouldBeActive && $userData->active !== 1) { |
||
| 66 | $this->controller->setFlashMessage('error.active', 'user_is_not_activated'); |
||
| 67 | return $this->redirectToAuthentication(); |
||
| 68 | } |
||
| 69 | |||
| 70 | $passOk = Crypt::verifyPassword($user->password, $userData->password); |
||
| 71 | |||
| 72 | if ($passOk && $userData instanceof Entity) { |
||
| 73 | |||
| 74 | $this->saveUserInSession($userData); |
||
| 75 | |||
| 76 | if ($redirectUrl) { |
||
| 77 | return $this->controller->redirect($redirectUrl); |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($userData->roles[0]->roleName === 'registered') { |
||
| 81 | return $this->controller->redirect($this->controller->route('user')); |
||
| 82 | } else { |
||
| 83 | return $this->controller->redirect($this->controller->route('admin')); |
||
| 84 | } |
||
| 85 | |||
| 86 | } |
||
| 87 | |||
| 88 | $this->controller->setFlashMessage('error.login', 'invalid_username_or_password'); |
||
| 89 | |||
| 90 | return $this->redirectToAuthentication(); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function redirectToAccessDeniedPage() |
|
| 97 | { |
||
| 98 | /** @var Config $config */ |
||
| 99 | $config = $this->controller->getServiceLocator()->get(Config::class); |
||
| 100 | $authUrl = $config->get('auth:authUrl'); |
||
| 101 | |||
| 102 | return $this->controller->redirect($authUrl); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | View Code Duplication | public function redirectToAuthentication() |
|
| 109 | { |
||
| 110 | /** @var Config $config */ |
||
| 111 | $config = $this->controller->getServiceLocator()->get(Config::class); |
||
| 112 | $authUrl = $config->get('auth:authUrl'); |
||
| 113 | |||
| 114 | return $this->controller->redirect($authUrl); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param array $roles |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | public function isPermitted(array $roles) |
||
| 122 | { |
||
| 123 | /** @var Entity $user */ |
||
| 124 | $user = $this->getUserFromSession(); |
||
| 125 | |||
| 126 | if (!$user instanceof Entity) { |
||
| 127 | return null; |
||
| 128 | } |
||
| 129 | |||
| 130 | foreach ($user->roles as $userRole) { |
||
| 131 | |||
| 132 | if (in_array($userRole->roleName, $roles, true)) { |
||
| 133 | return true; |
||
| 134 | } |
||
| 135 | |||
| 136 | } |
||
| 137 | |||
| 138 | return false; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param Entity $user |
||
| 143 | * @codeCoverageIgnore |
||
| 144 | */ |
||
| 145 | public function saveUserInSession(Entity $user) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param string $entity |
||
| 152 | * @return Entity |
||
| 153 | * @codeCoverageIgnore |
||
| 154 | */ |
||
| 155 | public function getUserFromSession(string $entity = '') |
||
| 172 | |||
| 173 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: