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 |
||
29 | class IntegrationTestCase extends CakeIntegrationTestCase |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * Default plugin. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $_plugin = 'Core'; |
||
38 | |||
39 | /** |
||
40 | * Core plugin. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $_corePlugin = 'Core'; |
||
45 | |||
46 | /** |
||
47 | * Default table name. |
||
48 | * |
||
49 | * @var null|string |
||
50 | */ |
||
51 | protected $_defaultTable; |
||
52 | |||
53 | /** |
||
54 | * Default url. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $_url = [ |
||
59 | 'prefix' => null, |
||
60 | 'plugin' => 'Core', |
||
61 | 'action' => '', |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * Setup the test case, backup the static object values so they can be restored. |
||
66 | * Specifically backs up the contents of Configure and paths in App if they have |
||
67 | * not already been backed up. |
||
68 | * |
||
69 | * @return void |
||
70 | */ |
||
71 | View Code Duplication | public function setUp() |
|
99 | |||
100 | /** |
||
101 | * Clears the state used for requests. |
||
102 | * |
||
103 | * @return void |
||
104 | */ |
||
105 | View Code Duplication | public function tearDown() |
|
117 | |||
118 | /** |
||
119 | * Prepare url. |
||
120 | * |
||
121 | * @param array $url |
||
122 | * @return array |
||
123 | */ |
||
124 | protected function _getUrl(array $url = []) |
||
128 | |||
129 | /** |
||
130 | * Get table object. |
||
131 | * |
||
132 | * @param null|string $name |
||
133 | * @return \Cake\ORM\Table |
||
134 | */ |
||
135 | protected function _getTable($name = null) |
||
140 | } |
||
141 |
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.