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 BaseTest 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 BaseTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | abstract class BaseTest extends TestCase |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * Maximum integer number accepted by the different backends. |
||
| 35 | */ |
||
| 36 | const DB_INT_MAX = 2147483647; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \eZ\Publish\API\Repository\Tests\SetupFactory |
||
| 40 | */ |
||
| 41 | private $setupFactory; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \eZ\Publish\API\Repository\Repository |
||
| 45 | */ |
||
| 46 | private $repository; |
||
| 47 | |||
| 48 | protected function setUp() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Resets the temporary used repository between each test run. |
||
| 80 | */ |
||
| 81 | protected function tearDown() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns the ID generator, fitting to the repository implementation. |
||
| 89 | * |
||
| 90 | * @return \eZ\Publish\API\Repository\Tests\IdManager |
||
| 91 | */ |
||
| 92 | protected function getIdManager() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Generates a repository specific ID value. |
||
| 99 | * |
||
| 100 | * @param string $type |
||
| 101 | * @param mixed $rawId |
||
| 102 | * |
||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | protected function generateId($type, $rawId) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Parses a repository specific ID value. |
||
| 112 | * |
||
| 113 | * @param string $type |
||
| 114 | * @param mixed $id |
||
| 115 | * |
||
| 116 | * @return mixed |
||
| 117 | */ |
||
| 118 | protected function parseId($type, $id) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns a config setting provided by the setup factory. |
||
| 125 | * |
||
| 126 | * @param string $configKey |
||
| 127 | * |
||
| 128 | * @return mixed |
||
| 129 | */ |
||
| 130 | protected function getConfigValue($configKey) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Tests if the currently tested api is based on a V4 implementation. |
||
| 137 | * |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | protected function isVersion4() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param bool $initialInitializeFromScratch Only has an effect if set in first call within a test |
||
| 147 | * |
||
| 148 | * @return \eZ\Publish\API\Repository\Repository |
||
| 149 | */ |
||
| 150 | protected function getRepository($initialInitializeFromScratch = true) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return \eZ\Publish\API\Repository\Tests\SetupFactory |
||
| 161 | */ |
||
| 162 | protected function getSetupFactory() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 189 | * $actualObject. |
||
| 190 | * |
||
| 191 | * @param mixed[] $expectedValues |
||
| 192 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 193 | */ |
||
| 194 | protected function assertPropertiesCorrect(array $expectedValues, ValueObject $actualObject) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 203 | * $actualObject. |
||
| 204 | * |
||
| 205 | * If the property type is array, it will be sorted before comparison. |
||
| 206 | * |
||
| 207 | * @TODO: introduced because of randomly failing tests, ref: https://jira.ez.no/browse/EZP-21734 |
||
| 208 | * |
||
| 209 | * @param mixed[] $expectedValues |
||
| 210 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 211 | */ |
||
| 212 | protected function assertPropertiesCorrectUnsorted(array $expectedValues, ValueObject $actualObject) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Asserts all properties from $expectedValues are correctly set in |
||
| 221 | * $actualObject. Additional (virtual) properties can be asserted using |
||
| 222 | * $additionalProperties. |
||
| 223 | * |
||
| 224 | * @param \eZ\Publish\API\Repository\Values\ValueObject $expectedValues |
||
| 225 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 226 | * @param array $propertyNames |
||
|
|
|||
| 227 | */ |
||
| 228 | protected function assertStructPropertiesCorrect(ValueObject $expectedValues, ValueObject $actualObject, array $additionalProperties = array()) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @see \eZ\Publish\API\Repository\Tests\BaseTest::assertPropertiesCorrectUnsorted() |
||
| 241 | * |
||
| 242 | * @param array $items An array of scalar values |
||
| 243 | */ |
||
| 244 | private function sortItems(array &$items) |
||
| 255 | |||
| 256 | private function assertPropertiesEqual($propertyName, $expectedValue, $actualValue, $sortArray = false) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Create a user in editor user group. |
||
| 283 | * |
||
| 284 | * @param string $login |
||
| 285 | * |
||
| 286 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 287 | */ |
||
| 288 | protected function createUserVersion1($login = 'user') |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Create a user in new user group with editor rights limited to Media Library (/1/48/). |
||
| 323 | * |
||
| 324 | * @uses ::createCustomUserVersion1() |
||
| 325 | * |
||
| 326 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 327 | */ |
||
| 328 | protected function createMediaUserVersion1() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 339 | * |
||
| 340 | * @param string $userGroupName Name of the new user group to create |
||
| 341 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 342 | * @param RoleLimitation|null $roleLimitation |
||
| 343 | * |
||
| 344 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 345 | */ |
||
| 346 | protected function createCustomUserVersion1($userGroupName, $roleIdentifier, RoleLimitation $roleLimitation = null) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 359 | * |
||
| 360 | * @param string $login User login |
||
| 361 | * @param string $email User e-mail |
||
| 362 | * @param string $userGroupName Name of the new user group to create |
||
| 363 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 364 | * @param RoleLimitation|null $roleLimitation |
||
| 365 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 366 | */ |
||
| 367 | protected function createCustomUserWithLogin( |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Create a user using given data. |
||
| 420 | * |
||
| 421 | * @param string $login |
||
| 422 | * @param string $firstName |
||
| 423 | * @param string $lastName |
||
| 424 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup|null $userGroup optional user group, Editor by default |
||
| 425 | * |
||
| 426 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 427 | */ |
||
| 428 | protected function createUser($login, $firstName, $lastName, UserGroup $userGroup = null) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Only for internal use. |
||
| 458 | * |
||
| 459 | * Creates a \DateTime object for $timestamp in the current time zone |
||
| 460 | * |
||
| 461 | * @param int $timestamp |
||
| 462 | * |
||
| 463 | * @return \DateTime |
||
| 464 | */ |
||
| 465 | View Code Duplication | public function createDateTime($timestamp = null) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Calls given Repository's aggregated SearchHandler::refresh(). |
||
| 477 | * |
||
| 478 | * Currently implemented only in Solr search engine. |
||
| 479 | * |
||
| 480 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 481 | */ |
||
| 482 | protected function refreshSearch(Repository $repository) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Create role of a given name with the given policies described by an array. |
||
| 513 | * |
||
| 514 | * @param $roleName |
||
| 515 | * @param array $policiesData [['module' => 'content', 'function' => 'read', 'limitations' => []] |
||
| 516 | * |
||
| 517 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
| 518 | * |
||
| 519 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 520 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException |
||
| 521 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 522 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 523 | */ |
||
| 524 | public function createRoleWithPolicies($roleName, array $policiesData) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Create user and assign new role with the given policies. |
||
| 554 | * |
||
| 555 | * @param string $login |
||
| 556 | * @param array $policiesData list of policies in the form of <code>[ [ 'module' => 'name', 'function' => 'name'] ]</code> |
||
| 557 | * |
||
| 558 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 559 | * |
||
| 560 | * @throws \Exception |
||
| 561 | */ |
||
| 562 | public function createUserWithPolicies($login, array $policiesData) |
||
| 591 | |||
| 592 | /** |
||
| 593 | * @return \Doctrine\DBAL\Connection |
||
| 594 | * |
||
| 595 | * @throws \ErrorException |
||
| 596 | */ |
||
| 597 | protected function getRawDatabaseConnection() |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Executes the given callback passing raw Database Connection (\Doctrine\DBAL\Connection). |
||
| 614 | * Returns the result returned by the given callback. |
||
| 615 | * |
||
| 616 | * **Note**: The method clears the entire persistence cache pool. |
||
| 617 | * |
||
| 618 | * @throws \Exception |
||
| 619 | * |
||
| 620 | * @param callable $callback |
||
| 621 | * |
||
| 622 | * @return mixed the return result of the given callback |
||
| 623 | */ |
||
| 624 | public function performRawDatabaseOperation(callable $callback) |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Traverse all errors for all fields in all Translations to find expected one. |
||
| 648 | * |
||
| 649 | * @param \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $exception |
||
| 650 | * @param string $expectedValidationErrorMessage |
||
| 651 | */ |
||
| 652 | protected function assertValidationErrorOccurs( |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Create 'folder' Content. |
||
| 663 | * |
||
| 664 | * @param array $names Folder names in the form of <code>['<language_code>' => '<name>']</code> |
||
| 665 | * @param int $parentLocationId |
||
| 666 | * |
||
| 667 | * @return \eZ\Publish\API\Repository\Values\Content\Content published Content |
||
| 668 | * |
||
| 669 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException |
||
| 670 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 671 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 672 | */ |
||
| 673 | protected function createFolder(array $names, $parentLocationId) |
||
| 699 | } |
||
| 700 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.