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