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 |
||
15 | class ViewCollection implements |
||
16 | ConfigureInterface, |
||
17 | InjectionAwareInterface |
||
18 | { |
||
19 | use InjectionAwareTrait; |
||
20 | use ConfigureTrait { |
||
21 | configure as protected loadConfiguration; |
||
22 | } |
||
23 | |||
24 | |||
25 | |||
26 | /** @var [] $views Array for all views. */ |
||
27 | private $views = []; |
||
28 | |||
29 | |||
30 | |||
31 | /** |
||
32 | * Load and apply configurations. |
||
33 | * |
||
34 | * @param array|string $what is an array with key/value config options |
||
35 | * or a file to be included which returns such |
||
36 | * an array. |
||
37 | * |
||
38 | * @throws Anax\View\Exception when template file is missing |
||
39 | * |
||
40 | * @return string as path to the template file |
||
41 | */ |
||
42 | public function configure($what) |
||
51 | |||
52 | |||
53 | |||
54 | /** |
||
55 | * Convert template to path to template file. |
||
56 | * |
||
57 | * @param string $template the name of the template file to include |
||
58 | * |
||
59 | * @throws Anax\View\Exception when template file is missing |
||
60 | * |
||
61 | * @return string as path to the template file |
||
62 | */ |
||
63 | View Code Duplication | public function getTemplateFile($template) |
|
77 | |||
78 | |||
79 | |||
80 | /** |
||
81 | * Add (create) a view to be included, pass optional data and put the |
||
82 | * view in an optional specific region (default region is "main") and |
||
83 | * pass an optional sort value where the highest value is rendered first. |
||
84 | * The $template can be a: |
||
85 | * filename (string), |
||
86 | * callback (array with key callback set to a callable array), |
||
87 | * view array (key value array with template, data, region, sort) |
||
88 | * |
||
89 | * @param string $template the name of the template file to include. |
||
90 | * @param array $data variables to make available to the view, |
||
91 | * default is empty. |
||
92 | * @param string $region which region to attach the view, default is |
||
93 | * "main". |
||
94 | * @param integer $sort which order to display the views. |
||
95 | * |
||
96 | * @return self for chaining. |
||
97 | */ |
||
98 | View Code Duplication | public function add($template, $data = [], $region = "main", $sort = 0) |
|
133 | |||
134 | |||
135 | |||
136 | /** |
||
137 | * Add a callback to be rendered as a view. |
||
138 | * |
||
139 | * @param string $callback function to call to get the content of the view |
||
140 | * @param array $data variables to make available to the view, default is empty |
||
141 | * @param string $region which region to attach the view |
||
142 | * @param int $sort which order to display the views |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | View Code Duplication | public function addCallback($callback, $data = [], $region = "main", $sort = 0) |
|
154 | |||
155 | |||
156 | |||
157 | /** |
||
158 | * Add a string as a view. |
||
159 | * |
||
160 | * @param string $content the content |
||
161 | * @param string $region which region to attach the view |
||
162 | * @param int $sort which order to display the views |
||
163 | * |
||
164 | * @return $this |
||
165 | */ |
||
166 | View Code Duplication | public function addString($content, $region = "main", $sort = 0) |
|
174 | |||
175 | |||
176 | |||
177 | /** |
||
178 | * Check if a region has views to render. |
||
179 | * |
||
180 | * @param string $region which region to check |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | public function hasContent($region) |
||
188 | |||
189 | |||
190 | |||
191 | /** |
||
192 | * Render all views for a specific region. |
||
193 | * |
||
194 | * @param string $region which region to use |
||
195 | * |
||
196 | * @return void |
||
197 | */ |
||
198 | View Code Duplication | public function render($region = "main") |
|
219 | |||
220 | |||
221 | /** |
||
222 | * Render all views for a specific region and buffer the result. |
||
223 | * |
||
224 | * @param string $region which region to use. |
||
225 | * |
||
226 | * @return string with the buffered results. |
||
227 | */ |
||
228 | public function renderBuffered($region = "main") |
||
236 | } |
||
237 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: