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 namespace Myth\Controllers; |
||
42 | class ThemedController extends BaseController |
||
43 | { |
||
44 | /** |
||
45 | * Stores data variables to be sent to the view. |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $vars = array(); |
||
49 | |||
50 | /** |
||
51 | * Stores current status message. |
||
52 | * @var |
||
53 | */ |
||
54 | protected $message; |
||
55 | |||
56 | /** |
||
57 | * The UIKit to make available to the template views. |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $uikit = ''; |
||
61 | |||
62 | /** |
||
63 | * An instance of an active Themer to use. |
||
64 | * @var null |
||
65 | */ |
||
66 | protected $themer = null; |
||
67 | |||
68 | /** |
||
69 | * Allows per-controller override of theme. |
||
70 | * @var null |
||
71 | */ |
||
72 | protected $theme = null; |
||
73 | |||
74 | /** |
||
75 | * Per-controller override of the current layout file. |
||
76 | * @var null |
||
77 | */ |
||
78 | protected $layout = null; |
||
79 | |||
80 | /** |
||
81 | * Stores an array of javascript files. |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $external_scripts = array(); |
||
85 | |||
86 | /** |
||
87 | * Stores an array of CSS stylesheets. |
||
88 | * @var array |
||
89 | */ |
||
90 | protected $stylesheets = array(); |
||
91 | |||
92 | /** |
||
93 | * A MenuCollection instance |
||
94 | * @var |
||
95 | */ |
||
96 | protected $meta; |
||
97 | |||
98 | /** |
||
99 | * Whether set() should escape the output... |
||
100 | * @var bool |
||
101 | */ |
||
102 | protected $auto_escape = null; |
||
103 | |||
104 | /** |
||
105 | * An instance of ZendFrameworks Escaper |
||
106 | * @var null |
||
107 | */ |
||
108 | protected $escaper = null; |
||
109 | |||
110 | //-------------------------------------------------------------------- |
||
111 | |||
112 | /** |
||
113 | * Constructor takes care of getting the template engine up and running |
||
114 | * and bound to our DI object, as well as any other preliminary needs, |
||
115 | * like detecting the variant to use, etc. |
||
116 | */ |
||
117 | public function __construct() |
||
166 | |||
167 | //-------------------------------------------------------------------- |
||
168 | |||
169 | /** |
||
170 | * Provides a common interface with the other rendering methods to |
||
171 | * set the output of the method. Uses the current instance of $this->template. |
||
172 | * Ensures that any data we've stored through $this->setVar() are present |
||
173 | * and includes the status messages into the data. |
||
174 | * |
||
175 | * @param array $data |
||
176 | * @param int $cache_time |
||
177 | */ |
||
178 | public function render($data = array(), $cache_time=0) |
||
221 | |||
222 | //-------------------------------------------------------------------- |
||
223 | |||
224 | /** |
||
225 | * Sets a data variable to be sent to the view during the render() method. |
||
226 | * Will auto-escape data on the way in, unless specifically told not to. |
||
227 | * |
||
228 | * Uses ZendFramework's Escaper to handle the data escaping, |
||
229 | * based on context. Valid contexts are: |
||
230 | * - html |
||
231 | * - htmlAttr |
||
232 | * - js |
||
233 | * - css |
||
234 | * - url |
||
235 | * |
||
236 | * @param string $name |
||
237 | * @param mixed $value |
||
238 | * @param string $context |
||
239 | * @param bool $do_escape |
||
240 | */ |
||
241 | public function setVar($name, $value = null, $context='html', $do_escape=null) |
||
262 | |||
263 | //-------------------------------------------------------------------- |
||
264 | |||
265 | //-------------------------------------------------------------------- |
||
266 | // Status Messages |
||
267 | //-------------------------------------------------------------------- |
||
268 | |||
269 | /** |
||
270 | * Sets a status message (for displaying small success/error messages). |
||
271 | * This is used in place of the session->flashdata functions since you |
||
272 | * don't always want to have to refresh the page to show the message. |
||
273 | * |
||
274 | * @param string $message The message to save. |
||
275 | * @param string $type The string to be included as the CSS class of the containing div. |
||
276 | */ |
||
277 | public function setMessage($message = '', $type = 'info') |
||
290 | |||
291 | //-------------------------------------------------------------------- |
||
292 | |||
293 | /** |
||
294 | * Retrieves the status message to display (if any). |
||
295 | * |
||
296 | * @param string $message [description] |
||
297 | * @param string $type [description] |
||
298 | * @return array |
||
299 | */ |
||
300 | public function message($message = '', $type = 'info') |
||
337 | |||
338 | //-------------------------------------------------------------------- |
||
339 | |||
340 | //-------------------------------------------------------------------- |
||
341 | // Utility Methods |
||
342 | //-------------------------------------------------------------------- |
||
343 | |||
344 | /** |
||
345 | * Detects whether the item is being displayed on a desktop, phone, |
||
346 | * or tablet device. |
||
347 | */ |
||
348 | protected function detectVariant() |
||
361 | |||
362 | //-------------------------------------------------------------------- |
||
363 | |||
364 | //-------------------------------------------------------------------- |
||
365 | // 'Asset' functions |
||
366 | //-------------------------------------------------------------------- |
||
367 | |||
368 | /** |
||
369 | * Adds an external javascript file to the 'external_scripts' array. |
||
370 | * |
||
371 | * @param [type] $filename [description] |
||
372 | */ |
||
373 | View Code Duplication | public function addScript($filename) |
|
381 | |||
382 | //-------------------------------------------------------------------- |
||
383 | |||
384 | /** |
||
385 | * Adds an external stylesheet file to the 'stylesheets' array. |
||
386 | */ |
||
387 | View Code Duplication | public function addStyle($filename) |
|
395 | |||
396 | //-------------------------------------------------------------------- |
||
397 | } |
||
398 | |||
399 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.