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 |
||
20 | class PhpunitController extends \hidev\base\Controller |
||
21 | { |
||
22 | protected $_before = ['phpunit.xml.dist']; |
||
23 | |||
24 | public $force; |
||
25 | |||
26 | public function getComponent() |
||
30 | |||
31 | /** |
||
32 | * Generates `tests/_bootstrap.php` and runs tests. |
||
33 | */ |
||
34 | public function actionIndex() |
||
40 | |||
41 | protected function doRun() |
||
47 | |||
48 | /** |
||
49 | * Generates skeleton class for fake. |
||
50 | */ |
||
51 | 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) |
|
85 | |||
86 | protected static function prepareFile($file) |
||
90 | |||
91 | protected function genSkel($file) |
||
104 | |||
105 | protected function buildNamespace($dir = '') |
||
109 | |||
110 | protected function buildTestNamespace($dir = 'tests\\unit') |
||
114 | |||
115 | protected function buildClass($file, $dir = '', $postfix = '') |
||
119 | |||
120 | protected function buildTestClass($file, $dir = 'tests\\unit', $postfix = 'Test') |
||
124 | |||
125 | protected function buildPath($file, $dir = 'src', $prefix = '', $postfix = '') |
||
132 | |||
133 | protected function buildTestPath($file, $dir = 'tests/unit', $prefix = '', $postfix = 'Test') |
||
137 | |||
138 | protected function buildFakePath($file, $dir = 'tests/unit', $prefix = 'Fake', $postfix = '') |
||
142 | } |
||
143 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: