Conditions | 27 |
Paths | 240 |
Total Lines | 95 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
107 | public function getPath($readonly = false): string |
||
108 | { |
||
109 | /** |
||
110 | * Get the base system temporary directory |
||
111 | */ |
||
112 | $tmp_dir = \rtrim(\ini_get('upload_tmp_dir') ?: \sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache'; |
||
113 | |||
114 | /** |
||
115 | * Calculate the security key |
||
116 | */ |
||
117 | { |
||
118 | $securityKey = $this->getConfig()->getSecurityKey(); |
||
119 | if (!$securityKey || \mb_strtolower($securityKey) === 'auto') { |
||
120 | if (isset($_SERVER['HTTP_HOST'])) { |
||
121 | $securityKey = \preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $_SERVER['HTTP_HOST']))); |
||
122 | } else { |
||
123 | $securityKey = ($this->isPHPModule() ? 'web' : 'cli'); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | if ($securityKey !== '') { |
||
128 | $securityKey .= '/'; |
||
129 | } |
||
130 | |||
131 | $securityKey = static::cleanFileName($securityKey); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Extends the temporary directory |
||
136 | * with the security key and the driver name |
||
137 | */ |
||
138 | $tmp_dir = \rtrim($tmp_dir, '/') . DIRECTORY_SEPARATOR; |
||
139 | |||
140 | if (empty($this->getConfig()->getPath())) { |
||
141 | $path = $tmp_dir; |
||
142 | } else { |
||
143 | $path = \rtrim($this->getConfig()->getPath(), '/') . DIRECTORY_SEPARATOR; |
||
144 | } |
||
145 | |||
146 | $path_suffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName(); |
||
147 | $full_path = Directory::getAbsolutePath($path . $path_suffix); |
||
148 | $full_path_tmp = Directory::getAbsolutePath($tmp_dir . $path_suffix); |
||
149 | $full_path_hash = $this->getConfig()->getDefaultFileNameHashFunction()($full_path); |
||
150 | |||
151 | /** |
||
152 | * In readonly mode we only attempt |
||
153 | * to verify if the directory exists |
||
154 | * or not, if it does not then we |
||
155 | * return the temp dir |
||
156 | */ |
||
157 | if ($readonly === true) { |
||
158 | if ($this->getConfig()->isAutoTmpFallback() && (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
||
159 | return $full_path_tmp; |
||
160 | } |
||
161 | return $full_path; |
||
162 | } |
||
163 | |||
164 | if (!isset($this->tmp[$full_path_hash]) || (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
||
165 | if (!@\file_exists($full_path)) { |
||
166 | if (@mkdir($full_path, $this->getDefaultChmod(), true) === false && !\is_dir($full_path)) { |
||
167 | throw new PhpfastcacheIOException('The directory ' . $full_path . ' could not be created.'); |
||
168 | } |
||
169 | } else { |
||
170 | if (!@\is_writable($full_path)) { |
||
171 | if (!@\chmod($full_path, $this->getDefaultChmod()) && $this->getConfig()->isAutoTmpFallback()) { |
||
172 | /** |
||
173 | * Switch back to tmp dir |
||
174 | * again if the path is not writable |
||
175 | */ |
||
176 | $full_path = $full_path_tmp; |
||
177 | if (!@\file_exists($full_path)) { |
||
178 | if (@\mkdir($full_path, $this->getDefaultChmod(), true) && !\is_dir($full_path)) { |
||
179 | throw new PhpfastcacheIOException('The directory ' . $full_path . ' could not be created.'); |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * In case there is no directory |
||
188 | * writable including the temporary |
||
189 | * one, we must throw an exception |
||
190 | */ |
||
191 | if (!@\file_exists($full_path) || !@\is_writable($full_path)) { |
||
192 | throw new PhpfastcacheIOException( |
||
193 | '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 !' |
||
194 | ); |
||
195 | } |
||
196 | |||
197 | $this->tmp[$full_path_hash] = $full_path; |
||
198 | $this->htaccessGen($full_path, $this->getConfig()->isValidOption('htaccess') ? $this->getConfig()->getHtaccess() : false); |
||
199 | } |
||
200 | |||
201 | return realpath($full_path); |
||
202 | } |
||
362 |