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 | 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()  | 
            ||
| 174 | |||
| 175 | /**  | 
            ||
| 176 | * Asserts that properties given in $expectedValues are correctly set in  | 
            ||
| 177 | * $actualObject.  | 
            ||
| 178 | *  | 
            ||
| 179 | * @param mixed[] $expectedValues  | 
            ||
| 180 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject  | 
            ||
| 181 | */  | 
            ||
| 182 | protected function assertPropertiesCorrect(array $expectedValues, ValueObject $actualObject)  | 
            ||
| 188 | |||
| 189 | /**  | 
            ||
| 190 | * Asserts that properties given in $expectedValues are correctly set in  | 
            ||
| 191 | * $actualObject.  | 
            ||
| 192 | *  | 
            ||
| 193 | * If the property type is array, it will be sorted before comparison.  | 
            ||
| 194 | *  | 
            ||
| 195 | * @TODO: introduced because of randomly failing tests, ref: https://jira.ez.no/browse/EZP-21734  | 
            ||
| 196 | *  | 
            ||
| 197 | * @param mixed[] $expectedValues  | 
            ||
| 198 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject  | 
            ||
| 199 | */  | 
            ||
| 200 | protected function assertPropertiesCorrectUnsorted(array $expectedValues, ValueObject $actualObject)  | 
            ||
| 206 | |||
| 207 | /**  | 
            ||
| 208 | * Asserts all properties from $expectedValues are correctly set in  | 
            ||
| 209 | * $actualObject. Additional (virtual) properties can be asserted using  | 
            ||
| 210 | * $additionalProperties.  | 
            ||
| 211 | *  | 
            ||
| 212 | * @param \eZ\Publish\API\Repository\Values\ValueObject $expectedValues  | 
            ||
| 213 | * @param \eZ\Publish\API\Repository\Values\ValueObject $actualObject  | 
            ||
| 214 | * @param array $propertyNames  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 215 | */  | 
            ||
| 216 | protected function assertStructPropertiesCorrect(ValueObject $expectedValues, ValueObject $actualObject, array $additionalProperties = array())  | 
            ||
| 226 | |||
| 227 | /**  | 
            ||
| 228 | * @see \eZ\Publish\API\Repository\Tests\BaseTest::assertPropertiesCorrectUnsorted()  | 
            ||
| 229 | *  | 
            ||
| 230 | * @param array $items An array of scalar values  | 
            ||
| 231 | */  | 
            ||
| 232 | private function sortItems(array &$items)  | 
            ||
| 243 | |||
| 244 | private function assertPropertiesEqual($propertyName, $expectedValue, $actualValue, $sortArray = false)  | 
            ||
| 268 | |||
| 269 | /**  | 
            ||
| 270 | * Create a user in editor user group.  | 
            ||
| 271 | *  | 
            ||
| 272 | * @return \eZ\Publish\API\Repository\Values\User\User  | 
            ||
| 273 | */  | 
            ||
| 274 | protected function createUserVersion1()  | 
            ||
| 275 |     { | 
            ||
| 276 | $repository = $this->getRepository();  | 
            ||
| 277 | |||
| 278 | /* BEGIN: Inline */  | 
            ||
| 279 | // ID of the "Editors" user group in an eZ Publish demo installation  | 
            ||
| 280 | $editorsGroupId = 13;  | 
            ||
| 281 | |||
| 282 | $userService = $repository->getUserService();  | 
            ||
| 283 | |||
| 284 | // Instantiate a create struct with mandatory properties  | 
            ||
| 285 | $userCreate = $userService->newUserCreateStruct(  | 
            ||
| 286 | 'user',  | 
            ||
| 287 | '[email protected]',  | 
            ||
| 288 | 'secret',  | 
            ||
| 289 | 'eng-US'  | 
            ||
| 290 | );  | 
            ||
| 291 | $userCreate->enabled = true;  | 
            ||
| 292 | |||
| 293 | // Set some fields required by the user ContentType  | 
            ||
| 294 |         $userCreate->setField('first_name', 'Example'); | 
            ||
| 295 |         $userCreate->setField('last_name', 'User'); | 
            ||
| 296 | |||
| 297 | // Load parent group for the user  | 
            ||
| 298 | $group = $userService->loadUserGroup($editorsGroupId);  | 
            ||
| 299 | |||
| 300 | // Create a new user instance.  | 
            ||
| 301 | $user = $userService->createUser($userCreate, array($group));  | 
            ||
| 302 | /* END: Inline */  | 
            ||
| 303 | |||
| 304 | return $user;  | 
            ||
| 305 | }  | 
            ||
| 306 | |||
| 307 | /**  | 
            ||
| 308 | * Create a user in new user group with editor rights limited to Media Library (/1/48/).  | 
            ||
| 309 | *  | 
            ||
| 310 | * @uses ::createCustomUserVersion1()  | 
            ||
| 311 | *  | 
            ||
| 312 | * @return \eZ\Publish\API\Repository\Values\User\User  | 
            ||
| 313 | */  | 
            ||
| 314 | protected function createMediaUserVersion1()  | 
            ||
| 322 | |||
| 323 | /**  | 
            ||
| 324 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation).  | 
            ||
| 325 | *  | 
            ||
| 326 | * @param string $userGroupName Name of the new user group to create  | 
            ||
| 327 | * @param string $roleIdentifier Role identifier to assign to the new group  | 
            ||
| 328 | * @param RoleLimitation|null $roleLimitation  | 
            ||
| 329 | *  | 
            ||
| 330 | * @return \eZ\Publish\API\Repository\Values\User\User  | 
            ||
| 331 | */  | 
            ||
| 332 | protected function createCustomUserVersion1($userGroupName, $roleIdentifier, RoleLimitation $roleLimitation = null)  | 
            ||
| 342 | |||
| 343 | /**  | 
            ||
| 344 | * Create a user with new user group and assign a existing role (optionally with RoleLimitation).  | 
            ||
| 345 | *  | 
            ||
| 346 | * @param string $login User login  | 
            ||
| 347 | * @param string $email User e-mail  | 
            ||
| 348 | * @param string $userGroupName Name of the new user group to create  | 
            ||
| 349 | * @param string $roleIdentifier Role identifier to assign to the new group  | 
            ||
| 350 | * @param RoleLimitation|null $roleLimitation  | 
            ||
| 351 | * @return \eZ\Publish\API\Repository\Values\User\User  | 
            ||
| 352 | */  | 
            ||
| 353 | protected function createCustomUserWithLogin(  | 
            ||
| 403 | |||
| 404 | /**  | 
            ||
| 405 | * Create a user using given data.  | 
            ||
| 406 | *  | 
            ||
| 407 | * @param string $login  | 
            ||
| 408 | * @param string $firstName  | 
            ||
| 409 | * @param string $lastName  | 
            ||
| 410 | * @return \eZ\Publish\API\Repository\Values\User\User  | 
            ||
| 411 | */  | 
            ||
| 412 | protected function createUser($login, $firstName, $lastName)  | 
            ||
| 437 | |||
| 438 | /**  | 
            ||
| 439 | * Only for internal use.  | 
            ||
| 440 | *  | 
            ||
| 441 | * Creates a \DateTime object for $timestamp in the current time zone  | 
            ||
| 442 | *  | 
            ||
| 443 | * @param int $timestamp  | 
            ||
| 444 | *  | 
            ||
| 445 | * @return \DateTime  | 
            ||
| 446 | */  | 
            ||
| 447 | View Code Duplication | public function createDateTime($timestamp = null)  | 
            |
| 456 | |||
| 457 | /**  | 
            ||
| 458 | * Calls given Repository's aggregated SearchHandler::refresh().  | 
            ||
| 459 | *  | 
            ||
| 460 | * Currently implemented only in Solr search engine.  | 
            ||
| 461 | *  | 
            ||
| 462 | * @param \eZ\Publish\API\Repository\Repository $repository  | 
            ||
| 463 | */  | 
            ||
| 464 | protected function refreshSearch(Repository $repository)  | 
            ||
| 492 | }  | 
            ||
| 493 | 
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.