Complex classes like Identity 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Identity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Identity extends Resource |
||
| 18 | { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The unique id |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $id; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Type: browser or robot |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $type; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The Address |
||
| 36 | * |
||
| 37 | * @var Address |
||
| 38 | */ |
||
| 39 | protected $address; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The Signature |
||
| 43 | * |
||
| 44 | * @var Signature |
||
| 45 | */ |
||
| 46 | protected $signature; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The HTTP Headers |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $headers; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The User Agent |
||
| 57 | * |
||
| 58 | * @var UserAgent |
||
| 59 | */ |
||
| 60 | protected $userAgent; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Session |
||
| 64 | * |
||
| 65 | * @var Session |
||
| 66 | */ |
||
| 67 | protected $session; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Reputation |
||
| 71 | * |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | protected $reputation; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param array $attributes |
||
| 78 | */ |
||
| 79 | public function __construct($attributes = null) |
||
| 80 | { |
||
| 81 | parent::__construct($attributes); |
||
| 82 | $address = $this->getAddress(); |
||
| 83 | $signature = $this->getSignature(); |
||
| 84 | if ($address && $signature) { |
||
| 85 | $this->id = Bouncer::hash($signature->getId() . $address->getId()); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /* |
||
| 90 | * @return string|null |
||
| 91 | */ |
||
| 92 | public function getId() |
||
| 96 | |||
| 97 | /* |
||
| 98 | * @param string |
||
| 99 | */ |
||
| 100 | public function setId($id) |
||
| 104 | |||
| 105 | /* |
||
| 106 | * @return string|null |
||
| 107 | */ |
||
| 108 | public function getType() |
||
| 112 | |||
| 113 | /* |
||
| 114 | * @param string |
||
| 115 | */ |
||
| 116 | public function setType($type) |
||
| 120 | |||
| 121 | /* |
||
| 122 | * @return Address|null |
||
| 123 | */ |
||
| 124 | public function getAddress() |
||
| 128 | |||
| 129 | /* |
||
| 130 | * @param string|array|object $address |
||
| 131 | */ |
||
| 132 | public function setAddress($address) |
||
| 140 | |||
| 141 | /* |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function getHeaders() |
||
| 148 | |||
| 149 | /* |
||
| 150 | * @param array $headers |
||
| 151 | */ |
||
| 152 | public function setHeaders($headers) |
||
| 158 | |||
| 159 | /* |
||
| 160 | * @return Signature|null |
||
| 161 | */ |
||
| 162 | public function getSignature() |
||
| 166 | |||
| 167 | /* |
||
| 168 | * @param array|object $signature |
||
| 169 | */ |
||
| 170 | public function setSignature($signature) |
||
| 178 | |||
| 179 | /* |
||
| 180 | * @return UserAgent|null |
||
| 181 | */ |
||
| 182 | public function getUserAgent() |
||
| 186 | |||
| 187 | /* |
||
| 188 | * @param array|object $userAgent |
||
| 189 | */ |
||
| 190 | public function setUserAgent($userAgent) |
||
| 198 | |||
| 199 | /* |
||
| 200 | * @return Session|null |
||
| 201 | */ |
||
| 202 | public function getSession() |
||
| 203 | { |
||
| 204 | return $this->session; |
||
| 205 | } |
||
| 206 | |||
| 207 | /* |
||
| 208 | * @param string|array|object $session |
||
| 209 | */ |
||
| 210 | public function setSession($session) |
||
| 211 | { |
||
| 212 | if (is_object($session)) { |
||
| 213 | $this->session = $session; |
||
| 214 | } elseif (is_string($session) || is_array($session)) { |
||
| 215 | $this->session = new Session($session); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | /* |
||
| 220 | * @return array|null |
||
| 221 | */ |
||
| 222 | public function getReputation() |
||
| 226 | |||
| 227 | /* |
||
| 228 | * @param array $reputation |
||
| 229 | */ |
||
| 230 | public function setReputation($reputation) |
||
| 231 | { |
||
| 232 | $this->reputation = $reputation; |
||
| 233 | } |
||
| 234 | |||
| 235 | /* |
||
| 236 | * @return string|null |
||
| 237 | */ |
||
| 238 | public function getStatus() |
||
| 245 | |||
| 246 | /* |
||
| 247 | * @return bool |
||
| 248 | */ |
||
| 249 | public function isNice() |
||
| 250 | { |
||
| 251 | return $this->getStatus() === Bouncer::NICE; |
||
| 252 | } |
||
| 253 | |||
| 254 | /* |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public function isSuspicious() |
||
| 261 | |||
| 262 | /* |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | public function isBad() |
||
| 269 | |||
| 270 | /* |
||
| 271 | * @return string|null |
||
| 272 | */ |
||
| 273 | public function getAgentName() |
||
| 274 | { |
||
| 275 | $userAgent = $this->getUserAgent(); |
||
| 276 | if ($userAgent) { |
||
| 277 | return $userAgent->getAgentName(); |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | /* |
||
| 282 | * @return string|null |
||
| 283 | */ |
||
| 284 | public function getSystemName() |
||
| 285 | { |
||
| 286 | $userAgent = $this->getUserAgent(); |
||
| 287 | if ($userAgent) { |
||
| 288 | return $userAgent->getSystemName(); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | /* |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | public function toArray() |
||
| 305 | |||
| 306 | } |
||
| 307 |