| Conditions | 27 |
| Paths | 240 |
| Total Lines | 93 |
| Code Lines | 39 |
| 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 |
||
| 48 | public function getPath($readonly = false): string |
||
| 49 | { |
||
| 50 | /** |
||
| 51 | * Get the base system temporary directory |
||
| 52 | */ |
||
| 53 | $tmp_dir = \rtrim(\ini_get('upload_tmp_dir') ?: \sys_get_temp_dir(), '\\/') . \DIRECTORY_SEPARATOR . 'phpfastcache'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Calculate the security key |
||
| 57 | */ |
||
| 58 | { |
||
| 59 | $securityKey = $this->getConfig()->getSecurityKey(); |
||
| 60 | if (!$securityKey || \mb_strtolower($securityKey) === 'auto') { |
||
| 61 | if (isset($_SERVER['HTTP_HOST'])) { |
||
| 62 | $securityKey = \preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $_SERVER['HTTP_HOST']))); |
||
| 63 | } else { |
||
| 64 | $securityKey = ($this->isPHPModule() ? 'web' : 'cli'); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | if ($securityKey !== '') { |
||
| 69 | $securityKey .= '/'; |
||
| 70 | } |
||
| 71 | |||
| 72 | $securityKey = static::cleanFileName($securityKey); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Extends the temporary directory |
||
| 77 | * with the security key and the driver name |
||
| 78 | */ |
||
| 79 | $tmp_dir = \rtrim($tmp_dir, '/') . \DIRECTORY_SEPARATOR; |
||
| 80 | |||
| 81 | if (empty($this->getConfig()->getPath())) { |
||
| 82 | $path = $tmp_dir; |
||
| 83 | } else { |
||
| 84 | $path = rtrim($this->getConfig()->getPath(), '/') . \DIRECTORY_SEPARATOR; |
||
| 85 | } |
||
| 86 | |||
| 87 | $path_suffix = $securityKey . \DIRECTORY_SEPARATOR . $this->getDriverName(); |
||
| 88 | $full_path = Directory::getAbsolutePath($path . $path_suffix); |
||
| 89 | $full_path_tmp = Directory::getAbsolutePath($tmp_dir . $path_suffix); |
||
| 90 | $full_path_hash = $this->getConfig()->getDefaultFileNameHashFunction()($full_path); |
||
| 91 | |||
| 92 | /** |
||
| 93 | * In readonly mode we only attempt |
||
| 94 | * to verify if the directory exists |
||
| 95 | * or not, if it does not then we |
||
| 96 | * return the temp dir |
||
| 97 | */ |
||
| 98 | if ($readonly === true) { |
||
| 99 | if ($this->getConfig()->isAutoTmpFallback() && (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
||
| 100 | return $full_path_tmp; |
||
| 101 | } |
||
| 102 | return $full_path; |
||
| 103 | } |
||
| 104 | |||
| 105 | if (!isset($this->tmp[$full_path_hash]) || (!@\file_exists($full_path) || !@\is_writable($full_path))) { |
||
| 106 | if (!@\file_exists($full_path)) { |
||
| 107 | if (@mkdir($full_path, $this->getDefaultChmod(), true) === false && !\is_dir($full_path) ) { |
||
| 108 | throw new PhpfastcacheIOException('The directory '.$full_path.' could not be created.'); |
||
| 109 | } |
||
| 110 | } else { |
||
| 111 | if (!@\is_writable($full_path)) { |
||
| 112 | if (!@chmod($full_path, $this->getDefaultChmod()) && $this->getConfig()->isAutoTmpFallback()) { |
||
| 113 | /** |
||
| 114 | * Switch back to tmp dir |
||
| 115 | * again if the path is not writable |
||
| 116 | */ |
||
| 117 | $full_path = $full_path_tmp; |
||
| 118 | if (!@\file_exists($full_path)) { |
||
| 119 | if(@mkdir($full_path, $this->getDefaultChmod(), true) && !\is_dir($full_path)){ |
||
| 120 | throw new PhpfastcacheIOException('The directory '.$full_path.' could not be created.'); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * In case there is no directory |
||
| 129 | * writable including the temporary |
||
| 130 | * one, we must throw an exception |
||
| 131 | */ |
||
| 132 | if (!@\file_exists($full_path) || !@\is_writable($full_path)) { |
||
| 133 | 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 !'); |
||
| 134 | } |
||
| 135 | |||
| 136 | $this->tmp[$full_path_hash] = $full_path; |
||
| 137 | $this->htaccessGen($full_path, $this->getConfig()->isValidOption('htaccess') ? $this->getConfig()->getHtaccess() : false); |
||
| 138 | } |
||
| 139 | |||
| 140 | return realpath($full_path); |
||
| 141 | } |
||
| 353 | } |