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 |
||
32 | abstract class CacheTestCase extends TestCase |
||
33 | { |
||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $cacheDirectory; |
||
38 | |||
39 | /** |
||
40 | * @param Adapter $adapter |
||
41 | * @param Extractor $annotationExtractor |
||
42 | * @param Generator $asserterGenerator |
||
43 | * @param Manager $assertionManager |
||
44 | * @param \Closure $reflectionClassFactory |
||
45 | * @param \Closure $phpExtensionFactory |
||
46 | * @param Analyzer $analyzer |
||
47 | */ |
||
48 | View Code Duplication | public function __construct( |
|
69 | |||
70 | /** |
||
71 | * @return CacheInterface |
||
72 | */ |
||
73 | abstract protected function createCache(); |
||
74 | |||
75 | /** |
||
76 | * Create the cache directory. |
||
77 | */ |
||
78 | public function setUp() |
||
84 | |||
85 | /** |
||
86 | * Remove the cache directory. |
||
87 | */ |
||
88 | public function tearDown() |
||
92 | |||
93 | /** |
||
94 | * Test Load method. |
||
95 | */ |
||
96 | public function testLoad() |
||
106 | |||
107 | /** |
||
108 | * Test Save method. |
||
109 | */ |
||
110 | public function testSave() |
||
122 | } |
||
123 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.