Total Complexity | 45 |
Total Lines | 372 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ViewManager 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 ViewManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class ViewManager extends \Slim\Views\Twig |
||
22 | { |
||
23 | use \PHPPgAdmin\Traits\HelperTrait; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | public $appLangFiles = []; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | public $appName = ''; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | public $appVersion = ''; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | public $form = ''; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | public $href = ''; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | public $lang = []; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | public $conf; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | * @psalm-suppress PropertyNotSetInConstructor |
||
63 | */ |
||
64 | public $phpMinVer; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | * @psalm-suppress PropertyNotSetInConstructor |
||
69 | */ |
||
70 | public $postgresqlMinVer; |
||
71 | |||
72 | /** |
||
73 | * @var \PHPPgAdmin\Misc |
||
74 | */ |
||
75 | public $misc; |
||
76 | |||
77 | /** |
||
78 | * @var \PHPPgAdmin\ContainerUtils |
||
79 | */ |
||
80 | protected $container; |
||
81 | |||
82 | private $_connection; |
||
|
|||
83 | |||
84 | /** |
||
85 | * @var bool |
||
86 | */ |
||
87 | private $_no_db_connection = false; |
||
88 | |||
89 | /** |
||
90 | * @var bool |
||
91 | */ |
||
92 | private $_reload_browser = false; |
||
93 | |||
94 | private $_data; |
||
95 | |||
96 | private $_database; |
||
97 | |||
98 | /** |
||
99 | * @var string |
||
100 | */ |
||
101 | private $_server_id; |
||
102 | |||
103 | private $_server_info; |
||
104 | |||
105 | /** |
||
106 | * @var string |
||
107 | */ |
||
108 | private $_error_msg = ''; |
||
109 | |||
110 | /** |
||
111 | * Undocumented variable. |
||
112 | * |
||
113 | * @var self |
||
114 | */ |
||
115 | private static $instance; |
||
116 | |||
117 | /** |
||
118 | * @param \PHPPgAdmin\ContainerUtils $container The container |
||
119 | * @param mixed $path |
||
120 | * @param mixed $settings |
||
121 | * @param \PHPPgAdmin\ContainerUtils $c |
||
122 | */ |
||
123 | public function __construct($path, $settings, \PHPPgAdmin\ContainerUtils $c) |
||
155 | } |
||
156 | } |
||
157 | |||
158 | public function maybeRenderIframes(\Slim\Http\Response $response, string $subject, string $query_string): \Slim\Http\Response |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Gets the theme from |
||
181 | * 1. The $_REQUEST global (when it's chosen from start screen) |
||
182 | * 2. Server specific config theme 3.- $_SESSION global (subsequent requests after 1.) 4.- $_COOKIE global (mostly |
||
183 | * fallback for $_SESSION after 1.- and 3.-) 5.- theme as set in config 6.- 'default' theme. |
||
184 | * |
||
185 | * @param array $conf The conf |
||
186 | * @param null|mixed $_server_info |
||
187 | * |
||
188 | * @return string the theme |
||
189 | */ |
||
190 | public function getTheme(array $conf, $_server_info = null) |
||
191 | { |
||
192 | $_theme = 'default'; |
||
193 | // List of themes |
||
194 | $themefolders = $this->getThemeFolders(); |
||
195 | // Check if theme is in $_REQUEST, $_SESSION or $_COOKIE |
||
196 | // 1.- First priority: $_REQUEST, this happens when you use the selector |
||
197 | if (\array_key_exists('theme', $_REQUEST) && |
||
198 | \array_key_exists($_REQUEST['theme'], $themefolders)) { |
||
199 | $_theme = $_REQUEST['theme']; |
||
200 | } elseif ( // otherwise, see if there's a theme associated with this particular server |
||
201 | null !== $_server_info && |
||
202 | \array_key_exists('theme', $_server_info) && |
||
203 | \is_string($_server_info['theme']) && |
||
204 | \array_key_exists($_COOKIE['ppaTheme'], $themefolders)) { |
||
205 | $_theme = $_server_info['theme']; |
||
206 | } elseif (isset($_SESSION) && \array_key_exists('ppaTheme', $_SESSION) && |
||
207 | \array_key_exists($_SESSION['ppaTheme'], $themefolders)) { |
||
208 | // otherwise check $_SESSION |
||
209 | $_theme = $_SESSION['ppaTheme']; |
||
210 | } elseif (\array_key_exists('ppaTheme', $_COOKIE) && |
||
211 | \array_key_exists($_COOKIE['ppaTheme'], $themefolders)) { |
||
212 | // oterwise check $_COOKIE |
||
213 | $_theme = $_COOKIE['ppaTheme']; |
||
214 | } elseif ( // see if there's a valid theme set in config file |
||
215 | \array_key_exists('theme', $conf) && |
||
216 | \is_string($conf['theme']) && |
||
217 | \array_key_exists($conf['theme'], $themefolders)) { |
||
218 | $_theme = $conf['theme']; |
||
219 | } |
||
220 | |||
221 | return $_theme; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Sets the form tracking variable. |
||
226 | */ |
||
227 | public function setForm(): string |
||
228 | { |
||
229 | $form = []; |
||
230 | |||
231 | if ($this->container->server) { |
||
232 | $form[] = \sprintf( |
||
233 | '<input type="hidden" name="%s" value="%s" />', |
||
234 | 'server', |
||
235 | \htmlspecialchars($this->container->server) |
||
236 | ); |
||
237 | } |
||
238 | |||
239 | if ($this->container->database) { |
||
240 | $form[] = \sprintf( |
||
241 | '<input type="hidden" name="%s" value="%s" />', |
||
242 | 'database', |
||
243 | \htmlspecialchars($this->container->database) |
||
244 | ); |
||
245 | } |
||
246 | |||
247 | if ($this->container->schema) { |
||
248 | $form[] = \sprintf( |
||
249 | '<input type="hidden" name="%s" value="%s" />', |
||
250 | 'schema', |
||
251 | \htmlspecialchars($this->container->schema) |
||
252 | ); |
||
253 | } |
||
254 | $this->form = \implode("\n", $form); |
||
255 | |||
256 | return $this->form; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Displays link to the context help. |
||
261 | * |
||
262 | * @param string $str the string that the context help is related to (already escaped) |
||
263 | * @param string $help help section identifier |
||
264 | * @param bool $do_print true to echo, false to return |
||
265 | * |
||
266 | * @return string|void |
||
267 | */ |
||
268 | public function printHelp($str, $help = null, $do_print = true) |
||
280 | } |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Gets the help link. |
||
285 | * |
||
286 | * @param string $help The help subject |
||
287 | * |
||
288 | * @return string the help link |
||
289 | */ |
||
290 | public function getHelpLink($help) |
||
291 | { |
||
292 | return \htmlspecialchars( |
||
293 | $this->container->getSubfolder('help?help=') . |
||
294 | \urlencode($help) . |
||
295 | '&server=' . |
||
296 | \urlencode($this->misc->getServerId()) |
||
297 | ); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @param string $icon |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | public function icon($icon = ''): string |
||
306 | { |
||
307 | $icon = (string) ($icon ?? ''); |
||
308 | |||
309 | $theme = $this->conf['theme']; |
||
310 | $path = 'assets/images/themes'; |
||
311 | $default_icon = \sprintf('%s/%s/default/DisconnectedServer.png', \containerInstance()->subFolder, $path); |
||
312 | |||
313 | if (\is_readable(\sprintf('%s/%s/%s/%s.png', \containerInstance()->BASE_PATH, $path, $theme, $icon))) { |
||
314 | return \sprintf('%s/%s/%s/%s.png', \containerInstance()->subFolder, $path, $theme, $icon); |
||
315 | } |
||
316 | |||
317 | if (\is_readable(\sprintf('%s/%s/%s/%s.gif', \containerInstance()->BASE_PATH, $path, $theme, $icon))) { |
||
318 | return \sprintf('%s/%s/%s/%s.gif', \containerInstance()->subFolder, $path, $theme, $icon); |
||
319 | } |
||
320 | |||
321 | if (\is_readable(\sprintf('%s/%s/%s/%s.ico', \containerInstance()->BASE_PATH, $path, $theme, $icon))) { |
||
322 | return \sprintf('%s/%s/%s/%s.ico', \containerInstance()->subFolder, $path, $theme, $icon); |
||
323 | } |
||
324 | |||
325 | if (\is_readable(\sprintf('%s/%s/default/%s.png', \containerInstance()->BASE_PATH, $path, $icon))) { |
||
326 | return \sprintf('%s/%s/default/%s.png', \containerInstance()->subFolder, $path, $icon); |
||
327 | } |
||
328 | |||
329 | if (\is_readable(\sprintf('%s/%s/default/%s.gif', \containerInstance()->BASE_PATH, $path, $icon))) { |
||
330 | return \sprintf('%s/%s/default/%s.gif', \containerInstance()->subFolder, $path, $icon); |
||
331 | } |
||
332 | |||
333 | if (\is_readable(\sprintf('%s/%s/default/%s.ico', \containerInstance()->BASE_PATH, $path, $icon))) { |
||
334 | return \sprintf('%s/%s/default/%s.ico', \containerInstance()->subFolder, $path, $icon); |
||
335 | } |
||
336 | |||
337 | return $default_icon; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Undocumented function. |
||
342 | * |
||
343 | * @param string $subject |
||
344 | * @psalm-suppress LessSpecificReturnStatement |
||
345 | * @psalm-suppress MoreSpecificReturnType |
||
346 | * |
||
347 | * @return class-string |
||
348 | */ |
||
349 | private static function getControllerClassName(string $subject): string |
||
352 | } |
||
353 | |||
354 | private function getContainer(): \PHPPgAdmin\ContainerUtils |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Traverse THEME_PATH, consider as theme folders those which |
||
361 | * contain a `global.css` stylesheet. |
||
362 | * |
||
363 | * @return array the theme folders |
||
364 | */ |
||
365 | private function getThemeFolders(): array |
||
395 |