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 DoctrineExtension 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 DoctrineExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class DoctrineExtension extends \Twig_Extension |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Number of maximum characters that one single line can hold in the interface |
||
| 27 | * |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | private $maxCharWidth = 100; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Define our functions |
||
| 34 | * |
||
| 35 | * @return \Twig_SimpleFilter[] |
||
| 36 | */ |
||
| 37 | public function getFilters() |
||
| 38 | { |
||
| 39 | return array( |
||
| 40 | new \Twig_SimpleFilter('doctrine_minify_query', array($this, 'minifyQuery'), array('deprecated' => true)), |
||
| 41 | new \Twig_SimpleFilter('doctrine_pretty_query', array($this, 'formatQuery'), array('is_safe' => array('html'))), |
||
| 42 | new \Twig_SimpleFilter('doctrine_replace_query_parameters', array($this, 'replaceQueryParameters')), |
||
| 43 | ); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Get the possible combinations of elements from the given array |
||
| 48 | * |
||
| 49 | * @param array $elements |
||
| 50 | * @param integer $combinationsLevel |
||
| 51 | * |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | private function getPossibleCombinations(array $elements, $combinationsLevel) |
||
| 55 | { |
||
| 56 | $baseCount = count($elements); |
||
| 57 | $result = array(); |
||
| 58 | |||
| 59 | if (1 === $combinationsLevel) { |
||
| 60 | foreach ($elements as $element) { |
||
| 61 | $result[] = array($element); |
||
| 62 | } |
||
| 63 | |||
| 64 | return $result; |
||
| 65 | } |
||
| 66 | |||
| 67 | $nextLevelElements = $this->getPossibleCombinations($elements, $combinationsLevel - 1); |
||
| 68 | |||
| 69 | foreach ($nextLevelElements as $nextLevelElement) { |
||
| 70 | $lastElement = $nextLevelElement[$combinationsLevel - 2]; |
||
| 71 | $found = false; |
||
| 72 | |||
| 73 | foreach ($elements as $key => $element) { |
||
| 74 | if ($element == $lastElement) { |
||
| 75 | $found = true; |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | |||
| 79 | if ($found == true && $key < $baseCount) { |
||
|
|
|||
| 80 | $tmp = $nextLevelElement; |
||
| 81 | $newCombination = array_slice($tmp, 0); |
||
| 82 | $newCombination[] = $element; |
||
| 83 | $result[] = array_slice($newCombination, 0); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | return $result; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Shrink the values of parameters from a combination |
||
| 93 | * |
||
| 94 | * @param array $parameters |
||
| 95 | * @param array $combination |
||
| 96 | * |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | private function shrinkParameters(array $parameters, array $combination) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Attempt to compose the best scenario minified query so that a user could find it without expanding it |
||
| 136 | * |
||
| 137 | * @param string $query |
||
| 138 | * @param array $keywords |
||
| 139 | * @param integer $required |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | private function composeMiniQuery($query, array $keywords, $required) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Minify the query |
||
| 189 | * |
||
| 190 | * @param string $query |
||
| 191 | * |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public function minifyQuery($query) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Escape parameters of a SQL query |
||
| 237 | * DON'T USE THIS FUNCTION OUTSIDE ITS INTENDED SCOPE |
||
| 238 | * |
||
| 239 | * @internal |
||
| 240 | * |
||
| 241 | * @param mixed $parameter |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public static function escapeFunction($parameter) |
||
| 246 | { |
||
| 247 | $result = $parameter; |
||
| 248 | |||
| 249 | switch (true) { |
||
| 250 | // Check if result is non-unicode string using PCRE_UTF8 modifier |
||
| 251 | case is_string($result) && !preg_match('//u', $result): |
||
| 252 | $result = '0x'. strtoupper(bin2hex($result)); |
||
| 253 | break; |
||
| 254 | |||
| 255 | case is_string($result): |
||
| 256 | $result = "'".addslashes($result)."'"; |
||
| 257 | break; |
||
| 258 | |||
| 259 | case is_array($result): |
||
| 260 | foreach ($result as &$value) { |
||
| 261 | $value = static::escapeFunction($value); |
||
| 262 | } |
||
| 263 | |||
| 264 | $result = implode(', ', $result); |
||
| 265 | break; |
||
| 266 | |||
| 267 | case is_object($result): |
||
| 268 | $result = addslashes((string) $result); |
||
| 269 | break; |
||
| 270 | |||
| 271 | case null === $result: |
||
| 272 | $result = 'NULL'; |
||
| 273 | break; |
||
| 274 | |||
| 275 | case is_bool($result): |
||
| 276 | $result = $result ? '1' : '0'; |
||
| 277 | break; |
||
| 278 | } |
||
| 279 | |||
| 280 | return $result; |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Return a query with the parameters replaced |
||
| 285 | * |
||
| 286 | * @param string $query |
||
| 287 | * @param array $parameters |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function replaceQueryParameters($query, array $parameters) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Formats and/or highlights the given SQL statement. |
||
| 321 | * |
||
| 322 | * @param string $sql |
||
| 323 | * @param bool $highlightOnly If true the query is not formatted, just highlighted |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function formatQuery($sql, $highlightOnly = false) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Get the name of the extension |
||
| 353 | * |
||
| 354 | * @return string |
||
| 355 | */ |
||
| 356 | public function getName() |
||
| 360 | } |
||
| 361 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.