| Conditions | 23 |
| Paths | 208 |
| Total Lines | 87 |
| Code Lines | 36 |
| 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 |
||
| 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 | } |
||
| 345 | } |