Total Complexity | 48 |
Total Lines | 310 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like IOHelperTrait 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 IOHelperTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | trait IOHelperTrait |
||
35 | { |
||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | public $tmp = []; |
||
40 | |||
41 | /** |
||
42 | * @param bool $readonly |
||
43 | * @return string |
||
44 | * @throws PhpfastcacheIOException |
||
45 | */ |
||
46 | public function getPath($readonly = false): string |
||
47 | { |
||
48 | /** |
||
49 | * Get the base system temporary directory |
||
50 | */ |
||
51 | $tmp_dir = rtrim(ini_get('upload_tmp_dir') ?: sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache'; |
||
52 | |||
53 | /** |
||
54 | * Calculate the security key |
||
55 | */ |
||
56 | { |
||
57 | $securityKey = $this->getConfig()->getSecurityKey(); |
||
58 | if (!$securityKey || mb_strtolower($securityKey) === 'auto') { |
||
59 | if (isset($_SERVER[ 'HTTP_HOST' ])) { |
||
60 | $securityKey = preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $_SERVER[ 'HTTP_HOST' ]))); |
||
61 | } else { |
||
62 | $securityKey = ($this->isPHPModule() ? 'web' : 'cli'); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | if ($securityKey !== '') { |
||
67 | $securityKey .= '/'; |
||
68 | } |
||
69 | |||
70 | $securityKey = static::cleanFileName($securityKey); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Extends the temporary directory |
||
75 | * with the security key and the driver name |
||
76 | */ |
||
77 | $tmp_dir = rtrim($tmp_dir, '/') . DIRECTORY_SEPARATOR; |
||
78 | |||
79 | if (empty($this->getConfig()->getPath())) { |
||
80 | $path = $tmp_dir; |
||
81 | } else { |
||
82 | $path = rtrim($this->getConfig()->getPath(), '/') . DIRECTORY_SEPARATOR; |
||
83 | } |
||
84 | |||
85 | $path_suffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName(); |
||
86 | $full_path = Directory::getAbsolutePath($path . $path_suffix); |
||
87 | $full_path_tmp = Directory::getAbsolutePath($tmp_dir . $path_suffix); |
||
88 | $full_path_hash = \md5($full_path); |
||
89 | |||
90 | /** |
||
91 | * In readonly mode we only attempt |
||
92 | * to verify if the directory exists |
||
93 | * or not, if it does not then we |
||
94 | * return the temp dir |
||
95 | */ |
||
96 | if ($readonly === true) { |
||
97 | if ($this->getConfig()->isAutoTmpFallback() && (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
||
98 | return $full_path_tmp; |
||
99 | } |
||
100 | return $full_path; |
||
101 | } |
||
102 | |||
103 | if (!isset($this->tmp[ $full_path_hash ]) || (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
||
104 | if (!@\file_exists($full_path)) { |
||
105 | @mkdir($full_path, $this->getDefaultChmod(), true); |
||
106 | } else if (!@\is_writable($full_path)) { |
||
107 | if (!@chmod($full_path, $this->getDefaultChmod()) && $this->getConfig()->isAutoTmpFallback()) { |
||
108 | /** |
||
109 | * Switch back to tmp dir |
||
110 | * again if the path is not writable |
||
111 | */ |
||
112 | $full_path = $full_path_tmp; |
||
113 | if (!@\file_exists($full_path)) { |
||
114 | @mkdir($full_path, $this->getDefaultChmod(), true); |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * In case there is no directory |
||
121 | * writable including the temporary |
||
122 | * one, we must throw an exception |
||
123 | */ |
||
124 | if (!@\file_exists($full_path) || !@\is_writable($full_path)) { |
||
125 | throw new PhpfastcacheIOException('Path "' . $full_path . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !'); |
||
126 | } |
||
127 | |||
128 | $this->tmp[ $full_path_hash ] = $full_path; |
||
129 | $this->htaccessGen($full_path, \array_key_exists('htaccess', $this->getConfig()) ? $this->getConfig()->getHtaccess() : false); |
||
130 | } |
||
131 | |||
132 | return realpath($full_path); |
||
133 | } |
||
134 | |||
135 | |||
136 | /** |
||
137 | * @param $keyword |
||
138 | * @param bool $skip |
||
139 | * @return string |
||
140 | * @throws PhpfastcacheIOException |
||
141 | */ |
||
142 | protected function getFilePath($keyword, $skip = false): string |
||
143 | { |
||
144 | $path = $this->getPath(); |
||
145 | |||
146 | if ($keyword === false) { |
||
147 | return $path; |
||
148 | } |
||
149 | |||
150 | $filename = $this->encodeFilename($keyword); |
||
151 | $folder = \substr($filename, 0, 2) . DIRECTORY_SEPARATOR . \substr($filename, 2, 2); |
||
152 | $path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR . $folder; |
||
153 | |||
154 | /** |
||
155 | * Skip Create Sub Folders; |
||
156 | */ |
||
157 | if (!$skip) { |
||
158 | if (!\file_exists($path)) { |
||
159 | if (@!\mkdir($path, $this->getDefaultChmod(), true)) { |
||
160 | throw new PhpfastcacheIOException('PLEASE CHMOD ' . $path . ' - ' . $this->getDefaultChmod() . ' OR ANY WRITABLE PERMISSION!'); |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | |||
165 | return $path . '/' . $filename . '.' . $this->getConfig()->getCacheFileExtension(); |
||
166 | } |
||
167 | |||
168 | |||
169 | /** |
||
170 | * @param $keyword |
||
171 | * @return string |
||
172 | */ |
||
173 | protected function encodeFilename($keyword): string |
||
174 | { |
||
175 | return \md5($keyword); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @return int |
||
180 | */ |
||
181 | protected function getDefaultChmod(): int |
||
182 | { |
||
183 | if (!$this->getConfig()->getDefaultChmod()) { |
||
184 | return 0777; |
||
185 | } |
||
186 | |||
187 | return $this->getConfig()->getDefaultChmod(); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param $filename |
||
192 | * @return string |
||
193 | */ |
||
194 | protected static function cleanFileName($filename): string |
||
195 | { |
||
196 | $regex = [ |
||
197 | '/[\?\[\]\/\\\=\<\>\:\;\,\'\"\&\$\#\*\(\)\|\~\`\!\{\}]/', |
||
198 | '/\.$/', |
||
199 | '/^\./', |
||
200 | ]; |
||
201 | $replace = ['-', '', '']; |
||
202 | |||
203 | return \trim(preg_replace($regex, $replace, \trim($filename)), '-'); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param $path |
||
208 | * @param bool $create |
||
209 | * @throws PhpfastcacheIOException |
||
210 | */ |
||
211 | protected function htaccessGen($path, $create = true) |
||
212 | { |
||
213 | if ($create === true) { |
||
214 | if (!\is_writable($path)) { |
||
215 | try { |
||
216 | if (!\chmod($path, 0777)) { |
||
217 | throw new PhpfastcacheIOException('Chmod failed on : ' . $path); |
||
218 | } |
||
219 | } catch (PhpfastcacheIOException $e) { |
||
220 | throw new PhpfastcacheIOException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!', 0, $e); |
||
221 | } |
||
222 | } |
||
223 | |||
224 | if (!\file_exists($path . "/.htaccess")) { |
||
225 | $content = <<<HTACCESS |
||
226 | ### This .htaccess is auto-generated by PhpFastCache ### |
||
227 | <IfModule mod_authz_host> |
||
228 | Require all denied |
||
229 | </IfModule> |
||
230 | <IfModule !mod_authz_host> |
||
231 | Order Allow,Deny |
||
232 | Deny from all |
||
233 | </IfModule> |
||
234 | HTACCESS; |
||
235 | |||
236 | $file = @\fopen($path . '/.htaccess', 'w+'); |
||
237 | if (!$file) { |
||
238 | throw new PhpfastcacheIOException('PLEASE CHMOD ' . $path . ' - 0777 OR ANY WRITABLE PERMISSION!'); |
||
239 | } |
||
240 | \fwrite($file, $content); |
||
241 | \fclose($file); |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 | |||
246 | |||
247 | /** |
||
248 | * @param $file |
||
249 | * @return string |
||
250 | * @throws PhpfastcacheIOException |
||
251 | */ |
||
252 | protected function readfile($file): string |
||
253 | { |
||
254 | if (\function_exists('file_get_contents')) { |
||
255 | return \file_get_contents($file); |
||
256 | } |
||
257 | |||
258 | $string = ''; |
||
259 | |||
260 | $file_handle = @\fopen($file, 'r'); |
||
261 | if (!$file_handle) { |
||
262 | throw new PhpfastcacheIOException("Cannot read file located at: {$file}"); |
||
263 | } |
||
264 | while (!\feof($file_handle)) { |
||
265 | $line = \fgets($file_handle); |
||
266 | $string .= $line; |
||
267 | } |
||
268 | \fclose($file_handle); |
||
269 | |||
270 | return $string; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param string $file |
||
275 | * @param string $data |
||
276 | * @param bool $secureFileManipulation |
||
277 | * @return bool |
||
278 | * @throws PhpfastcacheIOException |
||
279 | */ |
||
280 | protected function writefile($file, $data, $secureFileManipulation = false): bool |
||
313 | } |
||
314 | |||
315 | /******************** |
||
316 | * |
||
317 | * PSR-6 Extended Methods |
||
318 | * |
||
319 | *******************/ |
||
320 | |||
321 | /** |
||
322 | * Provide a generic getStats() method |
||
323 | * for files-based drivers |
||
324 | * @return DriverStatistic |
||
325 | * @throws \Phpfastcache\Exceptions\PhpfastcacheIOException |
||
326 | */ |
||
327 | public function getStats(): DriverStatistic |
||
344 | } |
||
345 | } |