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 RuleSetFactoryTest 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 RuleSetFactoryTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class RuleSetFactoryTest extends AbstractTest |
||
28 | { |
||
29 | /** |
||
30 | * Used to test files/directories access for ignore code rule |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | const DIR_UNDER_TESTS = 'designăôü0汉字'; |
||
35 | |||
36 | /** |
||
37 | * testCreateRuleSetFileNameFindsXmlFileInBundledRuleSets |
||
38 | * |
||
39 | * @return void |
||
40 | */ |
||
41 | public function testCreateRuleSetFileNameFindsXmlFileInBundledRuleSets() |
||
48 | |||
49 | /** |
||
50 | * testCreateRuleSetFileNameFindsXmlFileInCurrentWorkingDirectory |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | public function testCreateRuleSetFileNameFindsXmlFileInCurrentWorkingDirectory() |
||
63 | |||
64 | /** |
||
65 | * testCreateRuleSetsReturnsArray |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | public function testCreateRuleSetsReturnsArray() |
||
74 | |||
75 | /** |
||
76 | * testCreateRuleSetsForSingleFileReturnsArrayWithOneElement |
||
77 | * |
||
78 | * @return void |
||
79 | */ |
||
80 | public function testCreateRuleSetsForSingleFileReturnsArrayWithOneElement() |
||
85 | |||
86 | /** |
||
87 | * testCreateRuleSetsForSingleFileReturnsOneRuleSetInstance |
||
88 | * |
||
89 | * @return void |
||
90 | */ |
||
91 | public function testCreateRuleSetsForSingleFileReturnsOneRuleSetInstance() |
||
96 | |||
97 | /** |
||
98 | * testCreateRuleSetsConfiguresExpectedRuleSetName |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | public function testCreateRuleSetsConfiguresExpectedRuleSetName() |
||
107 | |||
108 | /** |
||
109 | * testCreateRuleSetsConfiguresExpectedRuleSetName |
||
110 | * |
||
111 | * @return void |
||
112 | */ |
||
113 | public function testCreateRuleSetsConfiguresExpectedRuleSetDescription() |
||
118 | |||
119 | /** |
||
120 | * testCreateRuleSetsForTwoFilesReturnsArrayWithTwoElements |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | public function testCreateRuleSetsForTwoFilesReturnsArrayWithTwoElements() |
||
132 | |||
133 | /** |
||
134 | * testCreateRuleSetsForTwoFilesReturnsExpectedRuleSetInstances |
||
135 | * |
||
136 | * @return void |
||
137 | */ |
||
138 | public function testCreateRuleSetsForTwoFilesReturnsExpectedRuleSetInstances() |
||
147 | |||
148 | /** |
||
149 | * testCreateRuleSetsForTwoConfiguresExpectedRuleSetNames |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | public function testCreateRuleSetsForTwoConfiguresExpectedRuleSetNames() |
||
162 | |||
163 | /** |
||
164 | * testCreateRuleSetsForTwoConfiguresExpectedRuleSetDescriptions |
||
165 | * |
||
166 | * @return void |
||
167 | */ |
||
168 | public function testCreateRuleSetsForTwoConfiguresExpectedRuleSetDescriptions() |
||
177 | |||
178 | /** |
||
179 | * testCreateRuleSetsForSingleLocalFileNameReturnsArray |
||
180 | * |
||
181 | * @return void |
||
182 | */ |
||
183 | public function testCreateRuleSetsForLocalFileNameReturnsArray() |
||
190 | |||
191 | /** |
||
192 | * testCreateRuleSetsForSingleLocalFileNameReturnsArrayWithOneElement |
||
193 | * |
||
194 | * @return void |
||
195 | */ |
||
196 | public function testCreateRuleSetsForLocalFileNameReturnsArrayWithOneElement() |
||
203 | |||
204 | /** |
||
205 | * testCreateRuleSetsForSingleLocalFileNameConfiguresExpectedRuleSetName |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | public function testCreateRuleSetsForLocalFileNameConfiguresExpectedRuleSetName() |
||
216 | |||
217 | /** |
||
218 | * testCreateRuleSetsWithReferenceContainsExpectedRuleSet |
||
219 | * |
||
220 | * @return void |
||
221 | */ |
||
222 | public function testCreateRuleSetsWithReferenceContainsExpectedRuleSet() |
||
229 | |||
230 | /** |
||
231 | * testCreateRuleSetsWithReferenceContainsExpectedNumberOfRules |
||
232 | * |
||
233 | * @return void |
||
234 | */ |
||
235 | public function testCreateRuleSetsWithReferenceContainsExpectedNumberOfRules() |
||
242 | |||
243 | /** |
||
244 | * testCreateRuleSetsForLocalFileWithRuleSetReferenceNodes |
||
245 | * |
||
246 | * @return void |
||
247 | */ |
||
248 | public function testCreateRuleSetsWithReferenceContainsRuleInstances() |
||
255 | |||
256 | /** |
||
257 | * testCreateRuleSetsWithReferenceContainsExpectedRules |
||
258 | * |
||
259 | * @return void |
||
260 | */ |
||
261 | public function testCreateRuleSetsWithReferenceContainsExpectedRules() |
||
276 | |||
277 | /** |
||
278 | * testCreateSingleRuleSetReturnsRuleSetInstance |
||
279 | * |
||
280 | * @return void |
||
281 | */ |
||
282 | public function testCreateSingleRuleSetReturnsRuleSetInstance() |
||
291 | |||
292 | /** |
||
293 | * Tests that the rule-set factory applies a set minimum priority filter correct. |
||
294 | * |
||
295 | * @return void |
||
296 | */ |
||
297 | public function testCreateRuleSetWithSpecifiedMinimumPriorityOnlyContainsMatchingRules() |
||
307 | |||
308 | /** |
||
309 | * Tests that the rule-set factory applies a set maximum priority filter correct. |
||
310 | * |
||
311 | * @return void |
||
312 | */ |
||
313 | public function testCreateRuleSetWithSpecifiedMaximumPriorityOnlyContainsMatchingRules() |
||
323 | |||
324 | /** |
||
325 | * Tests that the rule-set factory applies a set maximum priority filter correct. |
||
326 | * |
||
327 | * @return void |
||
328 | */ |
||
329 | public function testCreateRuleSetWithSpecifiedPrioritiesOnlyContainsMatchingRules() |
||
340 | |||
341 | /** |
||
342 | * testCreateRuleWithExcludePattern |
||
343 | * |
||
344 | * @return void |
||
345 | */ |
||
346 | public function testCreateRuleWithExcludePattern() |
||
360 | |||
361 | /** |
||
362 | * testCreateRuleSetsWithRuleReferenceThatOverwritesPrioritySetting |
||
363 | * |
||
364 | * @return void |
||
365 | */ |
||
366 | View Code Duplication | public function testCreateRuleSetsWithRuleReferenceThatOverwritesPrioritySetting() |
|
376 | |||
377 | /** |
||
378 | * testCreateRuleWithExpectedExample |
||
379 | * |
||
380 | * @return void |
||
381 | */ |
||
382 | View Code Duplication | public function testCreateRuleWithExpectedExample() |
|
392 | |||
393 | /** |
||
394 | * testCreateRuleWithExpectedMultipleExamples |
||
395 | * |
||
396 | * @return void |
||
397 | */ |
||
398 | public function testCreateRuleWithExpectedMultipleExamples() |
||
408 | |||
409 | /** |
||
410 | * testCreateRuleSetsWithRuleReferenceThatOverwritesDescriptionSetting |
||
411 | * |
||
412 | * @return void |
||
413 | */ |
||
414 | View Code Duplication | public function testCreateRuleSetsWithRuleReferenceThatOverwritesDescriptionSetting() |
|
424 | |||
425 | /** |
||
426 | * testCreateRuleSetsWithRuleReferenceThatOverwritesPropertySetting |
||
427 | * |
||
428 | * @return void |
||
429 | */ |
||
430 | View Code Duplication | public function testCreateRuleSetsWithRuleReferenceThatOverwritesPropertySetting() |
|
440 | |||
441 | /** |
||
442 | * testFactorySupportsAlternativeSyntaxForPropertyValue |
||
443 | * |
||
444 | * @return void |
||
445 | */ |
||
446 | View Code Duplication | public function testFactorySupportsAlternativeSyntaxForPropertyValue() |
|
456 | |||
457 | /** |
||
458 | * testCreateRuleSetsWithRuleReferenceThatOverwritesExamplesSetting |
||
459 | * |
||
460 | * @return void |
||
461 | */ |
||
462 | View Code Duplication | public function testCreateRuleSetsWithRuleReferenceThatOverwritesExamplesSetting() |
|
474 | |||
475 | /** |
||
476 | * testCreateRuleSetsWithRuleReferenceThatOverwritesExamplesSetting |
||
477 | * |
||
478 | * @return void |
||
479 | */ |
||
480 | View Code Duplication | public function testCreateRuleSetsWithRuleReferenceThatOverwritesNameSetting() |
|
490 | |||
491 | /** |
||
492 | * testCreateRuleSetsWithRuleReferenceThatOverwritesMessageSetting |
||
493 | * |
||
494 | * @return void |
||
495 | */ |
||
496 | View Code Duplication | public function testCreateRuleSetsWithRuleReferenceThatOverwritesMessageSetting() |
|
506 | |||
507 | /** |
||
508 | * testCreateRuleSetsWithRuleReferenceThatOverwritesExtInfoUrlSetting |
||
509 | * |
||
510 | * @return void |
||
511 | */ |
||
512 | View Code Duplication | public function testCreateRuleSetsWithRuleReferenceThatOverwritesExtInfoUrlSetting() |
|
522 | |||
523 | /** |
||
524 | * testCreateRuleSetsWithRuleReferenceNotContainsExcludedRule |
||
525 | * |
||
526 | * @return void |
||
527 | */ |
||
528 | View Code Duplication | public function testCreateRuleSetsWithRuleReferenceNotContainsExcludedRule() |
|
538 | |||
539 | /** |
||
540 | * testCreateRuleSetsWithRuleReferenceNotContainsExcludedRules |
||
541 | * |
||
542 | * @return void |
||
543 | */ |
||
544 | View Code Duplication | public function testCreateRuleSetsWithRuleReferenceNotContainsExcludedRules() |
|
554 | |||
555 | /** |
||
556 | * Tests that the factory throws the expected exception for an invalid ruleset |
||
557 | * identifier. |
||
558 | * |
||
559 | * @return void |
||
560 | * @covers \PHPMD\RuleSetNotFoundException |
||
561 | */ |
||
562 | public function testCreateRuleSetsThrowsExceptionForInvalidIdentifier() |
||
573 | |||
574 | /** |
||
575 | * Tests that the factory throws an exception when the source code filename |
||
576 | * for the configured rule does not exist. |
||
577 | * |
||
578 | * @return void |
||
579 | * @covers \PHPMD\RuleClassFileNotFoundException |
||
580 | */ |
||
581 | View Code Duplication | public function testCreateRuleSetsThrowsExceptionWhenClassFileNotInIncludePath() |
|
593 | |||
594 | /** |
||
595 | * Tests that the factory throws the expected exception when a rule class |
||
596 | * cannot be found. |
||
597 | * |
||
598 | * @return void |
||
599 | * @covers \PHPMD\RuleClassNotFoundException |
||
600 | */ |
||
601 | View Code Duplication | public function testCreateRuleSetThrowsExceptionWhenFileNotContainsClass() |
|
613 | |||
614 | /** |
||
615 | * Tests that the factory throws the expected exception when a rule class |
||
616 | * cannot be found. |
||
617 | * |
||
618 | * @return void |
||
619 | * @covers \PHPMD\RuleClassNotFoundException |
||
620 | * @expectedException \RuntimeException |
||
621 | */ |
||
622 | public function testCreateRuleSetsThrowsExpectedExceptionForInvalidXmlFile() |
||
629 | |||
630 | /** |
||
631 | * testCreateRuleSetsActivatesStrictModeOnRuleSet |
||
632 | * |
||
633 | * @return void |
||
634 | */ |
||
635 | public function testCreateRuleSetsActivatesStrictModeOnRuleSet() |
||
646 | |||
647 | /** |
||
648 | * Tests that adding an include-path via ruleset works. |
||
649 | * Also implicitly tests (by parsing the ruleset) that |
||
650 | * reference-by-includepath and explicit-classfile-declaration works. |
||
651 | * |
||
652 | * @return void |
||
653 | * @throws \Exception |
||
654 | */ |
||
655 | public function testAddPHPIncludePath() |
||
681 | |||
682 | /** |
||
683 | * Checks if PHPMD doesn't treat directories named as code rule as files |
||
684 | * |
||
685 | * @return void |
||
686 | * @link https://github.com/phpmd/phpmd/issues/47 |
||
687 | */ |
||
688 | public function testIfGettingRuleFilePathExcludeUnreadablePaths() |
||
713 | |||
714 | /** |
||
715 | * Checks the ruleset XML files provided with PHPMD all provide externalInfoUrls |
||
716 | * |
||
717 | * @param string $file The path to the ruleset xml to test |
||
718 | * @return void |
||
719 | * @dataProvider getDefaultRuleSets |
||
720 | */ |
||
721 | public function testDefaultRuleSetsProvideExternalInfoUrls($file) |
||
736 | |||
737 | /** |
||
738 | * Provides an array of the file paths to rule sets provided with PHPMD |
||
739 | * |
||
740 | * @return array |
||
741 | */ |
||
742 | public function getDefaultRuleSets() |
||
746 | |||
747 | /** |
||
748 | * Invokes the <b>createRuleSets()</b> of the {@link RuleSetFactory} |
||
749 | * class. |
||
750 | * |
||
751 | * @param string $file At least one rule configuration file name. You can |
||
752 | * also pass multiple parameters with ruleset configuration files. |
||
753 | * @return \PHPMD\RuleSet[] |
||
754 | */ |
||
755 | private function createRuleSetsFromAbsoluteFiles($file) |
||
762 | |||
763 | /** |
||
764 | * Invokes the <b>createRuleSets()</b> of the {@link RuleSetFactory} |
||
765 | * class. |
||
766 | * |
||
767 | * @param string $file At least one rule configuration file name. You can |
||
768 | * also pass multiple parameters with ruleset configuration files. |
||
769 | * @return \PHPMD\RuleSet[] |
||
770 | * @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter |
||
771 | */ |
||
772 | private function createRuleSetsFromFiles($file) |
||
780 | |||
781 | /** |
||
782 | * Sets up files and directories for XML rule file access test |
||
783 | * |
||
784 | * @return array Paths to test against |
||
785 | */ |
||
786 | public function getPathsForFileAccessTest() |
||
823 | } |
||
824 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.