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 |
||
33 | class UserContext implements Context |
||
34 | { |
||
35 | /** |
||
36 | * @var CoreContext |
||
37 | */ |
||
38 | private $coreContext; |
||
39 | |||
40 | /** |
||
41 | * @var MinkContext |
||
42 | */ |
||
43 | private $minkContext; |
||
44 | |||
45 | /** |
||
46 | * @var User |
||
47 | */ |
||
48 | private $currentUser; |
||
49 | |||
50 | /** |
||
51 | * @var User[] |
||
52 | */ |
||
53 | static private $users = []; |
||
54 | |||
55 | /** |
||
56 | * @var UserRepository |
||
57 | */ |
||
58 | static private $userRepo; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | static private $currentSession; |
||
|
|||
64 | |||
65 | private $socialLoginInfo = []; |
||
66 | |||
67 | /** |
||
68 | * @var UserInterface |
||
69 | */ |
||
70 | private $loggedInUser; |
||
71 | |||
72 | public function __construct($parameters=[]) |
||
87 | |||
88 | /** |
||
89 | * @AfterSuite |
||
90 | * @param AfterSuiteScope $scope |
||
91 | */ |
||
92 | static public function afterSuite(AfterSuiteScope $scope) |
||
106 | |||
107 | /** |
||
108 | * @BeforeScenario |
||
109 | * @param BeforeScenarioScope $scope |
||
110 | */ |
||
111 | public function beforeScenario(BeforeScenarioScope $scope) |
||
117 | |||
118 | /** |
||
119 | * @return User |
||
120 | */ |
||
121 | public function getCurrentUser() |
||
125 | |||
126 | /** |
||
127 | * @When I fill in login form with :provider user |
||
128 | */ |
||
129 | public function iSignInWithSocialUser($provider) |
||
137 | |||
138 | /** |
||
139 | * @Given I am logged in as a recruiter |
||
140 | * @Given I am logged in as a recruiter with :organization as organization |
||
141 | */ |
||
142 | public function iAmLoggedInAsARecruiter($organization=null) |
||
152 | |||
153 | /** |
||
154 | * @Given I don't have :login user |
||
155 | * @param string $login |
||
156 | */ |
||
157 | public function iDonTHaveUser($login) |
||
165 | |||
166 | /** |
||
167 | * @Given I have a :role with the following: |
||
168 | * @param $role |
||
169 | * @param TableNode $fields |
||
170 | */ |
||
171 | public function iHaveUserWithTheFollowing($role,TableNode $fields) |
||
194 | |||
195 | /** |
||
196 | * @Given I am logged in as an administrator |
||
197 | */ |
||
198 | public function iAmLoggedInAsAnAdmin() |
||
203 | |||
204 | private function startLogin(UserInterface $user, $password) |
||
215 | |||
216 | /** |
||
217 | * @return UserRepository |
||
218 | */ |
||
219 | public function getUserRepository() |
||
223 | |||
224 | /** |
||
225 | * @Given there is a user :email identified by :password |
||
226 | */ |
||
227 | 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) |
||
307 | |||
308 | /** |
||
309 | * @When I want to log in |
||
310 | */ |
||
311 | public function iWantToLogIn() |
||
317 | |||
318 | /** |
||
319 | * @When I specify the username as :username |
||
320 | */ |
||
321 | public function iSpecifyTheUsernameAs($username) |
||
325 | |||
326 | /** |
||
327 | * @When I specify the password as :password |
||
328 | */ |
||
329 | public function iSpecifyThePasswordAs($password) |
||
333 | |||
334 | /** |
||
335 | * @Given I am logged in as :username identified by :password |
||
336 | */ |
||
337 | public function iAmLoggedInAsIdentifiedBy($username, $password) |
||
351 | |||
352 | /** |
||
353 | * @When I log in |
||
354 | */ |
||
355 | public function iLogIn() |
||
359 | |||
360 | /** |
||
361 | * @When I press logout link |
||
362 | */ |
||
363 | public function iPressLogoutLink() |
||
369 | |||
370 | /** |
||
371 | * @Given I log in with username :username and password :password |
||
372 | */ |
||
373 | public function iLogInWith($username, $password) |
||
383 | |||
384 | /** |
||
385 | * @When I go to profile page |
||
386 | */ |
||
387 | public function iGoToProfilePage() |
||
392 | |||
393 | /** |
||
394 | * @Given there is a user with the following: |
||
395 | */ |
||
396 | public function thereIsAUserWithTheFollowing(TableNode $table) |
||
411 | |||
412 | private function addCreatedUser(UserInterface $user) |
||
418 | |||
419 | /** |
||
420 | * @When I want to change my password |
||
421 | */ |
||
422 | public function iWantToChangeMyPassword() |
||
428 | |||
429 | } |
This check marks private properties in classes that are never used. Those properties can be removed.