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 have user with the following: |
||
154 | * |
||
155 | * |
||
156 | */ |
||
157 | public function iHaveRecruiterWithOrganization(TableNode $tableNode) |
||
169 | |||
170 | /** |
||
171 | * @Given I am logged out |
||
172 | */ |
||
173 | public function iHaveLoggedOut() |
||
178 | |||
179 | |||
180 | /** |
||
181 | * @Given I don't have :login user |
||
182 | * @param string $login |
||
183 | */ |
||
184 | public function iDonTHaveUser($login) |
||
192 | |||
193 | /** |
||
194 | * @Given I have a :role with the following: |
||
195 | * @param $role |
||
196 | * @param TableNode $fields |
||
197 | */ |
||
198 | public function iHaveUserWithTheFollowing($role,TableNode $fields) |
||
221 | |||
222 | /** |
||
223 | * @Given I am logged in as an administrator |
||
224 | */ |
||
225 | public function iAmLoggedInAsAnAdmin() |
||
230 | |||
231 | private function startLogin(UserInterface $user, $password) |
||
242 | |||
243 | /** |
||
244 | * @return UserRepository |
||
245 | */ |
||
246 | public function getUserRepository() |
||
250 | |||
251 | /** |
||
252 | * @Given there is a user :email identified by :password |
||
253 | */ |
||
254 | public function thereIsAUserIdentifiedBy($email, $password,$role=User::ROLE_RECRUITER,$fullname="Test Recruiter",$organization=null) |
||
268 | |||
269 | /** |
||
270 | * @param $email |
||
271 | * @param $password |
||
272 | * @param $username |
||
273 | * @param string $fullname |
||
274 | * @param string $role |
||
275 | * |
||
276 | * @return \Auth\Entity\UserInterface |
||
277 | */ |
||
278 | public function createUser($email,$password,$role=User::ROLE_RECRUITER,$fullname="Test Recruiter") |
||
312 | |||
313 | /** |
||
314 | * @When I have :organization as my main organization |
||
315 | * @param $orgName |
||
316 | */ |
||
317 | public function iHaveMainOrganization(UserInterface $user,$orgName) |
||
338 | |||
339 | /** |
||
340 | * @return Organization |
||
341 | */ |
||
342 | public function getMainOrganization() |
||
346 | |||
347 | /** |
||
348 | * @When I want to log in |
||
349 | */ |
||
350 | public function iWantToLogIn() |
||
356 | |||
357 | /** |
||
358 | * @When I specify the username as :username |
||
359 | */ |
||
360 | public function iSpecifyTheUsernameAs($username) |
||
364 | |||
365 | /** |
||
366 | * @When I specify the password as :password |
||
367 | */ |
||
368 | public function iSpecifyThePasswordAs($password) |
||
372 | |||
373 | /** |
||
374 | * @Given I am logged in as :username identified by :password |
||
375 | */ |
||
376 | public function iAmLoggedInAsIdentifiedBy($username, $password) |
||
390 | |||
391 | /** |
||
392 | * @When I log in |
||
393 | */ |
||
394 | public function iLogIn() |
||
398 | |||
399 | /** |
||
400 | * @When I press logout link |
||
401 | */ |
||
402 | public function iPressLogoutLink() |
||
407 | |||
408 | /** |
||
409 | * @Given I log in with username :username and password :password |
||
410 | */ |
||
411 | public function iLogInWith($username, $password) |
||
421 | |||
422 | /** |
||
423 | * @When I go to profile page |
||
424 | */ |
||
425 | public function iGoToProfilePage() |
||
430 | |||
431 | /** |
||
432 | * @Given there is a user with the following: |
||
433 | */ |
||
434 | public function thereIsAUserWithTheFollowing(TableNode $table) |
||
449 | |||
450 | private function addCreatedUser(UserInterface $user) |
||
456 | |||
457 | /** |
||
458 | * @When I want to change my password |
||
459 | */ |
||
460 | public function iWantToChangeMyPassword() |
||
465 | |||
466 | /** |
||
467 | * @return User |
||
468 | * @throws FailedExpectationException |
||
469 | */ |
||
470 | public function getCurrentUser() |
||
477 | } |
||
478 | |||
479 |