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