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:
Complex classes like TwigView often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TwigView, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class TwigView extends View |
||
29 | // @codingStandardsIgnoreEnd |
||
30 | { |
||
31 | const EXT = '.twig'; |
||
32 | |||
33 | const ENV_CONFIG = 'WyriHaximus.TwigView.environment'; |
||
34 | |||
35 | /** |
||
36 | * Extension to use. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | // @codingStandardsIgnoreStart |
||
41 | protected $_ext = self::EXT; |
||
42 | // @codingStandardsIgnoreEnd |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $extensions = [ |
||
48 | self::EXT, |
||
49 | '.tpl', |
||
50 | '.ctp', |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * Twig instance. |
||
55 | * |
||
56 | * @var \Twig_Environment |
||
57 | */ |
||
58 | protected $twig; |
||
59 | |||
60 | /** |
||
61 | * Initialize view. |
||
62 | * |
||
63 | * @return void |
||
64 | */ |
||
65 | 21 | public function initialize() |
|
75 | |||
76 | /** |
||
77 | * @return array |
||
78 | */ |
||
79 | 21 | protected function resolveConfig() |
|
94 | |||
95 | /** |
||
96 | * @return array |
||
97 | */ |
||
98 | 21 | protected function readConfig() |
|
111 | |||
112 | /** |
||
113 | * @param string $extension |
||
114 | */ |
||
115 | public function unshiftExtension($extension) |
||
119 | |||
120 | /** |
||
121 | * Create the template loader. |
||
122 | * |
||
123 | * @return \Twig_LoaderInterface |
||
124 | */ |
||
125 | 21 | protected function getLoader() |
|
131 | |||
132 | /** |
||
133 | * Get helper list. |
||
134 | * |
||
135 | * @return \Cake\View\Helper[] |
||
136 | */ |
||
137 | 12 | protected function generateHelperList() |
|
148 | /** |
||
149 | * Render the template. |
||
150 | * |
||
151 | * @param string $viewFile Template file. |
||
152 | * @param array $data Data that can be used by the template. |
||
153 | * |
||
154 | * @throws \Exception |
||
155 | * @return string |
||
156 | */ |
||
157 | // @codingStandardsIgnoreStart |
||
158 | 15 | protected function _render($viewFile, $data = array()) |
|
195 | |||
196 | /** |
||
197 | * @param string|null $name |
||
198 | * @return string |
||
199 | * @throws \Exception |
||
200 | */ |
||
201 | // @codingStandardsIgnoreStart |
||
202 | 11 | protected function _getViewFileName($name = null) |
|
203 | { |
||
204 | // @codingStandardsIgnoreEnd |
||
205 | 11 | $templatePath = $subDir = ''; |
|
206 | |||
207 | 11 | if ($this->subDir !== null) { |
|
208 | 11 | $subDir = $this->subDir . DIRECTORY_SEPARATOR; |
|
209 | } |
||
210 | 11 | if ($this->templatePath) { |
|
211 | $templatePath = $this->templatePath . DIRECTORY_SEPARATOR; |
||
212 | } |
||
213 | |||
214 | 11 | if ($name === null) { |
|
215 | $name = $this->template; |
||
216 | } |
||
217 | |||
218 | 11 | list($plugin, $name) = $this->pluginSplit($name); |
|
219 | 11 | $name = str_replace('/', DIRECTORY_SEPARATOR, $name); |
|
220 | |||
221 | 11 | if (strpos($name, DIRECTORY_SEPARATOR) === false && $name[0] !== '.') { |
|
222 | 2 | $name = $templatePath . $subDir . $this->_inflectViewFileName($name); |
|
223 | 9 | } elseif (strpos($name, DIRECTORY_SEPARATOR) !== false) { |
|
224 | 9 | if ($name[0] === DIRECTORY_SEPARATOR || $name[1] === ':') { |
|
225 | $name = trim($name, DIRECTORY_SEPARATOR); |
||
226 | 9 | } elseif (!$plugin || $this->templatePath !== $this->name) { |
|
227 | 9 | $name = $templatePath . $subDir . $name; |
|
228 | } else { |
||
229 | $name = DIRECTORY_SEPARATOR . $subDir . $name; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | 11 | View Code Duplication | foreach ($this->_paths($plugin) as $path) { |
234 | 11 | foreach ($this->extensions as $extension) { |
|
235 | 11 | if (file_exists($path . $name . $extension)) { |
|
236 | 11 | return $this->_checkFilePath($path . $name . $extension, $path); |
|
237 | } |
||
238 | } |
||
239 | } |
||
240 | |||
241 | throw new MissingTemplateException(['file' => $name . $this->_ext]); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @param string|null $name |
||
246 | * @return string |
||
247 | * @throws \Exception |
||
248 | */ |
||
249 | // @codingStandardsIgnoreStart |
||
250 | 7 | protected function _getLayoutFileName($name = null) |
|
279 | |||
280 | /** |
||
281 | * @param string $name |
||
282 | * @param bool $pluginCheck |
||
283 | * @return string |
||
284 | * @throws \Exception |
||
285 | */ |
||
286 | // @codingStandardsIgnoreStart |
||
287 | 3 | protected function _getElementFileName($name, $pluginCheck = true) |
|
307 | |||
308 | /** |
||
309 | * Get twig environment instance. |
||
310 | * |
||
311 | * @return \Twig_Environment |
||
312 | */ |
||
313 | 11 | public function getTwig() |
|
317 | |||
318 | /** |
||
319 | * Return empty string when View instance is cast to string. |
||
320 | * |
||
321 | * @return string |
||
322 | */ |
||
323 | public function __toString() |
||
327 | } |
||
328 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.