Complex classes like DateRange 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 DateRange, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class DateRange |
||
| 19 | { |
||
| 20 | /** @var DateTime */ |
||
| 21 | private $startDate; |
||
| 22 | |||
| 23 | /** @var DateTime */ |
||
| 24 | private $endDate; |
||
| 25 | |||
| 26 | /** @var FormatterInterface */ |
||
| 27 | private $formatter; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param DateTime|null $startDate |
||
| 31 | * @param DateTime|null $endDate |
||
| 32 | * @throws \InvalidArgumentException |
||
| 33 | */ |
||
| 34 | 18 | public function __construct(DateTime $startDate = null, DateTime $endDate = null) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * @return DateTime|null |
||
| 48 | */ |
||
| 49 | 26 | public function getStartDate() |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @return DateTime|null |
||
| 56 | */ |
||
| 57 | 22 | public function getEndDate() |
|
| 61 | |||
| 62 | /** |
||
| 63 | * @param DateTime $date |
||
| 64 | * @return bool |
||
| 65 | */ |
||
| 66 | 8 | public function containsDate(DateTime $date) : bool |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @param DateRange $dateRange |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | 10 | public function containsDateRange(DateRange $dateRange) : bool |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @param DateRange $dateRange |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | 6 | public function overlapsDateRange(DateRange $dateRange) : bool |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param DateTime $date |
||
| 103 | * @return bool |
||
| 104 | */ |
||
| 105 | 5 | public function isBefore(DateTime $date) : bool |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | 1 | public function isNow() : bool |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @param DateTime $date |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | 5 | public function isAfter(DateTime $date) : bool |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param DateRange $dateRange |
||
| 137 | * @return DateRange|null |
||
| 138 | */ |
||
| 139 | 9 | public function intersect(DateRange $dateRange) |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param string $nullPlaceholder |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | 10 | public function getShort(string $nullPlaceholder = 'Ongoing') : string |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param string $nullPlaceholder |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | 21 | public function getLong(string $nullPlaceholder = 'Ongoing') : string |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @return DateTime[] |
||
| 171 | * @throws InvalidArgumentException |
||
| 172 | */ |
||
| 173 | 4 | public function getAllDates() : array |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @return Time|null |
||
| 195 | */ |
||
| 196 | 4 | public function getDuration() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | 7 | public function __toString() : string |
|
| 212 | |||
| 213 | /** |
||
| 214 | * |
||
| 215 | */ |
||
| 216 | 1 | public function __clone() |
|
| 221 | } |
||
| 222 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.