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 |
||
19 | class PhpunitController extends \hidev\base\Controller |
||
20 | { |
||
21 | protected $_before = ['phpunit.xml.dist']; |
||
22 | |||
23 | public $force; |
||
24 | |||
25 | public function getComponent() |
||
29 | |||
30 | /** |
||
31 | * Generates `tests/_bootstrap.php` and runs tests. |
||
32 | */ |
||
33 | public function actionIndex() |
||
39 | |||
40 | protected function doRun() |
||
46 | |||
47 | /** |
||
48 | * Generates skeleton class for fake. |
||
49 | */ |
||
50 | View Code Duplication | public function actionGenfake($file) |
|
61 | |||
62 | protected function genFake($file, $path) |
||
71 | |||
72 | /** |
||
73 | * Generates skeleton class for test. |
||
74 | */ |
||
75 | View Code Duplication | public function actionGentest($file) |
|
86 | |||
87 | protected static function prepareFile($file) |
||
91 | |||
92 | protected function genSkel($file) |
||
105 | |||
106 | protected function buildNamespace($dir = '') |
||
110 | |||
111 | protected function buildTestNamespace($dir = 'tests\\unit') |
||
115 | |||
116 | protected function buildClass($file, $dir = '', $postfix = '') |
||
120 | |||
121 | protected function buildTestClass($file, $dir = 'tests\\unit', $postfix = 'Test') |
||
125 | |||
126 | protected function buildPath($file, $dir = 'src', $prefix = '', $postfix = '') |
||
133 | |||
134 | protected function buildTestPath($file, $dir = 'tests/unit', $prefix = '', $postfix = 'Test') |
||
138 | |||
139 | protected function buildFakePath($file, $dir = 'tests/unit', $prefix = 'Fake', $postfix = '') |
||
143 | } |
||
144 |
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.