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 |
||
| 27 | trait TestingTrait |
||
| 28 | { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * the Logged in user, used for protected routes. |
||
| 32 | * |
||
| 33 | * @var User |
||
| 34 | */ |
||
| 35 | public $loggedInTestingUser; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param $endpoint |
||
| 39 | * @param string $verb |
||
| 40 | * @param array $data |
||
| 41 | * @param bool $protected |
||
| 42 | * @param array $headers |
||
| 43 | * |
||
| 44 | * @return mixed |
||
| 45 | * @throws \Symfony\Component\Debug\Exception\UndefinedMethodException |
||
| 46 | */ |
||
| 47 | public function apiCall($endpoint, $verb = 'get', array $data = [], $protected = true, array $headers = []) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param $fileName |
||
| 78 | * @param $stubDirPath |
||
| 79 | * @param string $mimeType |
||
| 80 | * @param null $size |
||
| 81 | * |
||
| 82 | * @return \Illuminate\Http\UploadedFile |
||
| 83 | */ |
||
| 84 | public function getTestingFile($fileName, $stubDirPath, $mimeType = 'text/plain', $size = null) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param $imageName |
||
| 93 | * @param $stubDirPath |
||
| 94 | * @param string $mimeType |
||
| 95 | * @param null $size |
||
| 96 | * |
||
| 97 | * @return \Illuminate\Http\UploadedFile |
||
| 98 | */ |
||
| 99 | public function getTestingImage($imageName, $stubDirPath, $mimeType = 'image/jpeg', $size = null) |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * get teh current logged in user OR create new one if no one exist |
||
| 107 | * |
||
| 108 | * @param null $access |
||
| 109 | * |
||
| 110 | * @return \App\Containers\User\Models\User|mixed |
||
| 111 | */ |
||
| 112 | public function getTestingUser($access = null) |
||
| 113 | { |
||
| 114 | if (!$user = $this->loggedInTestingUser) { |
||
| 115 | $user = $this->createTestingUser($access); |
||
| 116 | } |
||
| 117 | |||
| 118 | return $user; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @return \App\Containers\User\Models\User|mixed |
||
| 123 | */ |
||
| 124 | public function getTestingUserWithoutPermissions() |
||
| 125 | { |
||
| 126 | if (!$user = $this->loggedInTestingUser) { |
||
| 127 | $user = $this->getTestingUser(['permissions' => null, 'roles' => null]); |
||
| 128 | } |
||
| 129 | |||
| 130 | return $user; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param null $permissions |
||
| 135 | * |
||
| 136 | * @return \App\Containers\User\Models\User|mixed |
||
| 137 | */ |
||
| 138 | public function getTestingAdmin($permissions = null) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * get teh current logged in user token. |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getLoggedInTestingUserToken() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param null $access |
||
| 158 | * @param null $userDetails |
||
| 159 | * |
||
| 160 | * @return mixed |
||
| 161 | */ |
||
| 162 | public function createTestingUser($access = null, $userDetails = null) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param $user |
||
| 190 | * @param $access |
||
| 191 | * |
||
| 192 | * @return mixed |
||
| 193 | */ |
||
| 194 | private function setupTestingUserAccess($user, $access) |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * @param \Dingo\Api\Http\Response $response |
||
| 211 | * @param array $messages |
||
| 212 | */ |
||
| 213 | public function assertValidationErrorContain(DingoAPIResponse $response, array $messages) |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * @param $keys |
||
| 225 | * @param $response |
||
| 226 | */ |
||
| 227 | public function assertResponseContainKeys($keys, $response) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param $values |
||
| 240 | * @param $response |
||
| 241 | */ |
||
| 242 | public function assertResponseContainValues($values, $response) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param $data |
||
| 255 | * @param $response |
||
| 256 | */ |
||
| 257 | public function assertResponseContainKeyValue($data, $response) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Migrate the database. |
||
| 272 | */ |
||
| 273 | public function migrateDatabase() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param $response |
||
| 280 | * |
||
| 281 | * @return mixed |
||
| 282 | */ |
||
| 283 | private function responseToArray($response) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Format the given key and value into a JSON string for expectation checks. |
||
| 298 | * |
||
| 299 | * @param string $key |
||
| 300 | * @param mixed $value |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | private function formatToKeyValueToString($key, $value) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Mocking helper |
||
| 321 | * |
||
| 322 | * @param $class |
||
| 323 | * |
||
| 324 | * @return \Mockery\MockInterface |
||
| 325 | */ |
||
| 326 | public function mock($class) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * get response object, get the string content from it and convert it to an std object |
||
| 336 | * making it easier to read |
||
| 337 | * |
||
| 338 | * @param $response |
||
| 339 | * |
||
| 340 | * @return mixed |
||
| 341 | */ |
||
| 342 | public function getResponseObject(Response $response) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Inject the ID in the Endpoint URI |
||
| 349 | * |
||
| 350 | * Example: you give it ('users/{id}/stores', 100) it returns 'users/100/stores' |
||
| 351 | * |
||
| 352 | * @param $endpoint |
||
| 353 | * @param $id |
||
| 354 | * @param bool $skipEncoding |
||
| 355 | * |
||
| 356 | * @return mixed |
||
| 357 | */ |
||
| 358 | public function injectEndpointId($endpoint, $id, $skipEncoding = false) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * override default URL subDomain in case you want to change it for some tests |
||
| 373 | * |
||
| 374 | * @param $subDomain |
||
| 375 | * @param null $url |
||
| 376 | */ |
||
| 377 | public function overrideSubDomain($subDomain, $url = null) |
||
| 392 | |||
| 393 | } |
||
| 394 |
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
Idableprovides a methodequalsIdthat 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.