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 /** MicroView */ |
||
22 | abstract class View implements IView |
||
23 | { |
||
24 | /** @var array $styleScripts */ |
||
25 | public $styleScripts = []; |
||
26 | /** @var bool $asWidget */ |
||
27 | public $asWidget = false; |
||
28 | /** @var array $params */ |
||
29 | public $params = []; |
||
30 | /** @var array $stack */ |
||
31 | public $stack = []; |
||
32 | /** @var Module $module */ |
||
33 | public $module; |
||
34 | /** @var IContainer $container */ |
||
35 | public $container; |
||
36 | |||
37 | /** |
||
38 | * @param IContainer $container |
||
39 | */ |
||
40 | public function __construct(IContainer $container) |
||
44 | |||
45 | /** |
||
46 | * @inheritdoc |
||
47 | */ |
||
48 | public function addParameter($name, $value) |
||
52 | |||
53 | /** |
||
54 | * @inheritdoc |
||
55 | */ |
||
56 | public function widget($name, array $options = [], $capture = false) |
||
57 | { |
||
58 | if (!class_exists($name)) { |
||
59 | throw new Exception('Widget ' . $name . ' not found.'); |
||
60 | } |
||
61 | |||
62 | $options = array_merge($options, ['container' => $this->container]); |
||
63 | |||
64 | /** @var \Micro\mvc\Widget $widget widget */ |
||
65 | $widget = new $name($options); |
||
66 | $widget->init(); |
||
67 | |||
68 | if ($capture) { |
||
69 | ob_start(); |
||
70 | $widget->run(); |
||
71 | $result = ob_get_clean(); |
||
72 | } else { |
||
73 | $result = $widget->run(); |
||
74 | } |
||
75 | |||
76 | View Code Duplication | if ($result instanceof PhpView) { |
|
|
|||
77 | $result->asWidget = true; |
||
78 | $result->path = get_class($widget); |
||
79 | |||
80 | $result = $result->render(); |
||
81 | } |
||
82 | |||
83 | unset($widget); |
||
84 | |||
85 | if ($capture) { |
||
86 | return $result; |
||
87 | } |
||
88 | |||
89 | echo $result; |
||
90 | |||
91 | return ''; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @inheritdoc |
||
96 | */ |
||
97 | public function beginWidget($name, array $options = []) |
||
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | public function endWidget($name = '') |
||
156 | |||
157 | /** |
||
158 | * @inheritdoc |
||
159 | */ |
||
160 | public function registerScript($source, $isHead = true) |
||
167 | |||
168 | /** |
||
169 | * @inheritdoc |
||
170 | */ |
||
171 | public function registerScriptFile($source, $isHead = true) |
||
178 | |||
179 | /** |
||
180 | * @inheritdoc |
||
181 | */ |
||
182 | public function registerCss($source, $isHead = true) |
||
189 | |||
190 | /** |
||
191 | * @inheritdoc |
||
192 | */ |
||
193 | public function registerCssFile($source, $isHead = true) |
||
200 | |||
201 | /** |
||
202 | * Insert styles and scripts into cache |
||
203 | * |
||
204 | * @access protected |
||
205 | * |
||
206 | * @param string $cache cache of generated page |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | protected function insertStyleScripts($cache) |
||
235 | } |
||
236 |
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.