Total Complexity | 45 |
Total Lines | 238 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like BeautifyExtension 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 BeautifyExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class BeautifyExtension extends \Twig_Extension |
||
6 | { |
||
7 | use TwigTrait; |
||
8 | |||
9 | const INFO = 'info'; |
||
10 | const SUCCESS = 'success'; |
||
11 | const REDIRECT = 'redirect'; |
||
12 | const CLIENT = 'client_error'; |
||
13 | const SERVER = 'server_error'; |
||
14 | |||
15 | protected $appDir; |
||
16 | private $paths = []; |
||
17 | |||
18 | public function __construct($shortFunctions) |
||
19 | { |
||
20 | global $kernel; |
||
21 | |||
22 | $this->shortFunctions = $shortFunctions; |
||
23 | $this->appDir = $kernel->getRootDir(); |
||
24 | } |
||
25 | |||
26 | public function getName() |
||
27 | { |
||
28 | return 'sludio_helper.twig.beautify_extension'; |
||
29 | } |
||
30 | |||
31 | public function getFilters() |
||
32 | { |
||
33 | $input = [ |
||
34 | 'beautify' => 'beautify', |
||
35 | 'urldecode' => 'url_decode', |
||
36 | 'parse' => 'parse', |
||
37 | 'file_exists' => 'file_exists', |
||
38 | 'html_entity_decode' => 'html_entity_decode', |
||
39 | 'strip_descr' => 'strip_descr', |
||
40 | 'pretty_print' => 'prettyPrint', |
||
41 | 'status_code_class' => 'statusCodeClass', |
||
42 | 'format_duration' => 'formatDuration', |
||
43 | 'short_uri' => 'shorthenUri', |
||
44 | 'is_ie' => 'isIE', |
||
45 | 'asset_version' => 'getAssetVersion', |
||
46 | 'usort' => 'usortFunction', |
||
47 | ]; |
||
48 | |||
49 | return $this->makeArray($input); |
||
50 | } |
||
51 | |||
52 | public function getFunctions() |
||
53 | { |
||
54 | $input = [ |
||
55 | 'detect_lang' => 'detectLang', |
||
56 | ]; |
||
57 | |||
58 | return $this->makeArray($input, 'function'); |
||
59 | } |
||
60 | |||
61 | public function url_decode($string) |
||
62 | { |
||
63 | return urldecode($string); |
||
64 | } |
||
65 | |||
66 | public function parse($string) |
||
67 | { |
||
68 | $str = parse_url($string); |
||
69 | |||
70 | $argv = []; |
||
71 | if (isset($str['query'])) { |
||
72 | $args = explode('&', $str['query']); |
||
73 | |||
74 | foreach ($args as $arg) { |
||
75 | $tmp = explode('=', $arg, 2); |
||
76 | $argv[$tmp[0]] = $tmp[1]; |
||
77 | } |
||
78 | } |
||
79 | |||
80 | return $argv; |
||
81 | } |
||
82 | |||
83 | public function file_exists($file) |
||
84 | { |
||
85 | return file_exists(getcwd().$file); |
||
86 | } |
||
87 | |||
88 | public function beautify($string) |
||
89 | { |
||
90 | $explode = explode('/', strip_tags($string)); |
||
91 | $string = implode(' / ', $explode); |
||
92 | |||
93 | return $string; |
||
94 | } |
||
95 | |||
96 | public function html_entity_decode($str) |
||
102 | } |
||
103 | |||
104 | public function strip_descr($body, $fallback = null, $type = null, $lengthIn = 300) |
||
118 | } |
||
119 | |||
120 | public function detectLang($body) |
||
121 | { |
||
122 | switch (true) { |
||
123 | case 0 === strpos($body, '<?xml'): |
||
124 | return 'xml'; |
||
125 | case 0 === strpos($body, '{'): |
||
126 | case 0 === strpos($body, '['): |
||
127 | return 'json'; |
||
128 | default: |
||
129 | return 'markup'; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | public function prettyPrint($code, $lang) |
||
147 | } |
||
148 | } |
||
149 | |||
150 | public function statusCodeClass($statusCode) |
||
162 | } |
||
163 | |||
164 | public function formatDuration($seconds) |
||
165 | { |
||
166 | $formats = [ |
||
167 | '%.2f s', |
||
168 | '%d ms', |
||
169 | '%d µs', |
||
170 | ]; |
||
171 | |||
172 | while ($format = array_shift($formats)) { |
||
173 | if ($seconds > 1) { |
||
174 | break; |
||
175 | } |
||
176 | |||
177 | $seconds *= 1000; |
||
178 | } |
||
179 | |||
180 | return sprintf($format, $seconds); |
||
181 | } |
||
182 | |||
183 | public function shortenUri($uri) |
||
184 | { |
||
185 | $parts = parse_url($uri); |
||
186 | |||
187 | return sprintf('%s://%s%s', isset($parts['scheme']) ? $parts['scheme'] : 'http', $parts['host'], isset($parts['port']) ? (':'.$parts['port']) : ''); |
||
188 | } |
||
189 | |||
190 | public function isIE() |
||
191 | { |
||
192 | $request = Request::createFromGlobals(); |
||
|
|||
193 | $agent = $request->server->get('HTTP_USER_AGENT'); |
||
194 | if (strpos($agent, 'MSIE') || strpos($agent, 'Edge') || strpos($agent, 'Trident/7')) { |
||
195 | return 1; |
||
196 | } |
||
197 | |||
198 | return 0; |
||
199 | } |
||
200 | |||
201 | public function getAssetVersion($filename) |
||
202 | { |
||
203 | if (count($this->paths) === 0) { |
||
204 | $manifestPath = $this->appDir.'/Resources/assets/rev-manifest.json'; |
||
205 | if (!file_exists($manifestPath)) { |
||
206 | return $filename; |
||
207 | } |
||
208 | $this->paths = json_decode(file_get_contents($manifestPath), true); |
||
209 | if (!isset($this->paths[$filename])) { |
||
210 | return $filename; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | return $this->paths[$filename]; |
||
215 | } |
||
216 | |||
217 | public function cmpOrderBy($aVar, $bVar) |
||
226 | } |
||
227 | } |
||
228 | |||
229 | public function usortFunction($objects, $parameter, $order = 'asc') |
||
230 | { |
||
231 | $this->param = $parameter; |
||
232 | $this->order = strtolower($order); |
||
233 | |||
234 | if (is_object($objects)) { |
||
235 | $objects = $objects->toArray(); |
||
236 | } |
||
243 | } |
||
244 | } |
||
245 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths