Total Complexity | 58 |
Total Lines | 253 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like PathController 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 PathController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class PathController |
||
25 | { |
||
26 | public $xoopsPath = [ |
||
27 | 'lib' => '', |
||
28 | 'data' => '', |
||
29 | ]; |
||
30 | public $path_lookup = [ |
||
31 | 'data' => 'VAR_PATH', |
||
32 | 'lib' => 'PATH', |
||
33 | ]; |
||
34 | |||
35 | public $validPath = [ |
||
36 | 'data' => 0, |
||
37 | 'lib' => 0, |
||
38 | ]; |
||
39 | |||
40 | public $permErrors = [ |
||
41 | 'data' => null, |
||
42 | ]; |
||
43 | |||
44 | public function __construct() |
||
45 | { |
||
46 | if (isset($_SESSION['settings']['VAR_PATH'])) { |
||
47 | foreach ($this->path_lookup as $req => $sess) { |
||
48 | $this->xoopsPath[$req] = $_SESSION['settings'][$sess]; |
||
49 | } |
||
50 | } else { |
||
51 | $path = XOOPS_ROOT_PATH; |
||
52 | if (defined('XOOPS_PATH')) { |
||
53 | $this->xoopsPath['lib'] = XOOPS_PATH; |
||
54 | } elseif (defined('XOOPS_TRUST_PATH')) { |
||
55 | $this->xoopsPath['lib'] = XOOPS_TRUST_PATH; |
||
56 | } else { |
||
57 | $this->xoopsPath['lib'] = dirname($path) . '/xoops_lib'; |
||
58 | if (!is_dir($this->xoopsPath['lib'] . '/')) { |
||
59 | $this->xoopsPath['lib'] = $path . '/xoops_lib'; |
||
60 | } |
||
61 | } |
||
62 | if (defined('XOOPS_VAR_PATH')) { |
||
63 | $this->xoopsPath['data'] = XOOPS_VAR_PATH; |
||
64 | } else { |
||
65 | $this->xoopsPath['data'] = dirname($path) . '/xoops_data'; |
||
66 | if (!is_dir($this->xoopsPath['data'] . '/')) { |
||
67 | $this->xoopsPath['data'] = $path . '/xoops_data'; |
||
68 | } |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function execute() |
||
77 | { |
||
78 | $this->readRequest(); |
||
79 | $valid = $this->validate(); |
||
80 | if ($_SERVER['REQUEST_METHOD'] === 'POST' && @$_POST['task'] === 'path') { |
||
81 | foreach ($this->path_lookup as $req => $sess) { |
||
82 | $_SESSION['settings'][$sess] = $this->xoopsPath[$req]; |
||
83 | } |
||
84 | if ($valid) { |
||
85 | return $_SESSION['settings']; |
||
86 | } else { |
||
87 | return false; |
||
88 | } |
||
89 | } |
||
90 | return null; |
||
91 | } |
||
92 | |||
93 | public function readRequest() |
||
94 | { |
||
95 | if ($_SERVER['REQUEST_METHOD'] === 'POST' && @$_POST['task'] === 'path') { |
||
96 | $request = $_POST; |
||
97 | foreach ($this->path_lookup as $req => $sess) { |
||
98 | if (isset($request[$req])) { |
||
99 | $request[$req] = str_replace("\\", '/', trim($request[$req])); |
||
100 | if (substr($request[$req], -1) === '/') { |
||
101 | $request[$req] = substr($request[$req], 0, -1); |
||
102 | } |
||
103 | $this->xoopsPath[$req] = $request[$req]; |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function validate() |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param string $PATH |
||
138 | * |
||
139 | * @return int |
||
140 | */ |
||
141 | public function checkPath($PATH = '') |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param $parent |
||
164 | * @param $path |
||
165 | * @param $error |
||
166 | * @return null |
||
167 | */ |
||
168 | public function setPermission($parent, $path, &$error) |
||
169 | { |
||
170 | if (is_array($path)) { |
||
171 | foreach (array_keys($path) as $item) { |
||
172 | if (is_string($item)) { |
||
173 | $error[$parent . '/' . $item] = $this->makeWritable($parent . '/' . $item); |
||
174 | if (empty($path[$item])) { |
||
175 | continue; |
||
176 | } |
||
177 | foreach ($path[$item] as $child) { |
||
178 | $this->setPermission($parent . '/' . $item, $child, $error); |
||
179 | } |
||
180 | } else { |
||
181 | $error[$parent . '/' . $path[$item]] = $this->makeWritable($parent . '/' . $path[$item]); |
||
182 | } |
||
183 | } |
||
184 | } else { |
||
185 | $error[$parent . '/' . $path] = $this->makeWritable($parent . '/' . $path); |
||
186 | } |
||
187 | |||
188 | return null; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param string $path |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function checkPermissions($path = 'data') |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Write-enable the specified file/folder |
||
229 | * |
||
230 | * @param string $path |
||
231 | * @param bool|string $group |
||
232 | * @param bool $create |
||
233 | * |
||
234 | * @internal param bool $recurse |
||
235 | * @return false on failure, method (u-ser,g-roup,w-orld) on success |
||
236 | */ |
||
237 | public function makeWritable($path, $group = false, $create = true) |
||
277 | } |
||
278 | } |
||
279 |