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 |
||
35 | abstract class Renderer extends ParameterHolder |
||
36 | { |
||
37 | /** |
||
38 | * @var Context An Context instance. |
||
39 | */ |
||
40 | protected $context = null; |
||
41 | |||
42 | /** |
||
43 | * @var string The context name. |
||
44 | */ |
||
45 | protected $contextName = null; |
||
46 | /** |
||
47 | * @var string A string with the default template file extension, |
||
48 | * including the dot. |
||
49 | */ |
||
50 | protected $defaultExtension = ''; |
||
51 | |||
52 | /** |
||
53 | * @var string The name of the array that contains the template vars. |
||
54 | */ |
||
55 | protected $varName = 'template'; |
||
56 | |||
57 | /** |
||
58 | * @var string The name of the array that contains the slots output. |
||
59 | */ |
||
60 | protected $slotsVarName = 'slots'; |
||
61 | |||
62 | /** |
||
63 | * @var bool Whether or not the template vars should be extracted. |
||
64 | */ |
||
65 | protected $extractVars = false; |
||
66 | |||
67 | /** |
||
68 | * @var array An array of objects to be exported for use in templates. |
||
69 | */ |
||
70 | protected $assigns = array(); |
||
71 | |||
72 | /** |
||
73 | * @var array An array of names for the "more" assigns. |
||
74 | */ |
||
75 | protected $moreAssignNames = array(); |
||
76 | |||
77 | /** |
||
78 | * Pre-serialization callback. |
||
79 | * |
||
80 | * Will set the name of the context and exclude the instance from serializing. |
||
81 | * |
||
82 | * @author David Zülke <[email protected]> |
||
83 | * @since 0.11.0 |
||
84 | */ |
||
85 | View Code Duplication | public function __sleep() |
|
92 | |||
93 | /** |
||
94 | * Post-unserialization callback. |
||
95 | * |
||
96 | * Will restore the context based on the names set by __sleep. |
||
97 | * |
||
98 | * @author David Zülke <[email protected]> |
||
99 | * @since 0.11.0 |
||
100 | */ |
||
101 | public function __wakeup() |
||
106 | |||
107 | /** |
||
108 | * Initialize this Renderer. |
||
109 | * |
||
110 | * @param Context $context The current application context. |
||
111 | * @param array $parameters An associative array of initialization parameters. |
||
112 | * |
||
113 | * @author David Zülke <[email protected]> |
||
114 | * @since 0.11.0 |
||
115 | */ |
||
116 | public function initialize(Context $context, array $parameters = array()) |
||
146 | |||
147 | /** |
||
148 | * Retrieve the current application context. |
||
149 | * |
||
150 | * @return Context The current Context instance. |
||
151 | * |
||
152 | * @author David Zülke <[email protected]> |
||
153 | * @since 0.11.0 |
||
154 | */ |
||
155 | final public function getContext() |
||
159 | |||
160 | /** |
||
161 | * Get the template file extension |
||
162 | * |
||
163 | * @return string The extension, including a leading dot. |
||
164 | * |
||
165 | * @author David Zülke <[email protected]> |
||
166 | * @since 0.11.0 |
||
167 | */ |
||
168 | public function getDefaultExtension() |
||
172 | |||
173 | /** |
||
174 | * Build an array of "more" assigns. |
||
175 | * |
||
176 | * @param array $moreAssigns The values to be assigned. |
||
177 | * @param array $moreAssignNames Assigns name map. |
||
178 | * |
||
179 | * @return array The data. |
||
180 | * |
||
181 | * @author David Zülke <[email protected]> |
||
182 | * @since 1.0.0 |
||
183 | */ |
||
184 | protected static function &buildMoreAssigns(&$moreAssigns, $moreAssignNames) |
||
200 | |||
201 | /** |
||
202 | * Render the presentation and return the result. |
||
203 | * |
||
204 | * @param TemplateLayer $layer The template layer to render. |
||
205 | * @param array $attributes The template variables. |
||
206 | * @param array $slots The slots. |
||
207 | * @param array $moreAssigns Associative array of additional assigns. |
||
208 | * |
||
209 | * @return string A rendered result. |
||
210 | * |
||
211 | * @author David Zülke <[email protected]> |
||
212 | * @since 0.11.0 |
||
213 | */ |
||
214 | abstract public function render(TemplateLayer $layer, array &$attributes = array(), array &$slots = array(), array &$moreAssigns = array()); |
||
215 | } |
||
216 |
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.