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 |
||
30 | class TestCase extends CakeTestCase |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * Default plugin name. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $_plugin = 'Core'; |
||
39 | |||
40 | /** |
||
41 | * Core plugin. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $_corePlugin = 'Core'; |
||
46 | |||
47 | /** |
||
48 | * Default table name. |
||
49 | * |
||
50 | * @var null|string |
||
51 | */ |
||
52 | protected $_defaultTable; |
||
53 | |||
54 | /** |
||
55 | * Hold CMS object. |
||
56 | * |
||
57 | * @var Cms |
||
58 | */ |
||
59 | protected $_cms; |
||
60 | |||
61 | /** |
||
62 | * Setup the test case, backup the static object values so they can be restored. |
||
63 | * Specifically backs up the contents of Configure and paths in App if they have |
||
64 | * not already been backed up. |
||
65 | * |
||
66 | * @return void |
||
67 | */ |
||
68 | View Code Duplication | public function setUp() |
|
96 | |||
97 | /** |
||
98 | * Clears the state used for requests. |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | View Code Duplication | public function tearDown() |
|
111 | |||
112 | /** |
||
113 | * Assert check is empty array. |
||
114 | * |
||
115 | * @param array $array |
||
116 | */ |
||
117 | public static function assertIsEmptyArray(array $array) |
||
121 | |||
122 | /** |
||
123 | * Check error validation. |
||
124 | * |
||
125 | * @param mixed $field |
||
126 | * @param mixed $value |
||
127 | * @param array $errorExpected |
||
128 | */ |
||
129 | public function assertFieldErrorValidation($field, $value, array $errorExpected = []) |
||
140 | |||
141 | /** |
||
142 | * Get table object. |
||
143 | * |
||
144 | * @param null|string $name |
||
145 | * @return \Cake\ORM\Table |
||
146 | */ |
||
147 | protected function _getTable($name = null) |
||
152 | |||
153 | /** |
||
154 | * @param string $string |
||
155 | * @return array |
||
156 | */ |
||
157 | protected function _getStrArray($string) |
||
168 | } |
||
169 |
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.