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 | View Code Duplication | protected function createUserVersion1() |
|
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) |
||
377 | |||
378 | /** |
||
379 | * Create a user using given data. |
||
380 | * |
||
381 | * @param string $login |
||
382 | * @param string $firstName |
||
383 | * @param string $lastName |
||
384 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
385 | */ |
||
386 | protected function createUser($login, $firstName, $lastName) |
||
411 | |||
412 | /** |
||
413 | * Only for internal use. |
||
414 | * |
||
415 | * Creates a \DateTime object for $timestamp in the current time zone |
||
416 | * |
||
417 | * @param int $timestamp |
||
418 | * |
||
419 | * @return \DateTime |
||
420 | */ |
||
421 | View Code Duplication | public function createDateTime($timestamp = null) |
|
430 | |||
431 | /** |
||
432 | * Calls given Repository's aggregated SearchHandler::refresh(). |
||
433 | * |
||
434 | * Currently implemented only in Solr search engine. |
||
435 | * |
||
436 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
437 | */ |
||
438 | protected function refreshSearch(Repository $repository) |
||
466 | } |
||
467 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.