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 CoreExtension 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 CoreExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class CoreExtension extends \Twig_Extension |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var ContainerInterface |
||
| 26 | */ |
||
| 27 | private $container; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var TranslatorInterface |
||
| 31 | */ |
||
| 32 | private $translator; |
||
| 33 | |||
| 34 | public function __construct(ContainerInterface $container = null) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @return ContainerInterface |
||
| 42 | */ |
||
| 43 | public function getContainer() |
||
| 47 | |||
| 48 | public function getTokenParsers() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Returns a list of functions to add to the existing list. |
||
| 57 | * |
||
| 58 | * @return array An array of functions |
||
| 59 | */ |
||
| 60 | public function getFunctions() |
||
| 87 | |||
| 88 | public function getFilters() |
||
| 97 | |||
| 98 | public function getAssetPath($path) |
||
| 102 | |||
| 103 | public function button() |
||
| 106 | |||
| 107 | public function img() |
||
| 110 | |||
| 111 | public function icon() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Display flash messages in twig template. Defaults to bootstrap alert classes. |
||
| 117 | * |
||
| 118 | * <pre> |
||
| 119 | * {{ showflashes() }} |
||
| 120 | * {{ showflashes({'class': 'custom-class', 'tag': 'span'}) }} |
||
| 121 | * </pre> |
||
| 122 | * |
||
| 123 | * @param array $params |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function showFlashes(array $params = []) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Delete a key of an array |
||
| 170 | * |
||
| 171 | * @param array $array Source array |
||
| 172 | * @param string $key The key to remove |
||
| 173 | * |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | public function arrayUnset($array, $key) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param string $code |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function languageName($code) |
||
| 191 | |||
| 192 | public function yesNo($string) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Apply an existing function (e.g. php's `md5`) to a string. |
||
| 203 | * |
||
| 204 | * @param $string |
||
| 205 | * @param $func |
||
| 206 | * @return mixed |
||
| 207 | */ |
||
| 208 | public function applyPhp($string, $func) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Protect a given mail address by finding the text 'x@y' and replacing |
||
| 219 | * it with HTML entities. This provides protection against email harvesters. |
||
| 220 | * |
||
| 221 | * @param string |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public function protectMailAddress($string) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Zikula imposes no restriction on page variable names. |
||
| 239 | * Typical usage is to set `title` `meta.charset` `lang` etc. |
||
| 240 | * array values are set using `.` in the `$name` string (e.g. `meta.charset`) |
||
| 241 | * @param string $name |
||
| 242 | * @param string $value |
||
| 243 | */ |
||
| 244 | View Code Duplication | public function pageSetVar($name, $value) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Zikula allows only the following asset types |
||
| 255 | * <ul> |
||
| 256 | * <li>stylesheet</li> |
||
| 257 | * <li>javascript</li> |
||
| 258 | * <li>header</li> |
||
| 259 | * <li>footer</li> |
||
| 260 | * </ul> |
||
| 261 | * |
||
| 262 | * @param string $type |
||
| 263 | * @param string $value |
||
| 264 | * @param int $weight |
||
| 265 | */ |
||
| 266 | public function pageAddAsset($type, $value, $weight = AssetBag::WEIGHT_DEFAULT) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param $name |
||
| 293 | * @param null $default |
||
| 294 | * @return mixed |
||
| 295 | */ |
||
| 296 | View Code Duplication | public function pageGetVar($name, $default = null) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @param $module |
||
| 307 | * @param $name |
||
| 308 | * @param null $default |
||
| 309 | * @return mixed |
||
| 310 | */ |
||
| 311 | View Code Duplication | public function getModVar($module, $name, $default = null) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @param $name |
||
| 322 | * @param null $default |
||
| 323 | * @return mixed |
||
| 324 | */ |
||
| 325 | View Code Duplication | public function getSystemVar($name, $default = null) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $name |
||
| 336 | * @param string $value |
||
| 337 | */ |
||
| 338 | public function setMetaTag($name, $value) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Call a php callable with parameters. |
||
| 351 | * @param callable $callable |
||
| 352 | * @param array $params |
||
| 353 | * @return mixed |
||
| 354 | */ |
||
| 355 | public function callFunc(callable $callable, array $params = []) |
||
| 362 | } |
||
| 363 |
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.