| Conditions | 47 |
| Paths | 14596 |
| Total Lines | 140 |
| Code Lines | 108 |
| Lines | 14 |
| Ratio | 10 % |
| 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 |
||
| 76 | // If $key is NULL, it means that the whole $source is requested |
||
| 77 | if (!isset($key) || $key == null) { |
||
| 78 | $key = array_keys($source); |
||
| 79 | } |
||
| 80 | |||
| 81 | if (is_array($key)) { |
||
| 82 | $output = array(); |
||
| 83 | foreach ($key as $k) { |
||
| 84 | $output[$k] = $this->getRequestParams($source, $k); |
||
| 85 | } |
||
| 86 | |||
| 87 | return $output; |
||
| 88 | } |
||
| 89 | |||
| 90 | if (isset($source[$key])) { |
||
| 91 | $value = $source[$key]; |
||
| 92 | } else { |
||
| 93 | return null; |
||
| 94 | } |
||
| 95 | |||
| 96 | return $value; |
||
| 97 | |||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Fetch an item from the GET array |
||
| 102 | * |
||
| 103 | * @param mixed $key Index for item to be fetched from $_GET |
||
| 104 | * @return mixed |
||
| 105 | */ |
||
| 106 | public function get($key = null) |
||
| 107 | { |
||
| 108 | return $this->getRequestParams($_GET, $key); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Fetch an item from the POST array |
||
| 113 | * |
||
| 114 | * @param mixed $key Index for item to be fetched from $_POST |
||
| 115 | * @return mixed |
||
| 116 | */ |
||
| 117 | public function post($key = null) |
||
| 118 | { |
||
| 119 | $rawPostData = file_get_contents('php://input'); |
||
| 120 | $source = json_decode($rawPostData, true); |
||
| 121 | if (json_last_error() != JSON_ERROR_NONE) { |
||
| 122 | $source = $_POST; |
||
| 123 | } |
||
| 124 | |||
| 125 | return $this->getRequestParams($source, $key); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Fetch an item from the SERVER array |
||
| 130 | * |
||
| 131 | * @param mixed $key Index for item to be fetched from $_SERVER |
||
| 132 | * @return mixed |
||
| 133 | */ |
||
| 134 | public function server($key = null) |
||
| 135 | { |
||
| 136 | return $this->getRequestParams($_SERVER, $key); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Set or Get cookie |
||
| 141 | * |
||
| 142 | * @param mixed $key Index for item for cookie |
||
| 143 | * @param string $value cookie value |
||
| 144 | * @param mixed options for cookie setting |
||
| 145 | * @return mixed |
||
| 146 | */ |
||
| 147 | public function cookie($key = '', $value = '', $options = null) |
||
| 148 | { |
||
| 149 | $defaultOptions = [ |
||
| 150 | 'prefix' => '', |
||
| 151 | 'expire' => 86400, |
||
| 152 | 'path' => '/', |
||
| 153 | 'secure' => false, |
||
| 154 | 'httponly' => false, |
||
| 155 | ]; |
||
| 156 | |||
| 157 | if (!is_null($options)) { |
||
| 158 | if (is_numeric($options)) { |
||
| 159 | $options = ['expire' => $options]; |
||
| 160 | } elseif (is_string($options)) { |
||
| 161 | parse_str($options, $options); |
||
| 162 | } |
||
| 163 | |||
| 164 | $options = array_merge($defaultOptions, array_change_key_case($options)); |
||
| 165 | } |
||
| 166 | |||
| 167 | if (!empty($options['httponly'])) { |
||
| 168 | ini_set('session.cookie_httponly', 1); |
||
| 169 | } |
||
| 170 | |||
| 171 | if (is_null($key)) { |
||
| 172 | if (empty($_COOKIE)) { |
||
| 173 | return null; |
||
| 174 | } |
||
| 175 | |||
| 176 | $prefix = empty($value) ? $options['prefix'] : $value; |
||
| 177 | if (!empty($prefix)) { |
||
| 178 | foreach ($_COOKIE as $key => $val) { |
||
| 179 | if (0 === stripos($key, $prefix)) { |
||
| 180 | setcookie($key, '', time() - 3600, $options['path'], $options['domain'], $options['secure'], $options['httponly']); |
||
| 181 | unset($_COOKIE[$key]); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | return null; |
||
| 187 | } elseif ('' === $key) { |
||
| 188 | // Get All Cookie |
||
| 189 | return $_COOKIE; |
||
| 190 | } |
||
| 191 | |||
| 192 | $key = $options['prefix'] . str_replace('.', '_', $key); |
||
| 193 | if ('' === $value) { |
||
| 194 | if (isset($_COOKIE[$key])) { |
||
| 195 | $value = $_COOKIE[$key]; |
||
| 196 | if (0 === strpos($value, 'kotori:')) { |
||
| 197 | $value = substr($value, 6); |
||
| 198 | return array_map('urldecode', json_decode(MAGIC_QUOTES_GPC ? stripslashes($value) : $value, true)); |
||
| 199 | } else { |
||
| 200 | return $value; |
||
| 201 | } |
||
| 202 | } else { |
||
| 203 | return null; |
||
| 204 | } |
||
| 205 | } else { |
||
| 206 | if (is_null($value)) { |
||
| 207 | setcookie($key, '', time() - 3600, $options['path'], $options['domain'], $options['secure'], $options['httponly']); |
||
| 208 | unset($_COOKIE[$key]); // Delete Cookie |
||
| 209 | } else { |
||
| 210 | // Set Cookie |
||
| 211 | if (is_array($value)) { |
||
| 212 | $value = 'kotori:' . json_encode(array_map('urlencode', $value)); |
||
| 213 | } |
||
| 214 | |||
| 215 | $expire = !empty($options['expire']) ? time() + intval($options['expire']) : 0; |
||
| 216 | setcookie($key, $value, $expire, $options['path'], $options['domain'], $options['secure'], $options['httponly']); |
||
| 403 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.