| Total Complexity | 42 |
| Total Lines | 202 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ImageFilterData 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 ImageFilterData, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class ImageFilterData |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var ?CarbonImmutable |
||
| 21 | * |
||
| 22 | * @Assert\Range( |
||
| 23 | * min = "Jan 1 1900", |
||
| 24 | * max = "now + 10 years" |
||
| 25 | * ) |
||
| 26 | */ |
||
| 27 | private $startDate; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var ?CarbonImmutable |
||
| 31 | * |
||
| 32 | * @Assert\Range( |
||
| 33 | * min = "Jan 1 1900", |
||
| 34 | * max = "now + 10 years" |
||
| 35 | * ) |
||
| 36 | */ |
||
| 37 | private $endDate; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var ?int |
||
| 41 | * |
||
| 42 | * @Assert\Range( |
||
| 43 | * min = 0, |
||
| 44 | * max = 5 |
||
| 45 | * ) |
||
| 46 | * |
||
| 47 | */ |
||
| 48 | private $rating; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | * |
||
| 53 | * @Assert\Regex( |
||
| 54 | * pattern = "/eq|lte|gte/" |
||
| 55 | * ) |
||
| 56 | * |
||
| 57 | */ |
||
| 58 | private $ratingComparison; |
||
| 59 | |||
| 60 | /** @var ?string */ |
||
| 61 | private $location; |
||
| 62 | |||
| 63 | public function __construct( |
||
| 64 | DateTimeInterface $startDate = null, |
||
| 65 | DateTimeInterface $endDate = null |
||
| 66 | ) { |
||
| 67 | $this->startDate = new CarbonImmutable($startDate); |
||
| 68 | $this->endDate = new CarbonImmutable($endDate); |
||
| 69 | $this->ratingComparison = 'eq'; |
||
| 70 | } |
||
| 71 | |||
| 72 | public function setStartDate(?DateTimeInterface $startDate): void |
||
| 73 | { |
||
| 74 | if ($startDate === null) { |
||
| 75 | $this->startDate = null; |
||
| 76 | } else { |
||
| 77 | $this->startDate = new CarbonImmutable($startDate); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | public function overrideStartDateFromUrlParam(?string $startDate): void |
||
| 81 | { |
||
| 82 | if (!empty($startDate)) { |
||
| 83 | $this->startDate = $this->dateFromUrlParam($startDate); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | public function getStartDate(): ?DateTimeInterface |
||
| 87 | { |
||
| 88 | if ($this->startDate === null) { |
||
| 89 | return null; |
||
| 90 | } |
||
| 91 | return $this->startDate->toDateTimeImmutable(); |
||
| 92 | } |
||
| 93 | public function hasStartDate(): bool |
||
| 94 | { |
||
| 95 | return $this->startDate !== null; |
||
| 96 | } |
||
| 97 | public function setEndDate(?DateTimeInterface $endDate): void |
||
| 98 | { |
||
| 99 | if ($endDate === null) { |
||
| 100 | $this->endDate = null; |
||
| 101 | } |
||
| 102 | $this->endDate = new CarbonImmutable($endDate); |
||
| 103 | } |
||
| 104 | public function overrideEndDateFromUrlParam(?string $endDate): void |
||
| 105 | { |
||
| 106 | if (!empty($endDate)) { |
||
| 107 | $this->endDate = $this->dateFromUrlParam($endDate); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | public function getEndDate(): ?DateTimeImmutable |
||
| 111 | { |
||
| 112 | if ($this->endDate === null) { |
||
| 113 | return null; |
||
| 114 | } |
||
| 115 | return $this->endDate->toDateTimeImmutable(); |
||
| 116 | } |
||
| 117 | public function hasEndDate(): bool |
||
| 120 | } |
||
| 121 | public function setRating(?int $rating): void |
||
| 122 | { |
||
| 123 | $this->rating = $rating; |
||
| 124 | } |
||
| 125 | /** |
||
| 126 | * @throws InvalidArgumentException |
||
| 127 | */ |
||
| 128 | public function overrideRatingFromUrlParam(?string $rating): void |
||
| 129 | { |
||
| 130 | if (!empty($rating)) { |
||
| 131 | if (intval($rating) >= 0 && intval($rating) <= 5) { |
||
| 132 | $this->rating = intval($rating); |
||
| 133 | } else { |
||
| 134 | throw new InvalidArgumentException('Invalid rating override in URL parameter'); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | public function getRating(): ?int |
||
| 139 | { |
||
| 140 | return $this->rating; |
||
| 141 | } |
||
| 142 | public function hasRating(): bool |
||
| 143 | { |
||
| 144 | return $this->rating !== null; |
||
| 145 | } |
||
| 146 | public function setRatingComparison(string $ratingComparison): void |
||
| 147 | { |
||
| 148 | $this->ratingComparison = $ratingComparison; |
||
| 149 | } |
||
| 150 | public function getRatingComparison(): string |
||
| 151 | { |
||
| 152 | return $this->ratingComparison; |
||
| 153 | } |
||
| 154 | public function setLocation(?string $location): void |
||
| 155 | { |
||
| 156 | $this->location = $location === "" ? null : $location; |
||
| 157 | } |
||
| 158 | public function overrideLocationFromUrlParam(?string $location): void |
||
| 162 | } |
||
| 163 | } |
||
| 164 | public function getLocation(): ?string |
||
| 167 | } |
||
| 168 | public function hasLocation(): bool |
||
| 169 | { |
||
| 170 | return $this->location !== null; |
||
| 171 | } |
||
| 172 | private function getDateForUrlParam(?CarbonImmutable $d): string |
||
| 173 | { |
||
| 174 | if ($d === null) { |
||
| 175 | return ''; |
||
| 176 | } |
||
| 177 | return $d->isoFormat('YYYY-MM-DD'); |
||
| 178 | } |
||
| 179 | /** |
||
| 180 | * Gets all filter parameters in a format suitable for passing to Symfony's |
||
| 181 | * route generator. |
||
| 182 | * |
||
| 183 | * @return array<string> |
||
| 184 | */ |
||
| 185 | public function getAsUrlParams(): array |
||
| 186 | { |
||
| 187 | return [ |
||
| 188 | 'startDate' => $this->getDateForUrlParam($this->startDate), |
||
| 189 | 'endDate' => $this->getDateForUrlParam($this->endDate), |
||
| 190 | 'rating' => $this->rating === null ? '' : (string) $this->rating, |
||
| 191 | 'ratingComparison' => $this->ratingComparison === null ? '' : $this->ratingComparison, |
||
| 192 | 'location' => $this->location === null ? '' : $this->location |
||
| 193 | ]; |
||
| 194 | } |
||
| 195 | /** |
||
| 196 | * @throws InvalidArgumentException |
||
| 197 | */ |
||
| 198 | private function dateFromUrlParam(?string $param): ?CarbonImmutable |
||
| 219 | } |
||
| 220 | } |
||
| 221 |