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 |
||
22 | View Code Duplication | class Create extends BaseAction |
|
|
|||
23 | { |
||
24 | /** |
||
25 | * @var string the scenario to be assigned to the new model |
||
26 | */ |
||
27 | public $scenario = Model::SCENARIO_DEFAULT; |
||
28 | |||
29 | /** |
||
30 | * @var string name of the view |
||
31 | */ |
||
32 | public $view = 'create'; |
||
33 | |||
34 | /** |
||
35 | * @var string|array|callable a PHP callable that will be called to create the new model or class name |
||
36 | * The callable should return the new model instance. |
||
37 | */ |
||
38 | public $newModel; |
||
39 | |||
40 | /** |
||
41 | * @var string the name variable model in the view template |
||
42 | */ |
||
43 | public $nameVariableModel = 'model'; |
||
44 | |||
45 | /** |
||
46 | * Creates new model instance. |
||
47 | * @return ActiveRecord new model instance. |
||
48 | * @throws InvalidConfigException on invalid configuration. |
||
49 | */ |
||
50 | 18 | public function newModel() |
|
54 | 18 | ||
55 | /** |
||
56 | * Creates new record. |
||
57 | * @return mixed response |
||
58 | */ |
||
59 | 18 | public function run() |
|
80 | } |
||
81 |
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.