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 |
||
| 28 | class TestCase extends CakeTestCase |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Default plugin name. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $_plugin = 'Core'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Core plugin. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $_corePlugin = 'Core'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Setup the test case, backup the static object values so they can be restored. |
||
| 47 | * Specifically backs up the contents of Configure and paths in App if they have |
||
| 48 | * not already been backed up. |
||
| 49 | * |
||
| 50 | * @return void |
||
| 51 | */ |
||
| 52 | public function setUp() |
||
| 53 | { |
||
| 54 | parent::setUp(); |
||
| 55 | |||
| 56 | if ($this->_plugin !== $this->_corePlugin) { |
||
| 57 | $options = [ |
||
| 58 | 'bootstrap' => true, |
||
| 59 | 'routes' => true, |
||
| 60 | 'autoload' => true, |
||
| 61 | ]; |
||
| 62 | |||
| 63 | Plugin::load($this->_plugin, $options); |
||
| 64 | Plugin::routes($this->_plugin); |
||
| 65 | } |
||
| 66 | |||
| 67 | View Code Duplication | if (!Plugin::loaded($this->_corePlugin)) { |
|
|
|
|||
| 68 | $loadParams = [ |
||
| 69 | 'bootstrap' => true, |
||
| 70 | 'routes' => true, |
||
| 71 | 'path' => ROOT . DS, |
||
| 72 | ]; |
||
| 73 | |||
| 74 | Plugin::load($this->_corePlugin, $loadParams); |
||
| 75 | Plugin::routes($this->_corePlugin); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Clears the state used for requests. |
||
| 81 | * |
||
| 82 | * @return void |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function tearDown() |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Assert check is empty array. |
||
| 96 | * |
||
| 97 | * @param array $array |
||
| 98 | */ |
||
| 99 | public function assertIsEmptyArray(array $array) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $string |
||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | protected function _getStrArray($string) |
||
| 119 | } |
||
| 120 |
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.