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 don't have :login user |
||
154 | * @param string $login |
||
155 | */ |
||
156 | public function iDonTHaveUser($login) |
||
164 | |||
165 | /** |
||
166 | * @Given I have a :role with the following: |
||
167 | * @param $role |
||
168 | * @param TableNode $fields |
||
169 | */ |
||
170 | public function iHaveUserWithTheFollowing($role,TableNode $fields) |
||
193 | |||
194 | /** |
||
195 | * @Given I am logged in as an administrator |
||
196 | */ |
||
197 | public function iAmLoggedInAsAnAdmin() |
||
202 | |||
203 | private function startLogin(UserInterface $user, $password) |
||
214 | |||
215 | /** |
||
216 | * @return UserRepository |
||
217 | */ |
||
218 | public function getUserRepository() |
||
222 | |||
223 | /** |
||
224 | * @Given there is a user :email identified by :password |
||
225 | */ |
||
226 | public function thereIsAUserIdentifiedBy($email, $password,$role=User::ROLE_RECRUITER,$fullname="Test Recruiter",$organization=null) |
||
240 | |||
241 | /** |
||
242 | * @param $email |
||
243 | * @param $password |
||
244 | * @param $username |
||
245 | * @param string $fullname |
||
246 | * @param string $role |
||
247 | * |
||
248 | * @return \Auth\Entity\UserInterface |
||
249 | */ |
||
250 | public function createUser($email,$password,$role=User::ROLE_RECRUITER,$fullname="Test Recruiter") |
||
284 | |||
285 | /** |
||
286 | * @When I have :organization as my main organization |
||
287 | * @param $orgName |
||
288 | */ |
||
289 | public function iHaveMainOrganization(UserInterface $user,$orgName) |
||
310 | |||
311 | /** |
||
312 | * @return Organization |
||
313 | */ |
||
314 | public function getMainOrganization() |
||
318 | |||
319 | |||
320 | /** |
||
321 | * @When I want to log in |
||
322 | */ |
||
323 | public function iWantToLogIn() |
||
329 | |||
330 | /** |
||
331 | * @When I specify the username as :username |
||
332 | */ |
||
333 | public function iSpecifyTheUsernameAs($username) |
||
337 | |||
338 | /** |
||
339 | * @When I specify the password as :password |
||
340 | */ |
||
341 | public function iSpecifyThePasswordAs($password) |
||
345 | |||
346 | /** |
||
347 | * @Given I am logged in as :username identified by :password |
||
348 | */ |
||
349 | public function iAmLoggedInAsIdentifiedBy($username, $password) |
||
363 | |||
364 | /** |
||
365 | * @When I log in |
||
366 | */ |
||
367 | public function iLogIn() |
||
371 | |||
372 | /** |
||
373 | * @When I press logout link |
||
374 | */ |
||
375 | public function iPressLogoutLink() |
||
380 | |||
381 | /** |
||
382 | * @Given I log in with username :username and password :password |
||
383 | */ |
||
384 | public function iLogInWith($username, $password) |
||
394 | |||
395 | /** |
||
396 | * @When I go to profile page |
||
397 | */ |
||
398 | public function iGoToProfilePage() |
||
403 | |||
404 | /** |
||
405 | * @Given there is a user with the following: |
||
406 | */ |
||
407 | public function thereIsAUserWithTheFollowing(TableNode $table) |
||
422 | |||
423 | private function addCreatedUser(UserInterface $user) |
||
429 | |||
430 | /** |
||
431 | * @When I want to change my password |
||
432 | */ |
||
433 | public function iWantToChangeMyPassword() |
||
438 | |||
439 | /** |
||
440 | * @return User |
||
441 | * @throws FailedExpectationException |
||
442 | */ |
||
443 | public function getCurrentUser() |
||
450 | } |
||
451 | |||
452 |