Complex classes like TestingTrait 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 TestingTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | trait TestingTrait |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * the Logged in user, used for protected routes. |
||
34 | * |
||
35 | * @var User |
||
36 | */ |
||
37 | public $loggedInTestingUser; |
||
38 | |||
39 | /** |
||
40 | * @param $endpoint |
||
41 | * @param string $verb |
||
42 | * @param array $data |
||
43 | * @param bool $protected |
||
44 | * @param array $headers |
||
45 | * |
||
46 | * @return mixed |
||
47 | * @throws \Symfony\Component\Debug\Exception\UndefinedMethodException |
||
48 | */ |
||
49 | public function apiCall($endpoint, $verb = 'get', array $data = [], $protected = true, array $headers = []) |
||
77 | |||
78 | /** |
||
79 | * @param $fileName |
||
80 | * @param $stubDirPath |
||
81 | * @param string $mimeType |
||
82 | * @param null $size |
||
83 | * |
||
84 | * @return \Illuminate\Http\UploadedFile |
||
85 | */ |
||
86 | public function getTestingFile($fileName, $stubDirPath, $mimeType = 'text/plain', $size = null) |
||
92 | |||
93 | /** |
||
94 | * @param $imageName |
||
95 | * @param $stubDirPath |
||
96 | * @param string $mimeType |
||
97 | * @param null $size |
||
98 | * |
||
99 | * @return \Illuminate\Http\UploadedFile |
||
100 | */ |
||
101 | public function getTestingImage($imageName, $stubDirPath, $mimeType = 'image/jpeg', $size = null) |
||
105 | |||
106 | /** |
||
107 | * @param \Dingo\Api\Http\Response $response |
||
108 | * @param array $messages |
||
109 | */ |
||
110 | public function assertValidationErrorContain(DingoAPIResponse $response, array $messages) |
||
118 | |||
119 | /** |
||
120 | * get teh current logged in user. |
||
121 | * |
||
122 | * @return App\Containers\User\Models\User |
||
123 | */ |
||
124 | public function getLoggedInTestingUser() |
||
134 | |||
135 | /** |
||
136 | * @return App\Containers\User\Models\User|mixed |
||
137 | */ |
||
138 | public function getLoggedInTestingAdmin() |
||
146 | |||
147 | /** |
||
148 | * @param $user |
||
149 | * |
||
150 | * @return App\Containers\User\Models\User |
||
151 | */ |
||
152 | public function makeAdmin(User $user) |
||
160 | |||
161 | /** |
||
162 | * get teh current logged in user token. |
||
163 | * |
||
164 | * @return string |
||
165 | */ |
||
166 | public function getLoggedInTestingUserToken() |
||
170 | |||
171 | /** |
||
172 | * @param null $userDetails |
||
173 | * |
||
174 | * @return mixed |
||
175 | */ |
||
176 | public function registerAndLoginTestingUser($userDetails = null) |
||
201 | |||
202 | /** |
||
203 | * @param null $userDetails |
||
204 | * |
||
205 | * @return mixed |
||
206 | */ |
||
207 | public function registerAndLoginTestingAdmin($userDetails = null) |
||
215 | |||
216 | /** |
||
217 | * Normal user with Developer Role |
||
218 | * |
||
219 | * @param null $userDetails |
||
220 | * |
||
221 | * @return mixed |
||
222 | */ |
||
223 | public function registerAndLoginTestingDeveloper($userDetails = null) |
||
234 | |||
235 | /** |
||
236 | * @param $keys |
||
237 | * @param $response |
||
238 | */ |
||
239 | public function assertResponseContainKeys($keys, $response) |
||
249 | |||
250 | /** |
||
251 | * @param $values |
||
252 | * @param $response |
||
253 | */ |
||
254 | public function assertResponseContainValues($values, $response) |
||
264 | |||
265 | /** |
||
266 | * @param $data |
||
267 | * @param $response |
||
268 | */ |
||
269 | public function assertResponseContainKeyValue($data, $response) |
||
281 | |||
282 | /** |
||
283 | * Migrate the database. |
||
284 | */ |
||
285 | public function migrateDatabase() |
||
289 | |||
290 | /** |
||
291 | * @param $response |
||
292 | * |
||
293 | * @return mixed |
||
294 | */ |
||
295 | private function responseToArray($response) |
||
307 | |||
308 | /** |
||
309 | * Format the given key and value into a JSON string for expectation checks. |
||
310 | * |
||
311 | * @param string $key |
||
312 | * @param mixed $value |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | private function formatToKeyValueToString($key, $value) |
||
330 | |||
331 | /** |
||
332 | * Mocking helper |
||
333 | * |
||
334 | * @param $class |
||
335 | * |
||
336 | * @return \Mockery\MockInterface |
||
337 | */ |
||
338 | public function mock($class) |
||
345 | |||
346 | /** |
||
347 | * get response object, get the string content from it and convert it to an std object |
||
348 | * making it easier to read |
||
349 | * |
||
350 | * @param $response |
||
351 | * |
||
352 | * @return mixed |
||
353 | */ |
||
354 | public function getResponseObject(Response $response) |
||
358 | |||
359 | /** |
||
360 | * Inject the ID in the Endpoint URI |
||
361 | * |
||
362 | * Example: you give it ('users/{id}/stores', 100) it returns 'users/100/stores' |
||
363 | * |
||
364 | * @param $endpoint |
||
365 | * @param $id |
||
366 | * @param bool $skipEncoding |
||
367 | * |
||
368 | * @return mixed |
||
369 | */ |
||
370 | public function injectEndpointId($endpoint, $id, $skipEncoding = false) |
||
382 | |||
383 | /** |
||
384 | * override default URL subDomain in case you want to change it for some tests |
||
385 | * |
||
386 | * @param $subDomain |
||
387 | * @param null $url |
||
388 | */ |
||
389 | public function overrideSubDomain($subDomain, $url = null) |
||
404 | |||
405 | } |
||
406 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.