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 UserContext 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 UserContext, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class UserContext implements Context |
||
| 31 | { |
||
| 32 | use CommonContextTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var User[] |
||
| 36 | */ |
||
| 37 | static private $users = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var UserRepository |
||
| 41 | */ |
||
| 42 | static private $userRepo; |
||
| 43 | |||
| 44 | private $socialLoginInfo = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var DocumentManager |
||
| 48 | */ |
||
| 49 | static private $dm; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var UserInterface |
||
| 53 | */ |
||
| 54 | private $loggedInUser; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Organization |
||
| 58 | */ |
||
| 59 | private $mainOrganization; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var User |
||
| 63 | */ |
||
| 64 | protected $currentUser; |
||
| 65 | |||
| 66 | public function __construct($parameters=[]) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Empty all data every each tests |
||
| 84 | * |
||
| 85 | * @AfterSuite |
||
| 86 | */ |
||
| 87 | static public function tearDown() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @BeforeScenario |
||
| 112 | * @param BeforeScenarioScope $scope |
||
| 113 | */ |
||
| 114 | public function beforeScenario(BeforeScenarioScope $scope) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @When I fill in login form with :provider user |
||
| 124 | */ |
||
| 125 | public function iSignInWithSocialUser($provider) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @Given I am logged in as a recruiter |
||
| 136 | * @Given I am logged in as a recruiter with :organization as organization |
||
| 137 | */ |
||
| 138 | public function iAmLoggedInAsARecruiter($organization=null) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @Given I am logged out |
||
| 154 | */ |
||
| 155 | public function iHaveLoggedOut() |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * @Given I don't have :login user |
||
| 164 | * @param string $login |
||
| 165 | */ |
||
| 166 | public function iDonTHaveUser($login) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @Given I have a :role with the following: |
||
| 177 | * @Given I have an :role with the following: |
||
| 178 | * |
||
| 179 | * @param $role |
||
| 180 | * @param TableNode $fields |
||
| 181 | */ |
||
| 182 | public function iHaveUserWithTheFollowing($role,TableNode $fields) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @Given I am logged in as an administrator |
||
| 208 | */ |
||
| 209 | public function iAmLoggedInAsAnAdmin() |
||
| 214 | |||
| 215 | private function startLogin(UserInterface $user, $password) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return UserRepository |
||
| 229 | */ |
||
| 230 | public function getUserRepository() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @Given there is a user :email identified by :password |
||
| 237 | */ |
||
| 238 | public function thereIsAUserIdentifiedBy($email, $password,$role=User::ROLE_RECRUITER,$fullname="Test Recruiter",$organization=null) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param $email |
||
| 255 | * @param $password |
||
| 256 | * @param $username |
||
| 257 | * @param string $fullname |
||
| 258 | * @param string $role |
||
| 259 | * |
||
| 260 | * @return \Auth\Entity\UserInterface |
||
| 261 | */ |
||
| 262 | public function createUser($email,$password,$role=User::ROLE_RECRUITER,$fullname="Test Recruiter") |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @When I have :organization as my main organization |
||
| 299 | * @param $orgName |
||
| 300 | */ |
||
| 301 | public function iHaveMainOrganization(UserInterface $user,$orgName) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @return Organization |
||
| 325 | */ |
||
| 326 | public function getMainOrganization() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @When I want to log in |
||
| 333 | */ |
||
| 334 | public function iWantToLogIn() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @When I specify the username as :username |
||
| 343 | */ |
||
| 344 | public function iSpecifyTheUsernameAs($username) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @When I specify the password as :password |
||
| 351 | */ |
||
| 352 | public function iSpecifyThePasswordAs($password) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @Given I am logged in as :username identified by :password |
||
| 359 | */ |
||
| 360 | public function iAmLoggedInAsIdentifiedBy($username, $password) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @When I log in |
||
| 377 | */ |
||
| 378 | public function iLogIn() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @When I press logout link |
||
| 385 | */ |
||
| 386 | public function iPressLogoutLink() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @Given I log in with username :username and password :password |
||
| 394 | */ |
||
| 395 | public function iLogInWith($username, $password) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @When I go to profile page |
||
| 408 | */ |
||
| 409 | public function iGoToProfilePage() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @Given there is a user with the following: |
||
| 417 | */ |
||
| 418 | public function thereIsAUserWithTheFollowing(TableNode $table) |
||
| 433 | |||
| 434 | private function addCreatedUser(UserInterface $user) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @When I want to change my password |
||
| 443 | */ |
||
| 444 | public function iWantToChangeMyPassword() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @return User |
||
| 452 | * @throws FailedExpectationException |
||
| 453 | */ |
||
| 454 | public function getCurrentUser() |
||
| 461 | } |
||
| 462 | |||
| 463 |