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() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 185 | * $actualObject. |
||
| 186 | * |
||
| 187 | * @param mixed[] $expectedValues |
||
| 188 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 189 | */ |
||
| 190 | protected function assertPropertiesCorrect(array $expectedValues, ValueObject $actualObject) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 199 | * $actualObject. |
||
| 200 | * |
||
| 201 | * If the property type is array, it will be sorted before comparison. |
||
| 202 | * |
||
| 203 | * @TODO: introduced because of randomly failing tests, ref: https://jira.ez.no/browse/EZP-21734 |
||
| 204 | * |
||
| 205 | * @param mixed[] $expectedValues |
||
| 206 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 207 | */ |
||
| 208 | protected function assertPropertiesCorrectUnsorted(array $expectedValues, ValueObject $actualObject) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Asserts all properties from $expectedValues are correctly set in |
||
| 217 | * $actualObject. Additional (virtual) properties can be asserted using |
||
| 218 | * $additionalProperties. |
||
| 219 | * |
||
| 220 | * @param \eZ\Publish\API\Repository\Values\ValueObject $expectedValues |
||
| 221 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 222 | * @param array $propertyNames |
||
|
|
|||
| 223 | */ |
||
| 224 | protected function assertStructPropertiesCorrect(ValueObject $expectedValues, ValueObject $actualObject, array $additionalProperties = array()) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @see \eZ\Publish\API\Repository\Tests\BaseTest::assertPropertiesCorrectUnsorted() |
||
| 237 | * |
||
| 238 | * @param array $items An array of scalar values |
||
| 239 | */ |
||
| 240 | private function sortItems(array &$items) |
||
| 251 | |||
| 252 | private function assertPropertiesEqual($propertyName, $expectedValue, $actualValue, $sortArray = false) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Create a user in editor user group. |
||
| 279 | * |
||
| 280 | * @param string $login |
||
| 281 | * |
||
| 282 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 283 | */ |
||
| 284 | protected function createUserVersion1($login = 'user') |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Create a user in new user group with editor rights limited to Media Library (/1/48/). |
||
| 319 | * |
||
| 320 | * @uses ::createCustomUserVersion1() |
||
| 321 | * |
||
| 322 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 323 | */ |
||
| 324 | protected function createMediaUserVersion1() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 335 | * |
||
| 336 | * @param string $userGroupName Name of the new user group to create |
||
| 337 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 338 | * @param RoleLimitation|null $roleLimitation |
||
| 339 | * |
||
| 340 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 341 | */ |
||
| 342 | protected function createCustomUserVersion1($userGroupName, $roleIdentifier, RoleLimitation $roleLimitation = null) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 355 | * |
||
| 356 | * @param string $login User login |
||
| 357 | * @param string $email User e-mail |
||
| 358 | * @param string $userGroupName Name of the new user group to create |
||
| 359 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 360 | * @param RoleLimitation|null $roleLimitation |
||
| 361 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 362 | */ |
||
| 363 | protected function createCustomUserWithLogin( |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Create a user using given data. |
||
| 416 | * |
||
| 417 | * @param string $login |
||
| 418 | * @param string $firstName |
||
| 419 | * @param string $lastName |
||
| 420 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup|null $userGroup optional user group, Editor by default |
||
| 421 | * |
||
| 422 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 423 | */ |
||
| 424 | protected function createUser($login, $firstName, $lastName, UserGroup $userGroup = null) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Only for internal use. |
||
| 454 | * |
||
| 455 | * Creates a \DateTime object for $timestamp in the current time zone |
||
| 456 | * |
||
| 457 | * @param int $timestamp |
||
| 458 | * |
||
| 459 | * @return \DateTime |
||
| 460 | */ |
||
| 461 | View Code Duplication | public function createDateTime($timestamp = null) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Calls given Repository's aggregated SearchHandler::refresh(). |
||
| 473 | * |
||
| 474 | * Currently implemented only in Solr search engine. |
||
| 475 | * |
||
| 476 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 477 | */ |
||
| 478 | protected function refreshSearch(Repository $repository) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Create role of a given name with the given policies described by an array. |
||
| 509 | * |
||
| 510 | * @param $roleName |
||
| 511 | * @param array $policiesData [['module' => 'content', 'function' => 'read', 'limitations' => []] |
||
| 512 | * |
||
| 513 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
| 514 | * |
||
| 515 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 516 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException |
||
| 517 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
||
| 518 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException |
||
| 519 | */ |
||
| 520 | public function createRoleWithPolicies($roleName, array $policiesData) |
||
| 547 | } |
||
| 548 |
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.