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 |
||
| 29 | abstract class BaseTest extends TestCase |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Maximum integer number accepted by the different backends. |
||
| 33 | */ |
||
| 34 | const DB_INT_MAX = 2147483647; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \eZ\Publish\API\Repository\Tests\SetupFactory |
||
| 38 | */ |
||
| 39 | private $setupFactory; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \eZ\Publish\API\Repository\Repository |
||
| 43 | */ |
||
| 44 | private $repository; |
||
| 45 | |||
| 46 | protected function setUp() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Resets the temporary used repository between each test run. |
||
| 78 | */ |
||
| 79 | protected function tearDown() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Returns the ID generator, fitting to the repository implementation. |
||
| 87 | * |
||
| 88 | * @return \eZ\Publish\API\Repository\Tests\IdManager |
||
| 89 | */ |
||
| 90 | protected function getIdManager() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Generates a repository specific ID value. |
||
| 97 | * |
||
| 98 | * @param string $type |
||
| 99 | * @param mixed $rawId |
||
| 100 | * |
||
| 101 | * @return mixed |
||
| 102 | */ |
||
| 103 | protected function generateId($type, $rawId) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Parses a repository specific ID value. |
||
| 110 | * |
||
| 111 | * @param string $type |
||
| 112 | * @param mixed $id |
||
| 113 | * |
||
| 114 | * @return mixed |
||
| 115 | */ |
||
| 116 | protected function parseId($type, $id) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Returns a config setting provided by the setup factory. |
||
| 123 | * |
||
| 124 | * @param string $configKey |
||
| 125 | * |
||
| 126 | * @return mixed |
||
| 127 | */ |
||
| 128 | protected function getConfigValue($configKey) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Tests if the currently tested api is based on a V4 implementation. |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | protected function isVersion4() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param bool $initialInitializeFromScratch Only has an effect if set in first call within a test |
||
| 145 | * |
||
| 146 | * @return \eZ\Publish\API\Repository\Repository |
||
| 147 | */ |
||
| 148 | protected function getRepository($initialInitializeFromScratch = true) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return \eZ\Publish\API\Repository\Tests\SetupFactory |
||
| 159 | */ |
||
| 160 | protected function getSetupFactory() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 187 | * $actualObject. |
||
| 188 | * |
||
| 189 | * @param mixed[] $expectedValues |
||
| 190 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 191 | */ |
||
| 192 | protected function assertPropertiesCorrect(array $expectedValues, ValueObject $actualObject) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 201 | * $actualObject. |
||
| 202 | * |
||
| 203 | * If the property type is array, it will be sorted before comparison. |
||
| 204 | * |
||
| 205 | * @TODO: introduced because of randomly failing tests, ref: https://jira.ez.no/browse/EZP-21734 |
||
| 206 | * |
||
| 207 | * @param mixed[] $expectedValues |
||
| 208 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 209 | */ |
||
| 210 | protected function assertPropertiesCorrectUnsorted(array $expectedValues, ValueObject $actualObject) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Asserts all properties from $expectedValues are correctly set in |
||
| 219 | * $actualObject. Additional (virtual) properties can be asserted using |
||
| 220 | * $additionalProperties. |
||
| 221 | * |
||
| 222 | * @param \eZ\Publish\API\Repository\Values\ValueObject $expectedValues |
||
| 223 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 224 | * @param array $propertyNames |
||
|
|
|||
| 225 | */ |
||
| 226 | protected function assertStructPropertiesCorrect(ValueObject $expectedValues, ValueObject $actualObject, array $additionalProperties = array()) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @see \eZ\Publish\API\Repository\Tests\BaseTest::assertPropertiesCorrectUnsorted() |
||
| 239 | * |
||
| 240 | * @param array $items An array of scalar values |
||
| 241 | */ |
||
| 242 | private function sortItems(array &$items) |
||
| 253 | |||
| 254 | private function assertPropertiesEqual($propertyName, $expectedValue, $actualValue, $sortArray = false) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Create a user in editor user group. |
||
| 281 | * |
||
| 282 | * @param string $login |
||
| 283 | * |
||
| 284 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 285 | */ |
||
| 286 | protected function createUserVersion1($login = 'user') |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Create a user in new user group with editor rights limited to Media Library (/1/48/). |
||
| 321 | * |
||
| 322 | * @uses ::createCustomUserVersion1() |
||
| 323 | * |
||
| 324 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 325 | */ |
||
| 326 | protected function createMediaUserVersion1() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 337 | * |
||
| 338 | * @param string $userGroupName Name of the new user group to create |
||
| 339 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 340 | * @param RoleLimitation|null $roleLimitation |
||
| 341 | * |
||
| 342 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 343 | */ |
||
| 344 | protected function createCustomUserVersion1($userGroupName, $roleIdentifier, RoleLimitation $roleLimitation = null) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 357 | * |
||
| 358 | * @param string $login User login |
||
| 359 | * @param string $email User e-mail |
||
| 360 | * @param string $userGroupName Name of the new user group to create |
||
| 361 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 362 | * @param RoleLimitation|null $roleLimitation |
||
| 363 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 364 | */ |
||
| 365 | protected function createCustomUserWithLogin( |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Create a user using given data. |
||
| 418 | * |
||
| 419 | * @param string $login |
||
| 420 | * @param string $firstName |
||
| 421 | * @param string $lastName |
||
| 422 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup|null $userGroup optional user group, Editor by default |
||
| 423 | * |
||
| 424 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 425 | */ |
||
| 426 | protected function createUser($login, $firstName, $lastName, UserGroup $userGroup = null) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Only for internal use. |
||
| 456 | * |
||
| 457 | * Creates a \DateTime object for $timestamp in the current time zone |
||
| 458 | * |
||
| 459 | * @param int $timestamp |
||
| 460 | * |
||
| 461 | * @return \DateTime |
||
| 462 | */ |
||
| 463 | View Code Duplication | public function createDateTime($timestamp = null) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Calls given Repository's aggregated SearchHandler::refresh(). |
||
| 475 | * |
||
| 476 | * Currently implemented only in Solr search engine. |
||
| 477 | * |
||
| 478 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 479 | */ |
||
| 480 | protected function refreshSearch(Repository $repository) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Create role of a given name with the given policies described by an array. |
||
| 511 | * |
||
| 512 | * @param $roleName |
||
| 513 | * @param array $policiesData [['module' => 'content', 'function' => 'read', 'limitations' => []] |
||
| 514 | * |
||
| 515 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
| 516 | * |
||
| 517 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 518 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException |
||
| 519 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 520 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 521 | */ |
||
| 522 | public function createRoleWithPolicies($roleName, array $policiesData) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Traverse all errors for all fields in all Translations to find expected one. |
||
| 552 | * |
||
| 553 | * @param \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException $exception |
||
| 554 | * @param string $expectedValidationErrorMessage |
||
| 555 | */ |
||
| 556 | protected function assertValidationErrorOccurs( |
||
| 564 | } |
||
| 565 |
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.