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