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 HandlerContentTest 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 HandlerContentTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class HandlerContentTest extends AbstractTestCase |
||
28 | { |
||
29 | /** |
||
30 | * Returns the content search handler to test. |
||
31 | * |
||
32 | * This method returns a fully functional search handler to perform tests |
||
33 | * on. |
||
34 | * |
||
35 | * @param array $fullTextSearchConfiguration |
||
36 | * |
||
37 | * @return \eZ\Publish\Core\Search\Legacy\Content\Handler |
||
38 | */ |
||
39 | protected function getContentSearchHandler(array $fullTextSearchConfiguration = []) |
||
40 | { |
||
41 | $transformationProcessor = new Persistence\TransformationProcessor\DefinitionBased( |
||
42 | new Persistence\TransformationProcessor\DefinitionBased\Parser(), |
||
43 | new Persistence\TransformationProcessor\PcreCompiler( |
||
44 | new Persistence\Utf8Converter() |
||
45 | ), |
||
46 | glob(__DIR__ . '/../../../../Persistence/Tests/TransformationProcessor/_fixtures/transformations/*.tr') |
||
47 | ); |
||
48 | $commaSeparatedCollectionValueHandler = new Content\Common\Gateway\CriterionHandler\FieldValue\Handler\Collection( |
||
49 | $this->getDatabaseHandler(), |
||
50 | $transformationProcessor, |
||
51 | ',' |
||
52 | ); |
||
53 | $hyphenSeparatedCollectionValueHandler = new Content\Common\Gateway\CriterionHandler\FieldValue\Handler\Collection( |
||
54 | $this->getDatabaseHandler(), |
||
55 | $transformationProcessor, |
||
56 | '-' |
||
57 | ); |
||
58 | $simpleValueHandler = new Content\Common\Gateway\CriterionHandler\FieldValue\Handler\Simple( |
||
59 | $this->getDatabaseHandler(), |
||
60 | $transformationProcessor |
||
61 | ); |
||
62 | $compositeValueHandler = new Content\Common\Gateway\CriterionHandler\FieldValue\Handler\Composite( |
||
63 | $this->getDatabaseHandler(), |
||
64 | $transformationProcessor |
||
65 | ); |
||
66 | |||
67 | return new Content\Handler( |
||
68 | new Content\Gateway\DoctrineDatabase( |
||
69 | $this->getDatabaseHandler(), |
||
70 | new Content\Common\Gateway\CriteriaConverter( |
||
71 | [ |
||
72 | new Content\Common\Gateway\CriterionHandler\ContentId( |
||
73 | $this->getDatabaseHandler() |
||
74 | ), |
||
75 | new Content\Common\Gateway\CriterionHandler\LogicalNot( |
||
76 | $this->getDatabaseHandler() |
||
77 | ), |
||
78 | new Content\Common\Gateway\CriterionHandler\LogicalAnd( |
||
79 | $this->getDatabaseHandler() |
||
80 | ), |
||
81 | new Content\Common\Gateway\CriterionHandler\LogicalOr( |
||
82 | $this->getDatabaseHandler() |
||
83 | ), |
||
84 | new Content\Gateway\CriterionHandler\Subtree( |
||
85 | $this->getDatabaseHandler() |
||
86 | ), |
||
87 | new Content\Common\Gateway\CriterionHandler\ContentTypeId( |
||
88 | $this->getDatabaseHandler() |
||
89 | ), |
||
90 | new Content\Common\Gateway\CriterionHandler\ContentTypeIdentifier( |
||
91 | $this->getDatabaseHandler(), |
||
92 | $this->getContentTypeHandler() |
||
93 | ), |
||
94 | new Content\Common\Gateway\CriterionHandler\ContentTypeGroupId( |
||
95 | $this->getDatabaseHandler() |
||
96 | ), |
||
97 | new Content\Common\Gateway\CriterionHandler\DateMetadata( |
||
98 | $this->getDatabaseHandler() |
||
99 | ), |
||
100 | new Content\Gateway\CriterionHandler\LocationId( |
||
101 | $this->getDatabaseHandler() |
||
102 | ), |
||
103 | new Content\Gateway\CriterionHandler\ParentLocationId( |
||
104 | $this->getDatabaseHandler() |
||
105 | ), |
||
106 | new Content\Common\Gateway\CriterionHandler\RemoteId( |
||
107 | $this->getDatabaseHandler() |
||
108 | ), |
||
109 | new Content\Gateway\CriterionHandler\LocationRemoteId( |
||
110 | $this->getDatabaseHandler() |
||
111 | ), |
||
112 | new Content\Common\Gateway\CriterionHandler\SectionId( |
||
113 | $this->getDatabaseHandler() |
||
114 | ), |
||
115 | new Content\Common\Gateway\CriterionHandler\FullText( |
||
116 | $this->getDatabaseHandler(), |
||
117 | $transformationProcessor, |
||
118 | $this->getLanguageMaskGenerator(), |
||
119 | $fullTextSearchConfiguration |
||
120 | ), |
||
121 | new Content\Common\Gateway\CriterionHandler\Field( |
||
122 | $this->getDatabaseHandler(), |
||
123 | $this->getContentTypeHandler(), |
||
124 | $this->getLanguageHandler(), |
||
125 | $this->getConverterRegistry(), |
||
126 | new Content\Common\Gateway\CriterionHandler\FieldValue\Converter( |
||
127 | new Content\Common\Gateway\CriterionHandler\FieldValue\HandlerRegistry( |
||
128 | [ |
||
129 | 'ezboolean' => $simpleValueHandler, |
||
130 | 'ezcountry' => $commaSeparatedCollectionValueHandler, |
||
131 | 'ezdate' => $simpleValueHandler, |
||
132 | 'ezdatetime' => $simpleValueHandler, |
||
133 | 'ezemail' => $simpleValueHandler, |
||
134 | 'ezinteger' => $simpleValueHandler, |
||
135 | 'ezobjectrelation' => $simpleValueHandler, |
||
136 | 'ezobjectrelationlist' => $commaSeparatedCollectionValueHandler, |
||
137 | 'ezselection' => $hyphenSeparatedCollectionValueHandler, |
||
138 | 'eztime' => $simpleValueHandler, |
||
139 | ] |
||
140 | ), |
||
141 | $compositeValueHandler |
||
142 | ), |
||
143 | $transformationProcessor |
||
144 | ), |
||
145 | new Content\Common\Gateway\CriterionHandler\ObjectStateId( |
||
146 | $this->getDatabaseHandler() |
||
147 | ), |
||
148 | new Content\Common\Gateway\CriterionHandler\LanguageCode( |
||
149 | $this->getDatabaseHandler(), |
||
150 | $this->getLanguageMaskGenerator() |
||
151 | ), |
||
152 | new Content\Gateway\CriterionHandler\Visibility( |
||
153 | $this->getDatabaseHandler() |
||
154 | ), |
||
155 | new Content\Common\Gateway\CriterionHandler\MatchAll( |
||
156 | $this->getDatabaseHandler() |
||
157 | ), |
||
158 | new Content\Common\Gateway\CriterionHandler\UserMetadata( |
||
159 | $this->getDatabaseHandler() |
||
160 | ), |
||
161 | new Content\Common\Gateway\CriterionHandler\FieldRelation( |
||
162 | $this->getDatabaseHandler(), |
||
163 | $this->getContentTypeHandler(), |
||
164 | $this->getLanguageHandler() |
||
165 | ), |
||
166 | ] |
||
167 | ), |
||
168 | new Content\Common\Gateway\SortClauseConverter( |
||
169 | [ |
||
170 | new Content\Common\Gateway\SortClauseHandler\ContentId($this->getDatabaseHandler()), |
||
171 | ] |
||
172 | ), |
||
173 | $this->getLanguageHandler() |
||
174 | ), |
||
175 | $this->createMock(LocationGateway::class), |
||
176 | new Content\WordIndexer\Gateway\DoctrineDatabase( |
||
177 | $this->getDatabaseHandler(), |
||
178 | $this->getContentTypeHandler(), |
||
179 | $this->getDefinitionBasedTransformationProcessor(), |
||
180 | new Content\WordIndexer\Repository\SearchIndex($this->getDatabaseHandler()), |
||
181 | $this->getLanguageMaskGenerator(), |
||
182 | $this->getFullTextSearchConfiguration() |
||
183 | ), |
||
184 | $this->getContentMapperMock(), |
||
185 | $this->createMock(LocationMapper::class), |
||
186 | $this->getLanguageHandler(), |
||
187 | $this->getFullTextMapper($this->getContentTypeHandler()) |
||
|
|||
188 | ); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Returns a content mapper mock. |
||
193 | * |
||
194 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\Mapper |
||
195 | */ |
||
196 | protected function getContentMapperMock() |
||
197 | { |
||
198 | $mapperMock = $this->getMockBuilder(ContentMapper::class) |
||
199 | ->setConstructorArgs( |
||
200 | [ |
||
201 | $this->getConverterRegistry(), |
||
202 | $this->getLanguageHandler(), |
||
203 | ] |
||
204 | ) |
||
205 | ->setMethods(['extractContentInfoFromRows']) |
||
206 | ->getMock(); |
||
207 | $mapperMock->expects($this->any()) |
||
208 | ->method('extractContentInfoFromRows') |
||
209 | ->with($this->isType('array')) |
||
210 | ->will( |
||
211 | $this->returnCallback( |
||
212 | function ($rows) { |
||
213 | $contentInfoObjs = []; |
||
214 | foreach ($rows as $row) { |
||
215 | $contentId = (int)$row['id']; |
||
216 | if (!isset($contentInfoObjs[$contentId])) { |
||
217 | $contentInfoObjs[$contentId] = new ContentInfo(); |
||
218 | $contentInfoObjs[$contentId]->id = $contentId; |
||
219 | } |
||
220 | } |
||
221 | |||
222 | return array_values($contentInfoObjs); |
||
223 | } |
||
224 | ) |
||
225 | ); |
||
226 | |||
227 | return $mapperMock; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Returns a content field handler mock. |
||
232 | * |
||
233 | * @return \eZ\Publish\Core\Persistence\Legacy\Content\FieldHandler |
||
234 | */ |
||
235 | protected function getContentFieldHandlerMock() |
||
236 | { |
||
237 | return $this->getMockBuilder(FieldHandler::class) |
||
238 | ->disableOriginalConstructor() |
||
239 | ->setMethods(['loadExternalFieldData']) |
||
240 | ->getMock(); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Bug #80. |
||
245 | */ |
||
246 | public function testFindWithoutOffsetLimit() |
||
247 | { |
||
248 | $locator = $this->getContentSearchHandler(); |
||
249 | |||
250 | $result = $locator->findContent( |
||
251 | new Query( |
||
252 | [ |
||
253 | 'filter' => new Criterion\ContentId(10), |
||
254 | ] |
||
255 | ) |
||
256 | ); |
||
257 | |||
258 | $this->assertEquals( |
||
259 | 1, |
||
260 | $result->totalCount |
||
261 | ); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Bug #81, bug #82. |
||
266 | */ |
||
267 | View Code Duplication | public function testFindWithZeroLimit() |
|
268 | { |
||
269 | $locator = $this->getContentSearchHandler(); |
||
270 | |||
271 | $result = $locator->findContent( |
||
272 | new Query( |
||
273 | [ |
||
274 | 'filter' => new Criterion\ContentId(10), |
||
275 | 'offset' => 0, |
||
276 | 'limit' => 0, |
||
277 | ] |
||
278 | ) |
||
279 | ); |
||
280 | |||
281 | $this->assertEquals( |
||
282 | 1, |
||
283 | $result->totalCount |
||
284 | ); |
||
285 | $this->assertEquals( |
||
286 | [], |
||
287 | $result->searchHits |
||
288 | ); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Issue with PHP_MAX_INT limit overflow in databases. |
||
293 | */ |
||
294 | public function testFindWithNullLimit() |
||
295 | { |
||
296 | $locator = $this->getContentSearchHandler(); |
||
297 | |||
298 | $result = $locator->findContent( |
||
299 | new Query( |
||
300 | [ |
||
301 | 'filter' => new Criterion\ContentId(10), |
||
302 | 'offset' => 0, |
||
303 | 'limit' => null, |
||
304 | ] |
||
305 | ) |
||
306 | ); |
||
307 | |||
308 | $this->assertEquals( |
||
309 | 1, |
||
310 | $result->totalCount |
||
311 | ); |
||
312 | $this->assertCount( |
||
313 | 1, |
||
314 | $result->searchHits |
||
315 | ); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Issue with offsetting to the nonexistent results produces \ezcQueryInvalidParameterException exception. |
||
320 | */ |
||
321 | public function testFindWithOffsetToNonexistent() |
||
322 | { |
||
323 | $locator = $this->getContentSearchHandler(); |
||
324 | |||
325 | $result = $locator->findContent( |
||
326 | new Query( |
||
327 | [ |
||
328 | 'filter' => new Criterion\ContentId(10), |
||
329 | 'offset' => 1000, |
||
330 | 'limit' => null, |
||
331 | ] |
||
332 | ) |
||
333 | ); |
||
334 | |||
335 | $this->assertEquals( |
||
336 | 1, |
||
337 | $result->totalCount |
||
338 | ); |
||
339 | $this->assertCount( |
||
340 | 0, |
||
341 | $result->searchHits |
||
342 | ); |
||
343 | } |
||
344 | |||
345 | public function testFindSingle() |
||
346 | { |
||
347 | $locator = $this->getContentSearchHandler(); |
||
348 | |||
349 | $contentInfo = $locator->findSingle(new Criterion\ContentId(10)); |
||
350 | |||
351 | $this->assertEquals(10, $contentInfo->id); |
||
352 | } |
||
353 | |||
354 | public function testFindSingleWithNonSearchableField() |
||
355 | { |
||
356 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class); |
||
357 | |||
358 | $locator = $this->getContentSearchHandler(); |
||
359 | $locator->findSingle( |
||
360 | new Criterion\Field( |
||
361 | 'tag_cloud_url', |
||
362 | Criterion\Operator::EQ, |
||
363 | 'http://nimbus.com' |
||
364 | ) |
||
365 | ); |
||
366 | } |
||
367 | |||
368 | public function testFindContentWithNonSearchableField() |
||
369 | { |
||
370 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class); |
||
371 | |||
372 | $locator = $this->getContentSearchHandler(); |
||
373 | $locator->findContent( |
||
374 | new Query( |
||
375 | [ |
||
376 | 'filter' => new Criterion\Field( |
||
377 | 'tag_cloud_url', |
||
378 | Criterion\Operator::EQ, |
||
379 | 'http://nimbus.com' |
||
380 | ), |
||
381 | 'sortClauses' => [new SortClause\ContentId()], |
||
382 | ] |
||
383 | ) |
||
384 | ); |
||
385 | } |
||
386 | |||
387 | public function testFindSingleTooMany() |
||
388 | { |
||
389 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class); |
||
390 | |||
391 | $locator = $this->getContentSearchHandler(); |
||
392 | $locator->findSingle(new Criterion\ContentId([4, 10, 12, 23])); |
||
393 | } |
||
394 | |||
395 | public function testFindSingleZero() |
||
396 | { |
||
397 | $this->expectException(NotFoundException::class); |
||
398 | |||
399 | $locator = $this->getContentSearchHandler(); |
||
400 | $locator->findSingle(new Criterion\ContentId(0)); |
||
401 | } |
||
402 | |||
403 | public function testContentIdFilter() |
||
419 | |||
420 | public function testContentIdFilterCount() |
||
437 | |||
438 | public function testContentAndCombinatorFilter() |
||
461 | |||
462 | public function testContentOrCombinatorFilter() |
||
497 | |||
498 | public function testContentNotCombinatorFilter() |
||
523 | |||
524 | View Code Duplication | public function testContentSubtreeFilterIn() |
|
540 | |||
541 | View Code Duplication | public function testContentSubtreeFilterEq() |
|
555 | |||
556 | View Code Duplication | public function testContentTypeIdFilter() |
|
570 | |||
571 | public function testContentTypeIdentifierFilter() |
||
586 | |||
587 | View Code Duplication | public function testContentTypeGroupFilter() |
|
601 | |||
602 | public function testDateMetadataFilterModifiedGreater() |
||
620 | |||
621 | public function testDateMetadataFilterModifiedGreaterOrEqual() |
||
639 | |||
640 | public function testDateMetadataFilterModifiedIn() |
||
658 | |||
659 | public function testDateMetadataFilterModifiedBetween() |
||
677 | |||
678 | public function testDateMetadataFilterCreatedBetween() |
||
696 | |||
697 | public function testLocationIdFilter() |
||
711 | |||
712 | public function testParentLocationIdFilter() |
||
726 | |||
727 | public function testRemoteIdFilter() |
||
743 | |||
744 | public function testLocationRemoteIdFilter() |
||
760 | |||
761 | View Code Duplication | public function testSectionFilter() |
|
775 | |||
776 | public function testStatusFilter() |
||
801 | |||
802 | public function testFieldFilter() |
||
820 | |||
821 | View Code Duplication | public function testFieldFilterIn() |
|
822 | { |
||
823 | $this->assertSearchResults( |
||
824 | [11, 42], |
||
825 | $this->getContentSearchHandler()->findContent( |
||
826 | new Query( |
||
827 | [ |
||
828 | 'filter' => new Criterion\Field( |
||
829 | 'name', |
||
830 | Criterion\Operator::IN, |
||
831 | ['members', 'anonymous users'] |
||
832 | ), |
||
833 | 'limit' => 10, |
||
834 | ] |
||
835 | ) |
||
836 | ) |
||
837 | ); |
||
838 | } |
||
839 | |||
840 | public function testFieldFilterContainsPartial() |
||
858 | |||
859 | public function testFieldFilterContainsSimple() |
||
877 | |||
878 | public function testFieldFilterContainsSimpleNoMatch() |
||
896 | |||
897 | View Code Duplication | public function testFieldFilterBetween() |
|
898 | { |
||
899 | $this->assertSearchResults( |
||
900 | [186, 187], |
||
901 | $this->getContentSearchHandler()->findContent( |
||
902 | new Query( |
||
903 | [ |
||
904 | 'filter' => new Criterion\Field( |
||
905 | 'publication_date', |
||
906 | Criterion\Operator::BETWEEN, |
||
907 | [1190000000, 1200000000] |
||
908 | ), |
||
909 | 'limit' => 10, |
||
910 | ] |
||
911 | ) |
||
912 | ) |
||
913 | ); |
||
914 | } |
||
915 | |||
916 | View Code Duplication | public function testFieldFilterOr() |
|
943 | |||
944 | View Code Duplication | public function testFullTextFilter() |
|
958 | |||
959 | View Code Duplication | public function testFullTextWildcardFilter() |
|
973 | |||
974 | View Code Duplication | public function testFullTextDisabledWildcardFilter() |
|
990 | |||
991 | View Code Duplication | public function testFullTextFilterStopwordRemoval() |
|
1011 | |||
1012 | public function testFullTextFilterNoStopwordRemoval() |
||
1013 | { |
||
1014 | $handler = $this->getContentSearchHandler( |
||
1015 | [ |
||
1016 | 'stopWordThresholdFactor' => 1, |
||
1017 | ] |
||
1018 | ); |
||
1019 | |||
1020 | $result = $handler->findContent( |
||
1021 | new Query( |
||
1022 | [ |
||
1023 | 'filter' => new Criterion\FullText( |
||
1024 | 'the' |
||
1025 | ), |
||
1026 | 'limit' => 10, |
||
1027 | ] |
||
1028 | ) |
||
1029 | ); |
||
1030 | |||
1031 | $this->assertCount( |
||
1032 | 10, |
||
1033 | |||
1034 | array_map( |
||
1035 | function ($hit) { |
||
1036 | return $hit->valueObject->id; |
||
1037 | }, |
||
1038 | $result->searchHits |
||
1039 | ) |
||
1040 | ); |
||
1041 | } |
||
1042 | |||
1043 | public function testFullTextFilterInvalidStopwordThreshold() |
||
1044 | { |
||
1045 | $this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class); |
||
1046 | |||
1047 | $this->getContentSearchHandler( |
||
1048 | [ |
||
1049 | 'stopWordThresholdFactor' => 2, |
||
1050 | ] |
||
1051 | ); |
||
1052 | } |
||
1053 | |||
1054 | public function testObjectStateIdFilter() |
||
1069 | |||
1070 | public function testObjectStateIdFilterIn() |
||
1085 | |||
1086 | public function testLanguageCodeFilter() |
||
1101 | |||
1102 | public function testLanguageCodeFilterIn() |
||
1117 | |||
1118 | View Code Duplication | public function testLanguageCodeFilterWithAlwaysAvailable() |
|
1133 | |||
1134 | public function testVisibilityFilter() |
||
1151 | |||
1152 | View Code Duplication | public function testUserMetadataFilterOwnerWrongUserId() |
|
1169 | |||
1170 | View Code Duplication | public function testUserMetadataFilterOwnerAdministrator() |
|
1189 | |||
1190 | View Code Duplication | public function testUserMetadataFilterOwnerEqAMember() |
|
1207 | |||
1208 | View Code Duplication | public function testUserMetadataFilterOwnerInAMember() |
|
1225 | |||
1226 | View Code Duplication | public function testUserMetadataFilterCreatorEqAMember() |
|
1243 | |||
1244 | View Code Duplication | public function testUserMetadataFilterCreatorInAMember() |
|
1261 | |||
1262 | View Code Duplication | public function testUserMetadataFilterEqGroupMember() |
|
1279 | |||
1280 | View Code Duplication | public function testUserMetadataFilterInGroupMember() |
|
1297 | |||
1298 | View Code Duplication | public function testUserMetadataFilterEqGroupMemberNoMatch() |
|
1315 | |||
1316 | View Code Duplication | public function testUserMetadataFilterInGroupMemberNoMatch() |
|
1333 | |||
1334 | public function testFieldRelationFilterContainsSingle() |
||
1351 | |||
1352 | View Code Duplication | public function testFieldRelationFilterContainsSingleNoMatch() |
|
1369 | |||
1370 | public function testFieldRelationFilterContainsArray() |
||
1387 | |||
1388 | public function testFieldRelationFilterContainsArrayNotMatch() |
||
1405 | |||
1406 | public function testFieldRelationFilterInArray() |
||
1423 | |||
1424 | public function testFieldRelationFilterInArrayNotMatch() |
||
1441 | |||
1442 | public function testGetNonExistingFieldDefinition(): void |
||
1443 | { |
||
1444 | $this->expectException(NotFoundException::class); |
||
1445 | |||
1446 | $this->getContentTypeHandler()->getFieldDefinition(0, Type::STATUS_DEFINED); |
||
1447 | } |
||
1448 | } |
||
1449 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.