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 PHPUnit_Framework_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 | /** |
||
| 44 | */ |
||
| 45 | protected function setUp() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Resets the temporary used repository between each test run. |
||
| 77 | */ |
||
| 78 | protected function tearDown() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Returns the ID generator, fitting to the repository implementation. |
||
| 86 | * |
||
| 87 | * @return \eZ\Publish\API\Repository\Tests\IdManager |
||
| 88 | */ |
||
| 89 | protected function getIdManager() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Generates a repository specific ID value. |
||
| 96 | * |
||
| 97 | * @param string $type |
||
| 98 | * @param mixed $rawId |
||
| 99 | * |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | protected function generateId($type, $rawId) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Parses a repository specific ID value. |
||
| 109 | * |
||
| 110 | * @param string $type |
||
| 111 | * @param mixed $id |
||
| 112 | * |
||
| 113 | * @return mixed |
||
| 114 | */ |
||
| 115 | protected function parseId($type, $id) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns a config setting provided by the setup factory. |
||
| 122 | * |
||
| 123 | * @param string $configKey |
||
| 124 | * |
||
| 125 | * @return mixed |
||
| 126 | */ |
||
| 127 | protected function getConfigValue($configKey) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Tests if the currently tested api is based on a V4 implementation. |
||
| 134 | * |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | protected function isVersion4() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param bool $initialInitializeFromScratch Only has an effect if set in first call within a test |
||
| 144 | * |
||
| 145 | * @return \eZ\Publish\API\Repository\Repository |
||
| 146 | */ |
||
| 147 | protected function getRepository($initialInitializeFromScratch = true) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return \eZ\Publish\API\Repository\Tests\SetupFactory |
||
| 158 | */ |
||
| 159 | protected function getSetupFactory() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 179 | * $actualObject. |
||
| 180 | * |
||
| 181 | * @param mixed[] $expectedValues |
||
| 182 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 183 | */ |
||
| 184 | protected function assertPropertiesCorrect(array $expectedValues, ValueObject $actualObject) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Asserts that properties given in $expectedValues are correctly set in |
||
| 193 | * $actualObject. |
||
| 194 | * |
||
| 195 | * If the property type is array, it will be sorted before comparison. |
||
| 196 | * |
||
| 197 | * @TODO: introduced because of randomly failing tests, ref: https://jira.ez.no/browse/EZP-21734 |
||
| 198 | * |
||
| 199 | * @param mixed[] $expectedValues |
||
| 200 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 201 | */ |
||
| 202 | protected function assertPropertiesCorrectUnsorted(array $expectedValues, ValueObject $actualObject) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Asserts all properties from $expectedValues are correctly set in |
||
| 211 | * $actualObject. Additional (virtual) properties can be asserted using |
||
| 212 | * $additionalProperties. |
||
| 213 | * |
||
| 214 | * @param \eZ\Publish\API\Repository\Values\ValueObject $expectedValues |
||
| 215 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject |
||
| 216 | * @param array $propertyNames |
||
|
|
|||
| 217 | */ |
||
| 218 | protected function assertStructPropertiesCorrect(ValueObject $expectedValues, ValueObject $actualObject, array $additionalProperties = array()) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @see \eZ\Publish\API\Repository\Tests\BaseTest::assertPropertiesCorrectUnsorted() |
||
| 231 | * |
||
| 232 | * @param array $items An array of scalar values |
||
| 233 | */ |
||
| 234 | private function sortItems(array &$items) |
||
| 245 | |||
| 246 | private function assertPropertiesEqual($propertyName, $expectedValue, $actualValue, $sortArray = false) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Create a user in editor user group. |
||
| 273 | * |
||
| 274 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 275 | */ |
||
| 276 | protected function createUserVersion1() |
||
| 277 | { |
||
| 278 | $repository = $this->getRepository(); |
||
| 279 | |||
| 280 | /* BEGIN: Inline */ |
||
| 281 | // ID of the "Editors" user group in an eZ Publish demo installation |
||
| 282 | $editorsGroupId = 13; |
||
| 283 | |||
| 284 | $userService = $repository->getUserService(); |
||
| 285 | |||
| 286 | // Instantiate a create struct with mandatory properties |
||
| 287 | $userCreate = $userService->newUserCreateStruct( |
||
| 288 | 'user', |
||
| 289 | '[email protected]', |
||
| 290 | 'secret', |
||
| 291 | 'eng-US' |
||
| 292 | ); |
||
| 293 | $userCreate->enabled = true; |
||
| 294 | |||
| 295 | // Set some fields required by the user ContentType |
||
| 296 | $userCreate->setField('first_name', 'Example'); |
||
| 297 | $userCreate->setField('last_name', 'User'); |
||
| 298 | |||
| 299 | // Load parent group for the user |
||
| 300 | $group = $userService->loadUserGroup($editorsGroupId); |
||
| 301 | |||
| 302 | // Create a new user instance. |
||
| 303 | $user = $userService->createUser($userCreate, array($group)); |
||
| 304 | /* END: Inline */ |
||
| 305 | |||
| 306 | return $user; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Create a user in new user group with editor rights limited to Media Library (/1/48/). |
||
| 311 | * |
||
| 312 | * @uses createCustomUserVersion1() |
||
| 313 | * |
||
| 314 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 315 | */ |
||
| 316 | protected function createMediaUserVersion1() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 327 | * |
||
| 328 | * @param string $userGroupName Name of the new user group to create |
||
| 329 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 330 | * @param RoleLimitation|null $roleLimitation |
||
| 331 | * |
||
| 332 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 333 | */ |
||
| 334 | protected function createCustomUserVersion1($userGroupName, $roleIdentifier, RoleLimitation $roleLimitation = null) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation). |
||
| 347 | * |
||
| 348 | * @param string $login User login |
||
| 349 | * @param string $email User e-mail |
||
| 350 | * @param string $userGroupName Name of the new user group to create |
||
| 351 | * @param string $roleIdentifier Role identifier to assign to the new group |
||
| 352 | * @param RoleLimitation|null $roleLimitation |
||
| 353 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
| 354 | */ |
||
| 355 | protected function createCustomUserWithLogin( |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Only for internal use. |
||
| 408 | * |
||
| 409 | * Creates a \DateTime object for $timestamp in the current time zone |
||
| 410 | * |
||
| 411 | * @param int $timestamp |
||
| 412 | * |
||
| 413 | * @return \DateTime |
||
| 414 | */ |
||
| 415 | View Code Duplication | public function createDateTime($timestamp = null) |
|
| 424 | |||
| 425 | /** |
||
| 426 | * Calls given Repository's aggregated SearchHandler::refresh(). |
||
| 427 | * |
||
| 428 | * Currently implemented only in Solr search engine. |
||
| 429 | * |
||
| 430 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 431 | */ |
||
| 432 | protected function refreshSearch(Repository $repository) |
||
| 460 | } |
||
| 461 |
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.