| Conditions | 26 |
| Paths | 176 |
| Total Lines | 89 |
| Code Lines | 37 |
| 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 |
||
| 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 | } |
||
| 312 |