Total Complexity | 48 |
Total Lines | 284 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
26 | trait IOHelperTrait |
||
27 | { |
||
28 | use TaggableCacheItemPoolTrait; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | public array $tmp = []; |
||
34 | |||
35 | /** |
||
36 | * Provide a generic getStats() method |
||
37 | * for files-based drivers |
||
38 | * @return DriverStatistic |
||
39 | * @throws PhpfastcacheIOException |
||
40 | */ |
||
41 | public function getStats(): DriverStatistic |
||
42 | { |
||
43 | $stat = new DriverStatistic(); |
||
44 | $path = $this->getFilePath(false); |
||
45 | |||
46 | if (!is_dir($path)) { |
||
47 | throw new PhpfastcacheIOException("Can't read PATH:" . $path); |
||
48 | } |
||
49 | $stat->setRawData( |
||
50 | [ |
||
51 | 'tmp' => $this->tmp, |
||
52 | ] |
||
53 | ) |
||
54 | ->setSize(Directory::dirSize($path)) |
||
55 | ->setInfo('Number of files used to build the cache: ' . Directory::getFileCount($path)); |
||
56 | |||
57 | if ($this->getConfig()->isUseStaticItemCaching()) { |
||
58 | $stat->setData(implode(', ', \array_keys($this->itemInstances))); |
||
59 | } else { |
||
60 | $stat->setData('No data available since static item caching option (useStaticItemCaching) is disabled.'); |
||
61 | } |
||
62 | |||
63 | return $stat; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param string|bool $keyword |
||
68 | * @param bool $skip |
||
69 | * @return string |
||
70 | * @throws PhpfastcacheIOException |
||
71 | */ |
||
72 | protected function getFilePath(string|bool $keyword, bool $skip = false): string |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param bool $readonly |
||
98 | * @return string |
||
99 | * @throws PhpfastcacheIOException |
||
100 | * @throws PhpfastcacheInvalidArgumentException |
||
101 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
102 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
103 | */ |
||
104 | public function getPath(bool $readonly = false): string |
||
105 | { |
||
106 | /** |
||
107 | * Get the base system temporary directory |
||
108 | */ |
||
109 | $tmpDir = \rtrim(\ini_get('upload_tmp_dir') ?: \sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache'; |
||
110 | |||
111 | /** |
||
112 | * Calculate the security key |
||
113 | */ |
||
114 | { |
||
115 | $httpHost = $this->getConfig()->getSuperGlobalAccessor()('SERVER', 'HTTP_HOST'); |
||
116 | $securityKey = $this->getConfig()->getSecurityKey(); |
||
117 | if (!$securityKey || \mb_strtolower($securityKey) === 'auto') { |
||
118 | if (isset($httpHost)) { |
||
119 | $securityKey = \preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $httpHost))); |
||
120 | } else { |
||
121 | $securityKey = (SapiDetector::isWebScript() ? 'web' : 'cli'); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | if ($securityKey !== '') { |
||
126 | $securityKey .= '/'; |
||
127 | } |
||
128 | |||
129 | $securityKey = static::cleanFileName($securityKey); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Extends the temporary directory |
||
134 | * with the security key and the driver name |
||
135 | */ |
||
136 | $tmpDir = \rtrim($tmpDir, '/') . DIRECTORY_SEPARATOR; |
||
137 | |||
138 | if (empty($this->getConfig()->getPath())) { |
||
139 | $path = $tmpDir; |
||
140 | } else { |
||
141 | $path = \rtrim($this->getConfig()->getPath(), '/') . DIRECTORY_SEPARATOR; |
||
142 | } |
||
143 | |||
144 | $pathSuffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName(); |
||
145 | $fullPath = Directory::getAbsolutePath($path . $pathSuffix); |
||
146 | $fullPathTmp = Directory::getAbsolutePath($tmpDir . $pathSuffix); |
||
147 | $fullPathHash = $this->getConfig()->getDefaultFileNameHashFunction()($fullPath); |
||
148 | |||
149 | /** |
||
150 | * In readonly mode we only attempt |
||
151 | * to verify if the directory exists |
||
152 | * or not, if it does not then we |
||
153 | * return the temp dir |
||
154 | */ |
||
155 | if ($readonly === true) { |
||
156 | if ($this->getConfig()->isAutoTmpFallback() && (!@\file_exists($fullPath) || !@\is_writable($fullPath))) { |
||
157 | return $fullPathTmp; |
||
158 | } |
||
159 | return $fullPath; |
||
160 | } |
||
161 | |||
162 | if (!isset($this->tmp[$fullPathHash]) || (!@\file_exists($fullPath) || !@\is_writable($fullPath))) { |
||
163 | if (!@\file_exists($fullPath)) { |
||
164 | if (@mkdir($fullPath, $this->getDefaultChmod(), true) === false && !\is_dir($fullPath)) { |
||
165 | throw new PhpfastcacheIOException('The directory ' . $fullPath . ' could not be created.'); |
||
166 | } |
||
167 | } elseif (!@\is_writable($fullPath) && !@\chmod($fullPath, $this->getDefaultChmod()) && $this->getConfig()->isAutoTmpFallback()) { |
||
168 | /** |
||
169 | * Switch back to tmp dir |
||
170 | * again if the path is not writable |
||
171 | */ |
||
172 | $fullPath = $fullPathTmp; |
||
173 | if (!@\file_exists($fullPath) && @\mkdir($fullPath, $this->getDefaultChmod(), true) && !\is_dir($fullPath)) { |
||
174 | throw new PhpfastcacheIOException('The directory ' . $fullPath . ' could not be created.'); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * In case there is no directory |
||
180 | * writable including the temporary |
||
181 | * one, we must throw an exception |
||
182 | */ |
||
183 | if (!@\file_exists($fullPath) || !@\is_writable($fullPath)) { |
||
184 | throw new PhpfastcacheIOException( |
||
185 | 'Path "' . $fullPath . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !' |
||
186 | ); |
||
187 | } |
||
188 | |||
189 | $this->tmp[$fullPathHash] = $fullPath; |
||
190 | } |
||
191 | |||
192 | return realpath($fullPath); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @param string $filename |
||
197 | * @return string |
||
198 | */ |
||
199 | protected static function cleanFileName(string $filename): string |
||
200 | { |
||
201 | $regex = [ |
||
202 | '/[\?\[\]\/\\\=\<\>\:\;\,\'\"\&\$\#\*\(\)\|\~\`\!\{\}]/', |
||
203 | '/\.$/', |
||
204 | '/^\./', |
||
205 | ]; |
||
206 | $replace = ['-', '', '']; |
||
207 | |||
208 | return \trim(\preg_replace($regex, $replace, \trim($filename)), '-'); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @return int |
||
213 | */ |
||
214 | protected function getDefaultChmod(): int |
||
215 | { |
||
216 | if (!$this->getConfig()->getDefaultChmod()) { |
||
217 | return 0777; |
||
218 | } |
||
219 | |||
220 | return $this->getConfig()->getDefaultChmod(); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @param string $keyword |
||
225 | * @return string |
||
226 | */ |
||
227 | protected function encodeFilename(string $keyword): string |
||
228 | { |
||
229 | return $this->getConfig()->getDefaultFileNameHashFunction()($keyword); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param string $file |
||
234 | * @return string |
||
235 | * @throws PhpfastcacheIOException |
||
236 | */ |
||
237 | protected function readFile(string $file): string |
||
238 | { |
||
239 | if (!\is_readable($file)) { |
||
240 | throw new PhpfastcacheIOException("Cannot read file located at: $file"); |
||
241 | } |
||
242 | if (\function_exists('file_get_contents')) { |
||
243 | return (string)\file_get_contents($file); |
||
244 | } |
||
245 | |||
246 | $string = ''; |
||
247 | |||
248 | $fileHandle = @\fopen($file, 'rb'); |
||
249 | while (!\feof($fileHandle)) { |
||
250 | $line = \fgets($fileHandle); |
||
251 | $string .= $line; |
||
252 | } |
||
253 | \fclose($fileHandle); |
||
254 | |||
255 | return $string; |
||
256 | } |
||
257 | |||
258 | /******************** |
||
259 | * |
||
260 | * PSR-6 Extended Methods |
||
261 | * |
||
262 | *******************/ |
||
263 | |||
264 | /** |
||
265 | * @param string $file |
||
266 | * @param string $data |
||
267 | * @param bool $secureFileManipulation |
||
268 | * @return bool |
||
269 | * @throws PhpfastcacheIOException |
||
270 | * @throws \Exception |
||
271 | */ |
||
272 | protected function writeFile(string $file, string $data, bool $secureFileManipulation = false): bool |
||
310 | } |
||
311 | } |
||
312 |