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 WebTestCase 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 WebTestCase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | abstract class WebTestCase extends BaseWebTestCase |
||
| 29 | { |
||
| 30 | /** Annotation names */ |
||
| 31 | const DB_ISOLATION_ANNOTATION = 'dbIsolation'; |
||
| 32 | const DB_REINDEX_ANNOTATION = 'dbReindex'; |
||
| 33 | |||
| 34 | /** Default WSSE credentials */ |
||
| 35 | const USER_NAME = 'admin'; |
||
| 36 | const USER_PASSWORD = 'admin_api_key'; |
||
| 37 | |||
| 38 | /** Default user name and password */ |
||
| 39 | const AUTH_USER = '[email protected]'; |
||
| 40 | const AUTH_PW = 'admin'; |
||
| 41 | const AUTH_ORGANIZATION = 1; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var bool[] |
||
| 45 | */ |
||
| 46 | private static $dbIsolation; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var bool[] |
||
| 50 | */ |
||
| 51 | private static $dbReindex; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var Client |
||
| 55 | */ |
||
| 56 | private static $clientInstance; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Client |
||
| 60 | */ |
||
| 61 | private static $soapClientInstance; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var Client |
||
| 65 | */ |
||
| 66 | protected $client; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var SoapClient |
||
| 70 | */ |
||
| 71 | protected $soapClient; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | private static $loadedFixtures = []; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var Connection[] |
||
| 80 | */ |
||
| 81 | private static $connections = []; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var ReferenceRepository |
||
| 85 | */ |
||
| 86 | private static $referenceRepository; |
||
| 87 | |||
| 88 | protected function tearDown() |
||
| 111 | |||
| 112 | public static function tearDownAfterClass() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Closes opened DB connections to avoid exceeding the `max_connections` limitation. |
||
| 129 | */ |
||
| 130 | public static function cleanUpConnections() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Creates a Client. |
||
| 141 | * |
||
| 142 | * @param array $options An array of options to pass to the createKernel class |
||
| 143 | * @param array $server An array of server parameters |
||
| 144 | * @param bool $force If this option - true, will reset client on each initClient call |
||
| 145 | * |
||
| 146 | * @return Client A Client instance |
||
| 147 | */ |
||
| 148 | protected function initClient(array $options = [], array $server = [], $force = false) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Reset client and rollback transaction |
||
| 182 | */ |
||
| 183 | protected function resetClient() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | protected static function getKernelClass() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get value of dbIsolation option from annotation of called class |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | View Code Duplication | private static function getDbIsolationSetting() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Get value of dbIsolation option from annotation of called class |
||
| 236 | * |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | View Code Duplication | private static function getDbReindexSetting() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $className |
||
| 251 | * @param string $annotationName |
||
| 252 | * |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | private static function isClassHasAnnotation($className, $annotationName) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $wsdl |
||
| 263 | * @param array $options |
||
| 264 | * @param bool $force |
||
| 265 | * |
||
| 266 | * @return SoapClient |
||
| 267 | * @throws \Exception |
||
| 268 | */ |
||
| 269 | protected function initSoapClient($wsdl = null, array $options = [], $force = false) |
||
| 270 | { |
||
| 271 | if (!self::$soapClientInstance || $force) { |
||
| 272 | if ($wsdl === null) { |
||
| 273 | $wsdl = "http://localhost/api/soap"; |
||
| 274 | } |
||
| 275 | |||
| 276 | $options = array_merge( |
||
| 277 | [ |
||
| 278 | 'location' => $wsdl, |
||
| 279 | 'soap_version' => SOAP_1_2 |
||
| 280 | ], |
||
| 281 | $options |
||
| 282 | ); |
||
| 283 | |||
| 284 | $client = $this->getClientInstance(); |
||
| 285 | if ($options['soap_version'] == SOAP_1_2) { |
||
| 286 | $contentType = 'application/soap+xml'; |
||
| 287 | } else { |
||
| 288 | $contentType = 'text/xml'; |
||
| 289 | } |
||
| 290 | $client->request('GET', $wsdl, [], [], ['CONTENT_TYPE' => $contentType]); |
||
| 291 | $status = $client->getResponse()->getStatusCode(); |
||
| 292 | $wsdl = $client->getResponse()->getContent(); |
||
| 293 | if ($status >= 400) { |
||
| 294 | throw new \Exception($wsdl, $status); |
||
| 295 | } |
||
| 296 | //save to file |
||
| 297 | $file = tempnam(sys_get_temp_dir(), date("Ymd") . '_') . '.xml'; |
||
| 298 | $fl = fopen($file, "w"); |
||
| 299 | fwrite($fl, $wsdl); |
||
| 300 | fclose($fl); |
||
| 301 | |||
| 302 | self::$soapClientInstance = new SoapClient($file, $options, $client); |
||
| 303 | |||
| 304 | unlink($file); |
||
| 305 | } |
||
| 306 | |||
| 307 | $this->soapClient = self::$soapClientInstance; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Builds up the environment to run the given command. |
||
| 312 | * |
||
| 313 | * @param string $name |
||
| 314 | * @param array $params |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | protected static function runCommand($name, array $params = []) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param array $classNames |
||
| 352 | * @param bool $force |
||
| 353 | */ |
||
| 354 | protected function loadFixtures(array $classNames, $force = false) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param string $referenceUID |
||
| 383 | * |
||
| 384 | * @return object |
||
| 385 | */ |
||
| 386 | protected function getReference($referenceUID) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return ReferenceRepository|null |
||
| 393 | */ |
||
| 394 | protected function getReferenceRepository() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Callback function to be executed after fixture load. |
||
| 401 | */ |
||
| 402 | protected function postFixtureLoad() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Retrieve Doctrine DataFixtures loader. |
||
| 409 | * |
||
| 410 | * @param array $classNames |
||
| 411 | * |
||
| 412 | * @return DataFixturesLoader |
||
| 413 | */ |
||
| 414 | private function getFixtureLoader(array $classNames) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Load a data fixture class. |
||
| 427 | * |
||
| 428 | * @param DataFixturesLoader $loader |
||
| 429 | * @param string $className |
||
| 430 | */ |
||
| 431 | private function loadFixtureClass(DataFixturesLoader $loader, $className) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Creates a mock object of a service identified by its id. |
||
| 451 | * |
||
| 452 | * @param string $id |
||
| 453 | * |
||
| 454 | * @return \PHPUnit_Framework_MockObject_MockBuilder |
||
| 455 | */ |
||
| 456 | protected function getServiceMockBuilder($id) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Generates a URL or path for a specific route based on the given parameters. |
||
| 465 | * |
||
| 466 | * @param string $name |
||
| 467 | * @param array $parameters |
||
| 468 | * @param bool $absolute |
||
| 469 | * |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | protected function getUrl($name, $parameters = [], $absolute = false) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get an instance of the dependency injection container. |
||
| 479 | * |
||
| 480 | * @return ContainerInterface |
||
| 481 | */ |
||
| 482 | protected static function getContainer() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * @return Client |
||
| 489 | * @throws \BadMethodCallException |
||
| 490 | */ |
||
| 491 | public static function getClientInstance() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Data provider for REST/SOAP API tests |
||
| 502 | * |
||
| 503 | * @param string $folder |
||
| 504 | * |
||
| 505 | * @return array |
||
| 506 | */ |
||
| 507 | public static function getApiRequestsData($folder) |
||
| 508 | { |
||
| 509 | static $randomString; |
||
| 510 | |||
| 511 | // generate unique value |
||
| 512 | if (!$randomString) { |
||
| 513 | $randomString = self::generateRandomString(5); |
||
| 514 | } |
||
| 515 | |||
| 516 | $parameters = []; |
||
| 517 | $testFiles = new \RecursiveDirectoryIterator($folder, \RecursiveDirectoryIterator::SKIP_DOTS); |
||
| 518 | foreach ($testFiles as $fileName => $object) { |
||
| 519 | $parameters[$fileName] = Yaml::parse(file_get_contents($fileName)) ?: []; |
||
| 520 | if (is_null($parameters[$fileName]['response'])) { |
||
| 521 | unset($parameters[$fileName]['response']); |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | $replaceCallback = function (&$value) use ($randomString) { |
||
| 526 | if (!is_null($value)) { |
||
| 527 | $value = str_replace('%str%', $randomString, $value); |
||
| 528 | } |
||
| 529 | }; |
||
| 530 | |||
| 531 | foreach ($parameters as $key => $value) { |
||
| 532 | array_walk( |
||
| 533 | $parameters[$key]['request'], |
||
| 534 | $replaceCallback, |
||
| 535 | $randomString |
||
| 536 | ); |
||
| 537 | array_walk( |
||
| 538 | $parameters[$key]['response'], |
||
| 539 | $replaceCallback, |
||
| 540 | $randomString |
||
| 541 | ); |
||
| 542 | } |
||
| 543 | |||
| 544 | return $parameters; |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param int $length |
||
| 549 | * |
||
| 550 | * @return string |
||
| 551 | */ |
||
| 552 | public static function generateRandomString($length = 10) |
||
| 566 | |||
| 567 | /** |
||
| 568 | * Generate WSSE authorization header |
||
| 569 | * |
||
| 570 | * @param string $userName |
||
| 571 | * @param string $userPassword |
||
| 572 | * @param string|null $nonce |
||
| 573 | * |
||
| 574 | * @return array |
||
| 575 | */ |
||
| 576 | public static function generateWsseAuthHeader( |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Generate Basic authorization header |
||
| 604 | * |
||
| 605 | * @param string $userName |
||
| 606 | * @param string $userPassword |
||
| 607 | * @param int $userOrganization |
||
| 608 | * |
||
| 609 | * @return array |
||
| 610 | */ |
||
| 611 | public static function generateBasicAuthHeader( |
||
| 622 | |||
| 623 | /** |
||
| 624 | * @return array |
||
| 625 | */ |
||
| 626 | public static function generateNoHashNavigationHeader() |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Convert value to array |
||
| 633 | * |
||
| 634 | * @param mixed $value |
||
| 635 | * |
||
| 636 | * @return array |
||
| 637 | */ |
||
| 638 | public static function valueToArray($value) |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Convert json to array |
||
| 645 | * |
||
| 646 | * @param string $json |
||
| 647 | * |
||
| 648 | * @return array |
||
| 649 | */ |
||
| 650 | public static function jsonToArray($json) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Checks json response status code and return content as array |
||
| 657 | * |
||
| 658 | * @param Response $response |
||
| 659 | * @param int $statusCode |
||
| 660 | * |
||
| 661 | * @return array |
||
| 662 | */ |
||
| 663 | public static function getJsonResponseContent(Response $response, $statusCode) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Assert response is json and has status code |
||
| 671 | * |
||
| 672 | * @param Response $response |
||
| 673 | * @param int $statusCode |
||
| 674 | */ |
||
| 675 | public static function assertEmptyResponseStatusCodeEquals(Response $response, $statusCode) |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Assert response is json and has status code |
||
| 686 | * |
||
| 687 | * @param Response $response |
||
| 688 | * @param int $statusCode |
||
| 689 | */ |
||
| 690 | public static function assertJsonResponseStatusCodeEquals(Response $response, $statusCode) |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Assert response is html and has status code |
||
| 698 | * |
||
| 699 | * @param Response $response |
||
| 700 | * @param int $statusCode |
||
| 701 | */ |
||
| 702 | public static function assertHtmlResponseStatusCodeEquals(Response $response, $statusCode) |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Assert response status code equals |
||
| 710 | * |
||
| 711 | * @param Response $response |
||
| 712 | * @param int $statusCode |
||
| 713 | */ |
||
| 714 | public static function assertResponseStatusCodeEquals(Response $response, $statusCode) |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Assert response content type equals |
||
| 749 | * |
||
| 750 | * @param Response $response |
||
| 751 | * @param string $contentType |
||
| 752 | */ |
||
| 753 | public static function assertResponseContentTypeEquals(Response $response, $contentType) |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Assert that intersect of $actual with $expected equals $expected |
||
| 763 | * |
||
| 764 | * @param array $expected |
||
| 765 | * @param array $actual |
||
| 766 | * @param string $message |
||
| 767 | */ |
||
| 768 | public static function assertArrayIntersectEquals(array $expected, array $actual, $message = null) |
||
| 782 | } |
||
| 783 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.