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 |
||
| 27 | abstract class BaseTest extends TestCase |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Maximum integer number accepted by the different backends. |
||
| 31 | */ |
||
| 32 | const DB_INT_MAX = 2147483647; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var \eZ\Publish\API\Repository\Tests\SetupFactory |
||
| 36 | */ |
||
| 37 | private $setupFactory; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var \eZ\Publish\API\Repository\Repository |
||
| 41 | */ |
||
| 42 | private $repository; |
||
| 43 | |||
| 44 | protected function setUp() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Resets the temporary used repository between each test run. |
||
| 76 | */ |
||
| 77 | protected function tearDown() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the ID generator, fitting to the repository implementation. |
||
| 85 | * |
||
| 86 | * @return \eZ\Publish\API\Repository\Tests\IdManager |
||
| 87 | */ |
||
| 88 | protected function getIdManager() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Generates a repository specific ID value. |
||
| 95 | * |
||
| 96 | * @param string $type |
||
| 97 | * @param mixed $rawId |
||
| 98 | * |
||
| 99 | * @return mixed |
||
| 100 | */ |
||
| 101 | protected function generateId($type, $rawId) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Parses a repository specific ID value. |
||
| 108 | * |
||
| 109 | * @param string $type |
||
| 110 | * @param mixed $id |
||
| 111 | * |
||
| 112 | * @return mixed |
||
| 113 | */ |
||
| 114 | protected function parseId($type, $id) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns a config setting provided by the setup factory. |
||
| 121 | * |
||
| 122 | * @param string $configKey |
||
| 123 | * |
||
| 124 | * @return mixed |
||
| 125 | */ |
||
| 126 | protected function getConfigValue($configKey) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Tests if the currently tested api is based on a V4 implementation. |
||
| 133 | * |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | protected function isVersion4() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param bool $initialInitializeFromScratch Only has an effect if set in first call within a test |
||
| 143 | * |
||
| 144 | * @return \eZ\Publish\API\Repository\Repository |
||
| 145 | */ |
||
| 146 | protected function getRepository($initialInitializeFromScratch = true) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @return \eZ\Publish\API\Repository\Tests\SetupFactory |
||
| 157 | */ |
||
| 158 | protected function getSetupFactory() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 178 | * $actualObject. |
||
| 179 | * |
||
| 180 | * @param mixed[] $expectedValues |
||
| 181 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 182 | */ |
||
| 183 | protected function assertPropertiesCorrect(array $expectedValues, ValueObject $actualObject) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 192 | * $actualObject. |
||
| 193 | * |
||
| 194 | * If the property type is array, it will be sorted before comparison. |
||
| 195 | * |
||
| 196 | * @TODO: introduced because of randomly failing tests, ref: https://jira.ez.no/browse/EZP-21734 |
||
| 197 | * |
||
| 198 | * @param mixed[] $expectedValues |
||
| 199 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 200 | */ |
||
| 201 | protected function assertPropertiesCorrectUnsorted(array $expectedValues, ValueObject $actualObject) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Asserts all properties from $expectedValues are correctly set in |
||
| 210 | * $actualObject. Additional (virtual) properties can be asserted using |
||
| 211 | * $additionalProperties. |
||
| 212 | * |
||
| 213 | * @param \eZ\Publish\API\Repository\Values\ValueObject $expectedValues |
||
| 214 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 215 | * @param array $propertyNames |
||
|
|
|||
| 216 | */ |
||
| 217 | protected function assertStructPropertiesCorrect(ValueObject $expectedValues, ValueObject $actualObject, array $additionalProperties = array()) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @see \eZ\Publish\API\Repository\Tests\BaseTest::assertPropertiesCorrectUnsorted() |
||
| 230 | * |
||
| 231 | * @param array $items An array of scalar values |
||
| 232 | */ |
||
| 233 | private function sortItems(array &$items) |
||
| 244 | |||
| 245 | private function assertPropertiesEqual($propertyName, $expectedValue, $actualValue, $sortArray = false) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Create a user in editor user group. |
||
| 272 | * |
||
| 273 | * @param string $login |
||
| 274 | * |
||
| 275 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 276 | */ |
||
| 277 | View Code Duplication | protected function createUserVersion1($login = 'user') |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Create a user in new user group with editor rights limited to Media Library (/1/48/). |
||
| 312 | * |
||
| 313 | * @uses ::createCustomUserVersion1() |
||
| 314 | * |
||
| 315 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 316 | */ |
||
| 317 | protected function createMediaUserVersion1() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 328 | * |
||
| 329 | * @param string $userGroupName Name of the new user group to create |
||
| 330 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 331 | * @param RoleLimitation|null $roleLimitation |
||
| 332 | * |
||
| 333 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 334 | */ |
||
| 335 | protected function createCustomUserVersion1($userGroupName, $roleIdentifier, RoleLimitation $roleLimitation = null) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Create a user using given data. |
||
| 383 | * |
||
| 384 | * @param string $login |
||
| 385 | * @param string $firstName |
||
| 386 | * @param string $lastName |
||
| 387 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup|null $userGroup optional user group, Editor by default |
||
| 388 | * |
||
| 389 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 390 | */ |
||
| 391 | protected function createUser($login, $firstName, $lastName, UserGroup $userGroup = null) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Only for internal use. |
||
| 421 | * |
||
| 422 | * Creates a \DateTime object for $timestamp in the current time zone |
||
| 423 | * |
||
| 424 | * @param int $timestamp |
||
| 425 | * |
||
| 426 | * @return \DateTime |
||
| 427 | */ |
||
| 428 | View Code Duplication | public function createDateTime($timestamp = null) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Calls given Repository's aggregated SearchHandler::refresh(). |
||
| 440 | * |
||
| 441 | * Currently implemented only in Solr search engine. |
||
| 442 | * |
||
| 443 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 444 | */ |
||
| 445 | protected function refreshSearch(Repository $repository) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Create role of a given name with the given policies described by an array. |
||
| 476 | * |
||
| 477 | * @param $roleName |
||
| 478 | * @param array $policiesData [['module' => 'content', 'function' => 'read', 'limitations' => []] |
||
| 479 | * |
||
| 480 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
| 481 | * |
||
| 482 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 483 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException |
||
| 484 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 485 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 486 | */ |
||
| 487 | public function createRoleWithPolicies($roleName, array $policiesData) |
||
| 514 | } |
||
| 515 |
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.