| Conditions | 22 |
| Paths | 288 |
| Total Lines | 75 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 33 | public function getPath($readonly = false) |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Get the base system temporary directory |
||
| 37 | */ |
||
| 38 | $tmp_dir = rtrim(ini_get('upload_tmp_dir') ?: sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache'; |
||
| 39 | /** |
||
| 40 | * Calculate the security key |
||
| 41 | */ |
||
| 42 | { |
||
| 43 | $securityKey = array_key_exists('securityKey', $this->config) ? $this->config[ 'securityKey' ] : ''; |
||
| 44 | if (!$securityKey || $securityKey === 'auto') { |
||
| 45 | if (isset($_SERVER[ 'HTTP_HOST' ])) { |
||
| 46 | $securityKey = preg_replace('/^www./', '', strtolower(str_replace(':', '_', $_SERVER[ 'HTTP_HOST' ]))); |
||
| 47 | } else { |
||
| 48 | $securityKey = ($this->isPHPModule() ? 'web' : 'cli'); |
||
| 49 | } |
||
| 50 | } |
||
| 51 | if ($securityKey !== '') { |
||
| 52 | $securityKey .= '/'; |
||
| 53 | } |
||
| 54 | $securityKey = static::cleanFileName($securityKey); |
||
| 55 | } |
||
| 56 | /** |
||
| 57 | * Extends the temporary directory |
||
| 58 | * with the security key and the driver name |
||
| 59 | */ |
||
| 60 | $tmp_dir = rtrim($tmp_dir, '/') . DIRECTORY_SEPARATOR; |
||
| 61 | if (empty($this->config[ 'path' ]) || !is_string($this->config[ 'path' ])) { |
||
| 62 | $path = $tmp_dir; |
||
| 63 | } else { |
||
| 64 | $path = rtrim($this->config[ 'path' ], '/') . DIRECTORY_SEPARATOR; |
||
| 65 | } |
||
| 66 | $path_suffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName(); |
||
| 67 | $full_path = Directory::getAbsolutePath($path . $path_suffix); |
||
| 68 | $full_path_tmp = Directory::getAbsolutePath($tmp_dir . $path_suffix); |
||
| 69 | $full_path_hash = md5($full_path); |
||
| 70 | /** |
||
| 71 | * In readonly mode we only attempt |
||
| 72 | * to verify if the directory exists |
||
| 73 | * or not, if it does not then we |
||
| 74 | * return the temp dir |
||
| 75 | */ |
||
| 76 | if ($readonly === true) { |
||
| 77 | if(!@file_exists($full_path) || !@is_writable($full_path)){ |
||
| 78 | return $full_path_tmp; |
||
| 79 | } |
||
| 80 | return $full_path; |
||
| 81 | }else{ |
||
| 82 | if (!isset($this->tmp[ $full_path_hash ]) || (!@file_exists($full_path) || !@is_writable($full_path))) { |
||
| 83 | if (!@file_exists($full_path)) { |
||
| 84 | @mkdir($full_path, $this->setChmodAuto(), true); |
||
| 85 | } elseif (!@is_writable($full_path)) { |
||
| 86 | if (!@chmod($full_path, $this->setChmodAuto())) |
||
| 87 | { |
||
| 88 | /** |
||
| 89 | * Switch back to tmp dir |
||
| 90 | * again if the path is not writable |
||
| 91 | */ |
||
| 92 | $full_path = $full_path_tmp; |
||
| 93 | if (!@file_exists($full_path)) { |
||
| 94 | if (!@mkdir($full_path, $this->setChmodAuto(), true)) |
||
| 95 | { |
||
| 96 | throw new phpFastCacheDriverException('PLEASE CREATE OR CHMOD ' . $full_path . ' - 0777 OR ANY WRITABLE PERMISSION!'); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | $this->tmp[ $full_path_hash ] = $full_path; |
||
| 103 | $this->htaccessGen($full_path, array_key_exists('htaccess', $this->config) ? $this->config[ 'htaccess' ] : false); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | return realpath($full_path); |
||
| 107 | } |
||
| 108 | |||
| 232 | } |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.