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() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 182 | * $actualObject. |
||
| 183 | * |
||
| 184 | * @param mixed[] $expectedValues |
||
| 185 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 186 | */ |
||
| 187 | protected function assertPropertiesCorrect(array $expectedValues, ValueObject $actualObject) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 196 | * $actualObject. |
||
| 197 | * |
||
| 198 | * If the property type is array, it will be sorted before comparison. |
||
| 199 | * |
||
| 200 | * @TODO: introduced because of randomly failing tests, ref: https://jira.ez.no/browse/EZP-21734 |
||
| 201 | * |
||
| 202 | * @param mixed[] $expectedValues |
||
| 203 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 204 | */ |
||
| 205 | protected function assertPropertiesCorrectUnsorted(array $expectedValues, ValueObject $actualObject) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Asserts all properties from $expectedValues are correctly set in |
||
| 214 | * $actualObject. Additional (virtual) properties can be asserted using |
||
| 215 | * $additionalProperties. |
||
| 216 | * |
||
| 217 | * @param \eZ\Publish\API\Repository\Values\ValueObject $expectedValues |
||
| 218 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 219 | * @param array $propertyNames |
||
|
|
|||
| 220 | */ |
||
| 221 | protected function assertStructPropertiesCorrect(ValueObject $expectedValues, ValueObject $actualObject, array $additionalProperties = array()) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @see \eZ\Publish\API\Repository\Tests\BaseTest::assertPropertiesCorrectUnsorted() |
||
| 234 | * |
||
| 235 | * @param array $items An array of scalar values |
||
| 236 | */ |
||
| 237 | private function sortItems(array &$items) |
||
| 248 | |||
| 249 | private function assertPropertiesEqual($propertyName, $expectedValue, $actualValue, $sortArray = false) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Create a user in editor user group. |
||
| 276 | * |
||
| 277 | * @param string $login |
||
| 278 | * |
||
| 279 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 280 | */ |
||
| 281 | View Code Duplication | protected function createUserVersion1($login = 'user') |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Create a user in new user group with editor rights limited to Media Library (/1/48/). |
||
| 316 | * |
||
| 317 | * @uses ::createCustomUserVersion1() |
||
| 318 | * |
||
| 319 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 320 | */ |
||
| 321 | protected function createMediaUserVersion1() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 332 | * |
||
| 333 | * @param string $userGroupName Name of the new user group to create |
||
| 334 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 335 | * @param RoleLimitation|null $roleLimitation |
||
| 336 | * |
||
| 337 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 338 | */ |
||
| 339 | protected function createCustomUserVersion1($userGroupName, $roleIdentifier, RoleLimitation $roleLimitation = null) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Create a user using given data. |
||
| 387 | * |
||
| 388 | * @param string $login |
||
| 389 | * @param string $firstName |
||
| 390 | * @param string $lastName |
||
| 391 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup|null $userGroup optional user group, Editor by default |
||
| 392 | * |
||
| 393 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 394 | */ |
||
| 395 | protected function createUser($login, $firstName, $lastName, UserGroup $userGroup = null) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Only for internal use. |
||
| 425 | * |
||
| 426 | * Creates a \DateTime object for $timestamp in the current time zone |
||
| 427 | * |
||
| 428 | * @param int $timestamp |
||
| 429 | * |
||
| 430 | * @return \DateTime |
||
| 431 | */ |
||
| 432 | View Code Duplication | public function createDateTime($timestamp = null) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Calls given Repository's aggregated SearchHandler::refresh(). |
||
| 444 | * |
||
| 445 | * Currently implemented only in Solr search engine. |
||
| 446 | * |
||
| 447 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 448 | */ |
||
| 449 | protected function refreshSearch(Repository $repository) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Create role of a given name with the given policies described by an array. |
||
| 480 | * |
||
| 481 | * @param $roleName |
||
| 482 | * @param array $policiesData [['module' => 'content', 'function' => 'read', 'limitations' => []] |
||
| 483 | * |
||
| 484 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
| 485 | * |
||
| 486 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 487 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException |
||
| 488 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 489 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 490 | */ |
||
| 491 | public function createRoleWithPolicies($roleName, array $policiesData) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Create user and assign new role with the given policies. |
||
| 521 | * |
||
| 522 | * @param string $login |
||
| 523 | * @param array $policiesData list of policies in the form of <code>[ [ 'module' => 'name', 'function' => 'name'] ]</code> |
||
| 524 | * |
||
| 525 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 526 | * |
||
| 527 | * @throws \Exception |
||
| 528 | */ |
||
| 529 | public function createUserWithPolicies($login, array $policiesData) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @return \Doctrine\DBAL\Connection |
||
| 561 | * |
||
| 562 | * @throws \ErrorException |
||
| 563 | */ |
||
| 564 | protected function getRawDatabaseConnection() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Executes the given callback passing raw Database Connection (\Doctrine\DBAL\Connection). |
||
| 581 | * Returns the result returned by the given callback. |
||
| 582 | * |
||
| 583 | * **Note**: The method clears the entire persistence cache pool. |
||
| 584 | * |
||
| 585 | * @throws \Exception |
||
| 586 | * |
||
| 587 | * @param callable $callback |
||
| 588 | * |
||
| 589 | * @return mixed the return result of the given callback |
||
| 590 | */ |
||
| 591 | public function performRawDatabaseOperation(callable $callback) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Traverse all errors for all fields in all Translations to find expected one. |
||
| 615 | * |
||
| 616 | * @param \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $exception |
||
| 617 | * @param string $expectedValidationErrorMessage |
||
| 618 | */ |
||
| 619 | protected function assertValidationErrorOccurs( |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Create 'folder' Content. |
||
| 630 | * |
||
| 631 | * @param array $names Folder names in the form of <code>['<language_code>' => '<name>']</code> |
||
| 632 | * @param int $parentLocationId |
||
| 633 | * |
||
| 634 | * @return \eZ\Publish\API\Repository\Values\Content\Content published Content |
||
| 635 | * |
||
| 636 | * @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException |
||
| 637 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 638 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 639 | */ |
||
| 640 | protected function createFolder(array $names, $parentLocationId) |
||
| 666 | } |
||
| 667 |
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.