| Total Complexity | 43 |
| Total Lines | 273 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Cookie often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Cookie, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Cookie |
||
| 27 | { |
||
| 28 | |||
| 29 | private static $default = [ |
||
| 30 | 'name'=> null, |
||
| 31 | 'value'=>null, |
||
| 32 | 'maxage' => null, |
||
| 33 | 'expire' => null, |
||
| 34 | 'path' => null, |
||
| 35 | 'domain' => null, |
||
| 36 | 'secure' => false, |
||
| 37 | 'HTTPOnly' => false |
||
| 38 | ]; |
||
| 39 | |||
| 40 | private $data; |
||
| 41 | private $request; |
||
|
|
|||
| 42 | private $response; |
||
| 43 | private $responseCode; |
||
| 44 | |||
| 45 | |||
| 46 | public function __construct() |
||
| 47 | { |
||
| 48 | $this->data = self::$default; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param $name |
||
| 53 | * @param int $maxage time in second, for example : 60 = 1min |
||
| 54 | * @param string $path |
||
| 55 | * @param string $domain |
||
| 56 | * @param bool $secure |
||
| 57 | * @param bool $HTTPOnly |
||
| 58 | * |
||
| 59 | * @return Cookie |
||
| 60 | */ |
||
| 61 | public function createCookie($name, int $maxage = 0, string $path = '', string $domain = '', bool $secure = false, bool $HTTPOnly = false): Cookie |
||
| 62 | { |
||
| 63 | $this->setName($name) |
||
| 64 | ->setMaxage($maxage) |
||
| 65 | ->setPath($path) |
||
| 66 | ->setDomain($domain) |
||
| 67 | ->setSecure($secure) |
||
| 68 | ->setHTTPOnly($HTTPOnly); |
||
| 69 | |||
| 70 | return $this->set(); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function set(): Cookie |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get the value of maxage. |
||
| 97 | */ |
||
| 98 | private function getMaxage() : int |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Set the value of maxage. |
||
| 105 | * |
||
| 106 | */ |
||
| 107 | public function setMaxage($maxage) : Cookie |
||
| 108 | { |
||
| 109 | |||
| 110 | if (!is_null($maxage)) { |
||
| 111 | $maxage = intval($maxage); |
||
| 112 | $this->data['maxage'] = 'Expires=' .gmdate("D, d M Y H:i:s", $maxage > 0 ? time() + $maxage : 0)." GMT" . 'Max-Age=' . $maxage; |
||
| 113 | } |
||
| 114 | $this->data['maxage'] = $maxage; |
||
| 115 | return $this; |
||
| 116 | |||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get the value of name. |
||
| 121 | */ |
||
| 122 | private function getName() : string |
||
| 123 | { |
||
| 124 | return $this->data['name']; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Set the value of name. |
||
| 129 | * |
||
| 130 | */ |
||
| 131 | public function setName($name) : Cookie |
||
| 132 | { |
||
| 133 | if (is_array($name)) { |
||
| 134 | |||
| 135 | foreach ($name as $k => $v) { |
||
| 136 | $this->data['name'] = $k . '=' . rawurlencode($v); |
||
| 137 | } |
||
| 138 | } else { |
||
| 139 | $this->data['name'] = $name . '=' . rawurlencode($name); |
||
| 140 | } |
||
| 141 | |||
| 142 | return $this; |
||
| 143 | |||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get the value of path. |
||
| 148 | */ |
||
| 149 | private function getPath() : string |
||
| 150 | { |
||
| 151 | return $this->data['path']; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Set the value of path. |
||
| 156 | * |
||
| 157 | */ |
||
| 158 | public function setPath($path) : Cookie |
||
| 159 | { |
||
| 160 | $this->data['path'] = $path; |
||
| 161 | return $this; |
||
| 162 | |||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get the value of domain. |
||
| 167 | */ |
||
| 168 | private function getDomain() |
||
| 169 | { |
||
| 170 | return $this->data['domain']; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Set the value of domain. |
||
| 175 | * |
||
| 176 | */ |
||
| 177 | public function setDomain($domain) |
||
| 178 | { |
||
| 179 | |||
| 180 | if (!empty($domain)) { |
||
| 181 | // Fix the domain to accept domains with and without 'www.'. |
||
| 182 | if (strtolower(substr($domain, 0, 4)) == 'www.') { |
||
| 183 | $this->data['domain'] = substr($domain, 4); |
||
| 184 | } |
||
| 185 | |||
| 186 | // Add the dot prefix to ensure compatibility with subdomains |
||
| 187 | if (substr($domain, 0, 1) != '.') { |
||
| 188 | $this->data['domain'] = '.' . $domain; |
||
| 189 | } |
||
| 190 | // Remove port information. |
||
| 191 | $port = strpos($domain, ':'); |
||
| 192 | if (false !== $port) { |
||
| 193 | $this->data['domain'] = substr($domain, 0, $port); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get the value of secure. |
||
| 202 | */ |
||
| 203 | private function getSecure() : bool |
||
| 204 | { |
||
| 205 | return $this->data['secure']; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Set the value of secure. |
||
| 210 | * |
||
| 211 | */ |
||
| 212 | public function setSecure($secure) : Cookie |
||
| 213 | { |
||
| 214 | $this->data['secure'] = $secure; |
||
| 215 | return $this; |
||
| 216 | |||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get the value of HTTPOnly. |
||
| 221 | */ |
||
| 222 | private function getHTTPOnly() : bool |
||
| 223 | { |
||
| 224 | return $this->data['HTTPOnly']; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Set the value of HTTPOnly. |
||
| 229 | * |
||
| 230 | */ |
||
| 231 | public function setHTTPOnly($HTTPOnly) : Cookie |
||
| 232 | { |
||
| 233 | $this->data['HTTPOnly'] = $HTTPOnly; |
||
| 234 | return $this; |
||
| 235 | |||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return mixed |
||
| 240 | */ |
||
| 241 | private function getResponseCode() |
||
| 242 | { |
||
| 243 | return $this->responseCode; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param mixed $responseCode |
||
| 248 | */ |
||
| 249 | public function setResponseCode($responseCode) |
||
| 250 | { |
||
| 251 | $this->responseCode = $responseCode; |
||
| 252 | return $this; |
||
| 253 | } |
||
| 254 | |||
| 255 | public function getValue() |
||
| 258 | } |
||
| 259 | |||
| 260 | public function setValue($value) { |
||
| 262 | |||
| 263 | } |
||
| 264 | |||
| 265 | private function validate() |
||
| 299 | } |
||
| 300 | } |
||
| 301 |