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 | View Code Duplication | protected function createUserVersion1() |
|
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) |
||
379 | |||
380 | /** |
||
381 | * Only for internal use. |
||
382 | * |
||
383 | * Creates a \DateTime object for $timestamp in the current time zone |
||
384 | * |
||
385 | * @param int $timestamp |
||
386 | * |
||
387 | * @return \DateTime |
||
388 | */ |
||
389 | View Code Duplication | public function createDateTime($timestamp = null) |
|
398 | |||
399 | /** |
||
400 | * Calls given Repository's aggregated SearchHandler::refresh(). |
||
401 | * |
||
402 | * Currently implemented only in Solr search engine. |
||
403 | * |
||
404 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
405 | */ |
||
406 | protected function refreshSearch(Repository $repository) |
||
434 | } |
||
435 |
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.