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 |
||
| 19 | class Impersonator |
||
| 20 | { |
||
| 21 | public $defaultAuthClient = 'hiam'; |
||
| 22 | /** |
||
| 23 | * @var Session |
||
| 24 | */ |
||
| 25 | private $session; |
||
| 26 | /** |
||
| 27 | * @var \yii\web\User |
||
| 28 | */ |
||
| 29 | private $user; |
||
| 30 | /** |
||
| 31 | * @var Collection |
||
| 32 | */ |
||
| 33 | private $collection; |
||
| 34 | |||
| 35 | public function __construct(Session $session, \yii\web\User $user, Collection $collection) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Method should be called to generate URL for user redirect. |
||
| 44 | * |
||
| 45 | * @param string $user_id |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | public function buildAuthUrl($user_id) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @return \hiam\authclient\HiamClient $client |
||
| 58 | */ |
||
| 59 | private function getClient() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Method should be called when authentication succeeded. |
||
| 66 | * @param HiamClient $client |
||
| 67 | */ |
||
| 68 | public function impersonateUser(HiamClient $client) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Method should be called when user should be unimpersonated. |
||
| 89 | */ |
||
| 90 | public function unimpersonateUser() |
||
| 101 | |||
| 102 | protected function restoreBackedUpToken() |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Method should be called before user redirect to authentication server. |
||
| 113 | */ |
||
| 114 | public function backupCurrentToken() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | public function isUserImpersonated() |
||
| 127 | } |
||
| 128 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.