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 |
||
17 | class ViewHelperVariableContainer |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * Two-dimensional object array storing the values. The first dimension is the fully qualified ViewHelper name, |
||
22 | * and the second dimension is the identifier for the data the ViewHelper wants to store. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $objects = []; |
||
27 | |||
28 | /** |
||
29 | * @var ViewInterface |
||
30 | */ |
||
31 | protected $view; |
||
32 | |||
33 | public function pushDelegateVariableContainer($viewHelperClassName, VariableProviderInterface $variableProvider) |
||
40 | |||
41 | View Code Duplication | public function getTopmostDelegateVariableContainer($viewHelperClassName) |
|
48 | |||
49 | View Code Duplication | public function popDelegateVariableContainer($viewHelperClassName) |
|
56 | |||
57 | /** |
||
58 | * Add a variable to the Variable Container. Make sure that $viewHelperName is ALWAYS set |
||
59 | * to your fully qualified ViewHelper Class Name |
||
60 | * |
||
61 | * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper") |
||
62 | * @param string $key Key of the data |
||
63 | * @param mixed $value The value to store |
||
64 | * @return void |
||
65 | * @api |
||
66 | */ |
||
67 | public function add($viewHelperName, $key, $value) |
||
71 | |||
72 | /** |
||
73 | * Adds, or overrides recursively, all current variables defined in associative |
||
74 | * array or Traversable (with string keys!). |
||
75 | * |
||
76 | * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper") |
||
77 | * @param array|\Traversable $variables An associative array of all variables to add |
||
78 | * @return void |
||
79 | * @api |
||
80 | */ |
||
81 | public function addAll($viewHelperName, $variables) |
||
95 | |||
96 | /** |
||
97 | * Add a variable to the Variable Container. Make sure that $viewHelperName is ALWAYS set |
||
98 | * to your fully qualified ViewHelper Class Name. |
||
99 | * In case the value is already inside, it is silently overridden. |
||
100 | * |
||
101 | * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper") |
||
102 | * @param string $key Key of the data |
||
103 | * @param mixed $value The value to store |
||
104 | * @return void |
||
105 | */ |
||
106 | public function addOrUpdate($viewHelperName, $key, $value) |
||
113 | |||
114 | /** |
||
115 | * Gets a variable which is stored |
||
116 | * |
||
117 | * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper") |
||
118 | * @param string $key Key of the data |
||
119 | * @param mixed $default Default value to use if no value is found. |
||
120 | * @return mixed The object stored |
||
121 | * @api |
||
122 | */ |
||
123 | public function get($viewHelperName, $key, $default = null) |
||
127 | |||
128 | /** |
||
129 | * Gets all variables stored for a particular ViewHelper |
||
130 | * |
||
131 | * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper") |
||
132 | * @param mixed $default |
||
133 | * @return array |
||
134 | */ |
||
135 | public function getAll($viewHelperName, $default = null) |
||
139 | |||
140 | /** |
||
141 | * Determine whether there is a variable stored for the given key |
||
142 | * |
||
143 | * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper") |
||
144 | * @param string $key Key of the data |
||
145 | * @return boolean TRUE if a value for the given ViewHelperName / Key is stored, FALSE otherwise. |
||
146 | * @api |
||
147 | */ |
||
148 | public function exists($viewHelperName, $key) |
||
152 | |||
153 | /** |
||
154 | * Remove a value from the variable container |
||
155 | * |
||
156 | * @param string $viewHelperName The ViewHelper Class name (Fully qualified, like "TYPO3Fluid\Fluid\ViewHelpers\ForViewHelper") |
||
157 | * @param string $key Key of the data to remove |
||
158 | * @return void |
||
159 | * @api |
||
160 | */ |
||
161 | public function remove($viewHelperName, $key) |
||
165 | |||
166 | /** |
||
167 | * Set the view to pass it to ViewHelpers. |
||
168 | * |
||
169 | * @param ViewInterface $view View to set |
||
170 | * @return void |
||
171 | */ |
||
172 | public function setView(ViewInterface $view) |
||
176 | |||
177 | /** |
||
178 | * Get the view. |
||
179 | * |
||
180 | * !!! This is NOT a public API and might still change!!! |
||
181 | * |
||
182 | * @return ViewInterface The View |
||
183 | */ |
||
184 | public function getView() |
||
188 | |||
189 | /** |
||
190 | * Clean up for serializing. |
||
191 | * |
||
192 | * @return array |
||
193 | */ |
||
194 | public function __sleep() |
||
198 | } |
||
199 |
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.