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:
1 | <?php |
||
24 | abstract class Base extends TestCase |
||
25 | { |
||
26 | use PHPUnit5CompatTrait; |
||
27 | |||
28 | /** |
||
29 | * @var \eZ\Publish\API\Repository\Repository |
||
30 | */ |
||
31 | private $repository; |
||
32 | |||
33 | /** |
||
34 | * @var \eZ\Publish\API\Repository\Repository|\PHPUnit_Framework_MockObject_MockObject |
||
35 | */ |
||
36 | private $repositoryMock; |
||
37 | |||
38 | /** |
||
39 | * @var \eZ\Publish\SPI\Persistence\Handler|\PHPUnit_Framework_MockObject_MockObject |
||
40 | */ |
||
41 | private $persistenceMock; |
||
42 | |||
43 | /** |
||
44 | * The Content / Location / Search ... handlers for the persistence / Search / .. handler mocks. |
||
45 | * |
||
46 | * @var \PHPUnit_Framework_MockObject_MockObject[] Key is relative to "\eZ\Publish\SPI\" |
||
47 | * |
||
48 | * @see getPersistenceMockHandler() |
||
49 | */ |
||
50 | private $spiMockHandlers = array(); |
||
51 | |||
52 | /** |
||
53 | * Get Real repository with mocked dependencies. |
||
54 | * |
||
55 | * @param array $serviceSettings If set then non shared instance of Repository is returned |
||
56 | * |
||
57 | * @return \eZ\Publish\API\Repository\Repository |
||
58 | */ |
||
59 | protected function getRepository(array $serviceSettings = array()) |
||
80 | |||
81 | protected $fieldTypeServiceMock; |
||
82 | |||
83 | /** |
||
84 | * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\API\Repository\FieldTypeService |
||
85 | */ |
||
86 | protected function getFieldTypeServiceMock() |
||
97 | |||
98 | protected $fieldTypeRegistryMock; |
||
99 | |||
100 | /** |
||
101 | * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\Repository\Helper\FieldTypeRegistry |
||
102 | */ |
||
103 | protected function getFieldTypeRegistryMock() |
||
114 | |||
115 | protected $nameableFieldTypeRegistryMock; |
||
116 | |||
117 | /** |
||
118 | * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\Repository\Helper\NameableFieldTypeRegistry |
||
119 | */ |
||
120 | protected function getNameableFieldTypeRegistryMock() |
||
131 | |||
132 | /** |
||
133 | * @return \eZ\Publish\API\Repository\Repository|\PHPUnit_Framework_MockObject_MockObject |
||
134 | */ |
||
135 | protected function getRepositoryMock() |
||
143 | |||
144 | /** |
||
145 | * Returns a persistence Handler mock. |
||
146 | * |
||
147 | * @return \eZ\Publish\SPI\Persistence\Handler|\PHPUnit_Framework_MockObject_MockObject |
||
148 | */ |
||
149 | protected function getPersistenceMock() |
||
207 | |||
208 | protected function getRelationProcessorMock() |
||
217 | |||
218 | /** |
||
219 | * Returns a SPI Handler mock. |
||
220 | * |
||
221 | * @param string $handler For instance "Content\\Type\\Handler" or "Search\\Handler", must be relative to "eZ\Publish\SPI" |
||
222 | * |
||
223 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
224 | */ |
||
225 | protected function getSPIMockHandler($handler) |
||
239 | |||
240 | /** |
||
241 | * Returns a persistence Handler mock. |
||
242 | * |
||
243 | * @param string $handler For instance "Content\\Type\\Handler", must be relative to "eZ\Publish\SPI\Persistence" |
||
244 | * |
||
245 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
246 | */ |
||
247 | protected function getPersistenceMockHandler($handler) |
||
251 | |||
252 | /** |
||
253 | * Returns User stub with $id as User/Content id. |
||
254 | * |
||
255 | * @param int $id |
||
256 | * |
||
257 | * @return \eZ\Publish\API\Repository\Values\User\User |
||
258 | */ |
||
259 | View Code Duplication | protected function getStubbedUser($id) |
|
276 | } |
||
277 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.