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 |
||
| 26 | abstract class BaseTest extends TestCase |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Maximum integer number accepted by the different backends. |
||
| 30 | */ |
||
| 31 | const DB_INT_MAX = 2147483647; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var \eZ\Publish\API\Repository\Tests\SetupFactory |
||
| 35 | */ |
||
| 36 | private $setupFactory; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \eZ\Publish\API\Repository\Repository |
||
| 40 | */ |
||
| 41 | private $repository; |
||
| 42 | |||
| 43 | protected function setUp() |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Resets the temporary used repository between each test run. |
||
| 75 | */ |
||
| 76 | protected function tearDown() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Returns the ID generator, fitting to the repository implementation. |
||
| 84 | * |
||
| 85 | * @return \eZ\Publish\API\Repository\Tests\IdManager |
||
| 86 | */ |
||
| 87 | protected function getIdManager() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Generates a repository specific ID value. |
||
| 94 | * |
||
| 95 | * @param string $type |
||
| 96 | * @param mixed $rawId |
||
| 97 | * |
||
| 98 | * @return mixed |
||
| 99 | */ |
||
| 100 | protected function generateId($type, $rawId) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Parses a repository specific ID value. |
||
| 107 | * |
||
| 108 | * @param string $type |
||
| 109 | * @param mixed $id |
||
| 110 | * |
||
| 111 | * @return mixed |
||
| 112 | */ |
||
| 113 | protected function parseId($type, $id) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Returns a config setting provided by the setup factory. |
||
| 120 | * |
||
| 121 | * @param string $configKey |
||
| 122 | * |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | protected function getConfigValue($configKey) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Tests if the currently tested api is based on a V4 implementation. |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | protected function isVersion4() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param bool $initialInitializeFromScratch Only has an effect if set in first call within a test |
||
| 142 | * |
||
| 143 | * @return \eZ\Publish\API\Repository\Repository |
||
| 144 | */ |
||
| 145 | protected function getRepository($initialInitializeFromScratch = true) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @return \eZ\Publish\API\Repository\Tests\SetupFactory |
||
| 156 | */ |
||
| 157 | protected function getSetupFactory() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 184 | * $actualObject. |
||
| 185 | * |
||
| 186 | * @param mixed[] $expectedValues |
||
| 187 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 188 | */ |
||
| 189 | protected function assertPropertiesCorrect(array $expectedValues, ValueObject $actualObject) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 198 | * $actualObject. |
||
| 199 | * |
||
| 200 | * If the property type is array, it will be sorted before comparison. |
||
| 201 | * |
||
| 202 | * @TODO: introduced because of randomly failing tests, ref: https://jira.ez.no/browse/EZP-21734 |
||
| 203 | * |
||
| 204 | * @param mixed[] $expectedValues |
||
| 205 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 206 | */ |
||
| 207 | protected function assertPropertiesCorrectUnsorted(array $expectedValues, ValueObject $actualObject) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Asserts all properties from $expectedValues are correctly set in |
||
| 216 | * $actualObject. Additional (virtual) properties can be asserted using |
||
| 217 | * $additionalProperties. |
||
| 218 | * |
||
| 219 | * @param \eZ\Publish\API\Repository\Values\ValueObject $expectedValues |
||
| 220 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 221 | * @param array $propertyNames |
||
|
|
|||
| 222 | */ |
||
| 223 | protected function assertStructPropertiesCorrect(ValueObject $expectedValues, ValueObject $actualObject, array $additionalProperties = array()) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @see \eZ\Publish\API\Repository\Tests\BaseTest::assertPropertiesCorrectUnsorted() |
||
| 236 | * |
||
| 237 | * @param array $items An array of scalar values |
||
| 238 | */ |
||
| 239 | private function sortItems(array &$items) |
||
| 250 | |||
| 251 | private function assertPropertiesEqual($propertyName, $expectedValue, $actualValue, $sortArray = false) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Create a user in editor user group. |
||
| 278 | * |
||
| 279 | * @param string $login |
||
| 280 | * |
||
| 281 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 282 | */ |
||
| 283 | protected function createUserVersion1($login = 'user') |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Create a user in new user group with editor rights limited to Media Library (/1/48/). |
||
| 318 | * |
||
| 319 | * @uses ::createCustomUserVersion1() |
||
| 320 | * |
||
| 321 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 322 | */ |
||
| 323 | protected function createMediaUserVersion1() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 334 | * |
||
| 335 | * @param string $userGroupName Name of the new user group to create |
||
| 336 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 337 | * @param RoleLimitation|null $roleLimitation |
||
| 338 | * |
||
| 339 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 340 | */ |
||
| 341 | protected function createCustomUserVersion1($userGroupName, $roleIdentifier, RoleLimitation $roleLimitation = null) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 354 | * |
||
| 355 | * @param string $login User login |
||
| 356 | * @param string $email User e-mail |
||
| 357 | * @param string $userGroupName Name of the new user group to create |
||
| 358 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 359 | * @param RoleLimitation|null $roleLimitation |
||
| 360 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 361 | */ |
||
| 362 | protected function createCustomUserWithLogin( |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Create a user using given data. |
||
| 415 | * |
||
| 416 | * @param string $login |
||
| 417 | * @param string $firstName |
||
| 418 | * @param string $lastName |
||
| 419 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 420 | */ |
||
| 421 | protected function createUser($login, $firstName, $lastName) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Only for internal use. |
||
| 449 | * |
||
| 450 | * Creates a \DateTime object for $timestamp in the current time zone |
||
| 451 | * |
||
| 452 | * @param int $timestamp |
||
| 453 | * |
||
| 454 | * @return \DateTime |
||
| 455 | */ |
||
| 456 | View Code Duplication | public function createDateTime($timestamp = null) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Calls given Repository's aggregated SearchHandler::refresh(). |
||
| 468 | * |
||
| 469 | * Currently implemented only in Solr search engine. |
||
| 470 | * |
||
| 471 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 472 | */ |
||
| 473 | protected function refreshSearch(Repository $repository) |
||
| 501 | } |
||
| 502 |
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.