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 TestCase 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 TestCase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class TestCase extends PHPUnit_Framework_TestCase |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var \Buzz\Client\ClientInterface |
||
| 22 | */ |
||
| 23 | private $httpClient; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $httpHost; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | * Basic auth login:password |
||
| 33 | */ |
||
| 34 | private $httpAuth; |
||
| 35 | |||
| 36 | protected static $testSuffix; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private $headers = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The username to use for login. |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $loginUsername; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The password to use for login. |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $loginPassword; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * If true, a login request is automatically done during setUp(). |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | protected $autoLogin = true; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * List of REST contentId (/content/objects/12345) created by tests. |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private static $createdContent = array(); |
||
| 67 | |||
| 68 | protected function setUp() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @return HttpResponse |
||
| 90 | */ |
||
| 91 | public function sendHttpRequest(HttpRequest $request) |
||
| 98 | |||
| 99 | protected function getHttpHost() |
||
| 103 | |||
| 104 | protected function getLoginUsername() |
||
| 108 | |||
| 109 | protected function getLoginPassword() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return HttpRequest |
||
| 116 | */ |
||
| 117 | public function createHttpRequest($method, $uri, $contentType = '', $acceptType = '') |
||
| 139 | |||
| 140 | protected function assertHttpResponseCodeEquals(HttpResponse $response, $expected) |
||
| 156 | |||
| 157 | private function getHttpResponseCodeErrorMessage($errorMessage) |
||
| 175 | |||
| 176 | protected function assertHttpResponseHasHeader(HttpResponse $response, $header, $expectedValue = null) |
||
| 184 | |||
| 185 | protected function generateMediaTypeString($typeString) |
||
| 189 | |||
| 190 | protected function addCreatedElement($href) |
||
| 199 | |||
| 200 | public static function tearDownAfterClass() |
||
| 204 | |||
| 205 | private static function clearCreatedElement(array $contentArray) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $parentLocationId The REST id of the parent location |
||
| 214 | * |
||
| 215 | * @return array created Content, as an array |
||
| 216 | */ |
||
| 217 | protected function createFolder($string, $parentLocationId) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param $xml |
||
| 252 | * |
||
| 253 | * @return array Content key of the Content struct array |
||
| 254 | */ |
||
| 255 | protected function createContent($xml) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $contentHref |
||
| 283 | * |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | View Code Duplication | protected function getContentLocations($contentHref) |
|
| 296 | |||
| 297 | protected function addTestSuffix($string) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Sends a login request to the REST server. |
||
| 308 | * |
||
| 309 | * @return \stdClass an object with the name, identifier, csrftoken properties. |
||
| 310 | */ |
||
| 311 | View Code Duplication | protected function login() |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Sets the request's content to a JSON session creation payload. |
||
| 323 | * |
||
| 324 | * @param HttpRequest $request |
||
| 325 | * @param string $password The password to use in the input. Will use the default one if not set. |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | protected function setSessionInput(HttpRequest $request, $password = null) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Asserts that $response has the given set of $cacheTags. |
||
| 338 | * |
||
| 339 | * @param Response $response |
||
| 340 | * @param array $expectedTags Example: ['content-42', 'location-300'] |
||
| 341 | */ |
||
| 342 | protected function assertHttpResponseHasCacheTags(Response $response, $expectedTags) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Extracts and returns the last id from $href. |
||
| 354 | * |
||
| 355 | * @param string $href Ex: '/api/ezp/v2/content/objects/1' |
||
| 356 | * @return int Ex: 1 |
||
| 357 | */ |
||
| 358 | protected function extractLastIdFromHref($href) |
||
| 364 | |||
| 365 | protected function extractPathFromHref($href) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Extracts a content id from any href containing one. |
||
| 379 | * |
||
| 380 | * @param string $href Ex: /api/ezp/v2/content/objects/1/anything |
||
| 381 | * @return int |
||
| 382 | */ |
||
| 383 | protected function extractContentIdFromHref($href) |
||
| 397 | } |
||
| 398 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: