Complex classes like DateHandler 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 DateHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class DateHandler implements SubscribingHandlerInterface |
||
| 30 | { |
||
| 31 | private $defaultFormat; |
||
| 32 | private $defaultTimezone; |
||
| 33 | private $xmlCData; |
||
| 34 | |||
| 35 | 383 | public static function getSubscribingMethods() |
|
| 36 | { |
||
| 37 | 383 | $methods = array(); |
|
| 38 | 383 | $deserializationTypes = array('DateTime', 'DateTimeImmutable', 'DateInterval'); |
|
| 39 | 383 | $serialisationTypes = array('DateTime', 'DateTimeImmutable', 'DateInterval'); |
|
| 40 | |||
| 41 | 383 | foreach (array('json', 'xml', 'yml') as $format) { |
|
| 42 | |||
| 43 | 383 | foreach ($deserializationTypes as $type) { |
|
| 44 | 383 | $methods[] = [ |
|
| 45 | 383 | 'type' => $type, |
|
| 46 | 383 | 'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
|
| 47 | 383 | 'format' => $format, |
|
| 48 | ]; |
||
| 49 | 383 | } |
|
| 50 | |||
| 51 | 383 | foreach ($serialisationTypes as $type) { |
|
| 52 | 383 | $methods[] = array( |
|
| 53 | 383 | 'type' => $type, |
|
| 54 | 383 | 'format' => $format, |
|
| 55 | 383 | 'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
|
| 56 | 383 | 'method' => 'serialize' . $type, |
|
| 57 | ); |
||
| 58 | 383 | } |
|
| 59 | 383 | } |
|
| 60 | |||
| 61 | 383 | return $methods; |
|
| 62 | } |
||
| 63 | |||
| 64 | 389 | public function __construct($defaultFormat = \DateTime::ISO8601, $defaultTimezone = 'UTC', $xmlCData = true) |
|
| 70 | |||
| 71 | 30 | private function serializeDateTimeInterface( |
|
| 89 | |||
| 90 | 24 | public function serializeDateTime(VisitorInterface $visitor, \DateTime $date, array $type, Context $context) |
|
| 94 | |||
| 95 | 6 | public function serializeDateTimeImmutable( |
|
| 104 | |||
| 105 | 3 | public function serializeDateInterval(VisitorInterface $visitor, \DateInterval $date, array $type, Context $context) |
|
| 115 | |||
| 116 | 7 | private function isDataXmlNull($data) |
|
| 121 | |||
| 122 | 5 | public function deserializeDateTimeFromXml(XmlDeserializationVisitor $visitor, $data, array $type) |
|
|
|
|||
| 123 | { |
||
| 124 | 5 | if ($this->isDataXmlNull($data)) { |
|
| 125 | return null; |
||
| 126 | } |
||
| 127 | |||
| 128 | 5 | return $this->parseDateTime($data, $type); |
|
| 129 | } |
||
| 130 | |||
| 131 | 1 | public function deserializeDateTimeImmutableFromXml(XmlDeserializationVisitor $visitor, $data, array $type) |
|
| 139 | |||
| 140 | 1 | public function deserializeDateIntervalFromXml(XmlDeserializationVisitor $visitor, $data, array $type) |
|
| 148 | |||
| 149 | 9 | public function deserializeDateTimeFromJson(JsonDeserializationVisitor $visitor, $data, array $type) |
|
| 157 | |||
| 158 | 2 | public function deserializeDateTimeImmutableFromJson(JsonDeserializationVisitor $visitor, $data, array $type) |
|
| 166 | |||
| 167 | 1 | public function deserializeDateIntervalFromJson(JsonDeserializationVisitor $visitor, $data, array $type) |
|
| 175 | |||
| 176 | 16 | private function parseDateTime($data, array $type, $immutable = false) |
|
| 177 | { |
||
| 193 | |||
| 194 | 2 | private function parseDateInterval($data) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @param array $type |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | 16 | private function getDeserializationFormat(array $type) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @return string |
||
| 223 | * @param array $type |
||
| 224 | */ |
||
| 225 | 30 | private function getFormat(array $type) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param \DateInterval $dateInterval |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | 4 | public function format(\DateInterval $dateInterval) |
|
| 272 | } |
||
| 273 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.