Complex classes like Locator 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 Locator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | class Locator implements LocatorInterface |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * Date PCRE pattern |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected static $datePattern = [ |
||
| 58 | 'Y' => '(?P<year>\d{4})', |
||
| 59 | 'm' => '(?P<month>\d{2})', |
||
| 60 | 'd' => '(?P<day>\d{2})', |
||
| 61 | 'H' => '(?P<hour>\d{2})', |
||
| 62 | 'i' => '(?P<minute>\d{2})', |
||
| 63 | 's' => '(?P<second>\d{2})', |
||
| 64 | ]; |
||
| 65 | /** |
||
| 66 | * Creation date |
||
| 67 | * |
||
| 68 | * @var \DateTimeInterface |
||
| 69 | */ |
||
| 70 | protected $creationDate = null; |
||
| 71 | /** |
||
| 72 | * Object ID |
||
| 73 | * |
||
| 74 | * @var Id |
||
| 75 | */ |
||
| 76 | protected $uid = null; |
||
| 77 | /** |
||
| 78 | * Object type |
||
| 79 | * |
||
| 80 | * @var Type |
||
| 81 | */ |
||
| 82 | protected $type = null; |
||
| 83 | /** |
||
| 84 | * Object revision |
||
| 85 | * |
||
| 86 | * @var Revision |
||
| 87 | */ |
||
| 88 | protected $revision = null; |
||
| 89 | /** |
||
| 90 | * Hidden object |
||
| 91 | * |
||
| 92 | * @var boolean |
||
| 93 | */ |
||
| 94 | protected $hidden = false; |
||
| 95 | |||
| 96 | /******************************************************************************* |
||
| 97 | * PUBLIC METHODS |
||
| 98 | *******************************************************************************/ |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Object URL constructor |
||
| 102 | * |
||
| 103 | * @param null|string $locator Object locator |
||
| 104 | * @param null|boolean|int $datePrecision Date precision [NULL = local default, TRUE = any precision (remote object |
||
| 105 | * URLs)] |
||
| 106 | * @param string $leader Leading base locator |
||
| 107 | * @throws InvalidArgumentException If the object URL locator is invalid |
||
| 108 | */ |
||
| 109 | 78 | public function __construct($locator = null, $datePrecision = null, &$leader = '') |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Build the regular expression for matching a local object locator |
||
| 168 | * |
||
| 169 | * @param null|boolean|int $datePrecision Date precision [NULL = local default, TRUE = any precision (remote object |
||
| 170 | * URLs)] |
||
| 171 | * @return string Regular expression for matching a local object locator |
||
| 172 | * @throws InvalidArgumentException If the date precision is invalid |
||
| 173 | */ |
||
| 174 | 72 | protected function buildLocatorRegex($datePrecision) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Serialize the object locator |
||
| 213 | * |
||
| 214 | * @return string Object locator |
||
| 215 | */ |
||
| 216 | 48 | public function __toString() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Serialize as relative URL |
||
| 223 | * |
||
| 224 | * @param bool $canonical Canonical URL |
||
| 225 | * @return string Relative URL |
||
| 226 | */ |
||
| 227 | 49 | public function toUrl($canonical = false) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Return the object's creation date |
||
| 254 | * |
||
| 255 | * @return \DateTimeInterface Object creation date |
||
| 256 | */ |
||
| 257 | 51 | public function getCreationDate() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Set the object's creation date |
||
| 264 | * |
||
| 265 | * @param \DateTimeInterface $creationDate |
||
| 266 | * @return LocatorInterface|Locator New object locator |
||
| 267 | */ |
||
| 268 | 8 | public function setCreationDate(\DateTimeInterface $creationDate) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Return the object type |
||
| 277 | * |
||
| 278 | * @return Type Object type |
||
| 279 | */ |
||
| 280 | 55 | public function getObjectType() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Set the object type |
||
| 287 | * |
||
| 288 | * @param Type $type Object type |
||
| 289 | * @return LocatorInterface|Locator New object locator |
||
| 290 | */ |
||
| 291 | 8 | public function setObjectType(Type $type) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Return the object ID |
||
| 300 | * |
||
| 301 | * @return Id Object ID |
||
| 302 | */ |
||
| 303 | 54 | public function getId() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Set the object ID |
||
| 310 | * |
||
| 311 | * @param Id $uid Object ID |
||
| 312 | * @return LocatorInterface|Locator New object locator |
||
| 313 | */ |
||
| 314 | 8 | public function setId(Id $uid) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Return the object revision |
||
| 323 | * |
||
| 324 | * @return Revision Object revision |
||
| 325 | */ |
||
| 326 | 53 | public function getRevision() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Set the object revision |
||
| 333 | * |
||
| 334 | * @param Revision $revision Object revision |
||
| 335 | * @return LocatorInterface|Locator New object locator |
||
| 336 | */ |
||
| 337 | 50 | public function setRevision(Revision $revision) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Return the object hidden state |
||
| 346 | * |
||
| 347 | * @return boolean Object hidden state |
||
| 348 | */ |
||
| 349 | 3 | public function isHidden() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Set the object hidden state |
||
| 356 | * |
||
| 357 | * @param boolean $hidden Object hidden state |
||
| 358 | * @return LocatorInterface|Locator New object locator |
||
| 359 | */ |
||
| 360 | 48 | public function setHidden($hidden) |
|
| 366 | } |
||
| 367 |