| Total Complexity | 45 |
| Total Lines | 251 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SludioExtension 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.
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 SludioExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class SludioExtension extends \Twig_Extension |
||
| 8 | { |
||
| 9 | use TwigTrait; |
||
| 10 | |||
| 11 | const INFO = 'info'; |
||
| 12 | const SUCCESS = 'success'; |
||
| 13 | const REDIRECT = 'redirect'; |
||
| 14 | const CLIENT = 'client_error'; |
||
| 15 | const SERVER = 'server_error'; |
||
| 16 | |||
| 17 | protected $appDir; |
||
| 18 | private $paths = []; |
||
| 19 | protected $param; |
||
| 20 | protected $order; |
||
| 21 | |||
| 22 | public $detector; |
||
| 23 | |||
| 24 | public function __construct($shortFunctions) |
||
| 25 | { |
||
| 26 | global $kernel; |
||
| 27 | |||
| 28 | $this->shortFunctions = $shortFunctions; |
||
| 29 | $this->appDir = $kernel->getRootDir(); |
||
| 30 | $this->detector = new \Mobile_Detect(); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function getName() |
||
| 36 | } |
||
| 37 | |||
| 38 | public function getFilters() |
||
| 57 | } |
||
| 58 | |||
| 59 | public function getFunctions() |
||
| 60 | { |
||
| 61 | $input = [ |
||
| 62 | 'detect_lang' => 'detectLang', |
||
| 63 | 'is_mobile' => [ |
||
| 64 | $this->detector, |
||
| 65 | 'isMobile', |
||
| 66 | ], |
||
| 67 | 'is_tablet' => [ |
||
| 68 | $this->detector, |
||
| 69 | 'isTablet', |
||
| 70 | ], |
||
| 71 | ]; |
||
| 72 | |||
| 73 | return $this->makeArray($input, 'function'); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function url_decode($string) |
||
| 79 | } |
||
| 80 | |||
| 81 | public function parse($string) |
||
| 82 | { |
||
| 83 | $str = parse_url($string); |
||
| 84 | |||
| 85 | $argv = []; |
||
| 86 | if (isset($str['query'])) { |
||
| 87 | $args = explode('&', $str['query']); |
||
| 88 | |||
| 89 | foreach ($args as $arg) { |
||
| 90 | $tmp = explode('=', $arg, 2); |
||
| 91 | $argv[$tmp[0]] = $tmp[1]; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | return $argv; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function file_exists($file) |
||
| 99 | { |
||
| 100 | return file_exists(getcwd().$file); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function beautify($string) |
||
| 104 | { |
||
| 105 | $explode = explode('/', strip_tags($string)); |
||
| 106 | $string = implode(' / ', $explode); |
||
| 107 | |||
| 108 | return $string; |
||
| 109 | } |
||
| 110 | |||
| 111 | public function html_entity_decode($str) |
||
| 112 | { |
||
| 113 | $str = html_entity_decode($str); |
||
| 114 | $str = preg_replace('#\R+#', '', $str); |
||
| 115 | |||
| 116 | return $str; |
||
| 117 | } |
||
| 118 | |||
| 119 | public function strip_descr($body, $fallback = null, $type = null, $lengthIn = 300) |
||
| 120 | { |
||
| 121 | if ($type && $fallback) { |
||
| 122 | $length = isset($fallback[$type]) ? $fallback[$type] : null; |
||
| 123 | } |
||
| 124 | if (!isset($length)) { |
||
| 125 | $length = $lengthIn; |
||
| 126 | } |
||
| 127 | |||
| 128 | if (strlen($body) > $length) { |
||
| 129 | $body = substr($body, 0, strpos($body, ' ', $length)).'...'; |
||
| 130 | } |
||
| 131 | |||
| 132 | return $body; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function detectLang($body) |
||
| 136 | { |
||
| 137 | switch (true) { |
||
| 138 | case 0 === strpos($body, '<?xml'): |
||
| 139 | return 'xml'; |
||
| 140 | case 0 === strpos($body, '{'): |
||
| 141 | case 0 === strpos($body, '['): |
||
| 142 | return 'json'; |
||
| 143 | default: |
||
| 144 | return 'markup'; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | public function prettyPrint($code, $lang) |
||
| 149 | { |
||
| 150 | switch ($lang) { |
||
| 151 | case 'json': |
||
| 152 | return json_encode(json_decode($code), JSON_PRETTY_PRINT); |
||
| 153 | case 'xml': |
||
| 154 | $xml = new \DomDocument('1.0'); |
||
| 155 | $xml->preserveWhiteSpace = false; |
||
| 156 | $xml->formatOutput = true; |
||
| 157 | $xml->loadXml($code); |
||
| 158 | |||
| 159 | return $xml->saveXml(); |
||
| 160 | default: |
||
| 161 | return $code; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | public function statusCodeClass($statusCode) |
||
| 166 | { |
||
| 167 | $codes = [ |
||
| 168 | 5 => self::SERVER, |
||
| 169 | 4 => self::CLIENT, |
||
| 170 | 3 => self::REDIRECT, |
||
| 171 | 2 => self::SUCCESS, |
||
| 172 | 1 => self::INFO, |
||
| 173 | ]; |
||
| 174 | $code = (int)floor(intval($statusCode) / 100); |
||
| 175 | |||
| 176 | return isset($codes[$code]) ? $codes[$code] : 'unknown'; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function formatDuration($seconds) |
||
| 180 | { |
||
| 181 | $formats = [ |
||
| 182 | '%.2f s', |
||
| 183 | '%d ms', |
||
| 184 | '%d µs', |
||
| 185 | ]; |
||
| 186 | |||
| 187 | while ($format = array_shift($formats)) { |
||
| 188 | if ($seconds > 1) { |
||
| 189 | break; |
||
| 190 | } |
||
| 191 | |||
| 192 | $seconds *= 1000; |
||
| 193 | } |
||
| 194 | |||
| 195 | return sprintf($format, $seconds); |
||
| 196 | } |
||
| 197 | |||
| 198 | public function shortenUri($uri) |
||
| 199 | { |
||
| 200 | $parts = parse_url($uri); |
||
| 201 | |||
| 202 | return sprintf('%s://%s%s', isset($parts['scheme']) ? $parts['scheme'] : 'http', $parts['host'], isset($parts['port']) ? (':'.$parts['port']) : ''); |
||
| 203 | } |
||
| 204 | |||
| 205 | public function isIE() |
||
| 206 | { |
||
| 207 | $request = Request::createFromGlobals(); |
||
| 208 | $agent = $request->server->get('HTTP_USER_AGENT'); |
||
| 209 | if (strpos($agent, 'MSIE') || strpos($agent, 'Edge') || strpos($agent, 'Trident/7')) { |
||
| 210 | return 1; |
||
| 211 | } |
||
| 212 | |||
| 213 | return 0; |
||
| 214 | } |
||
| 215 | |||
| 216 | public function getAssetVersion($filename) |
||
| 230 | } |
||
| 231 | |||
| 232 | public function cmpOrderBy($aVar, $bVar) |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | public function usortFunction($objects, $parameter, $order = 'asc') |
||
| 245 | { |
||
| 246 | $this->param = $parameter; |
||
| 258 | } |
||
| 259 | } |
||
| 260 |