Complex classes like ValidatorTrait 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 ValidatorTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | trait ValidatorTrait |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @throws \Kerox\Messenger\Exception\InvalidColorException |
||
| 30 | */ |
||
| 31 | 2 | protected function isValidColor(string $value): void |
|
| 37 | |||
| 38 | /** |
||
| 39 | * @throws \Kerox\Messenger\Exception\InvalidStringException |
||
| 40 | */ |
||
| 41 | 39 | protected function isValidString(string $value, int $length = 20): void |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @throws \Kerox\Messenger\Exception\InvalidUrlException |
||
| 50 | */ |
||
| 51 | 38 | protected function isValidUrl(string $value): void |
|
| 52 | { |
||
| 53 | 38 | if (!preg_match( |
|
| 54 | 38 | '/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)$/', |
|
| 55 | $value |
||
| 56 | )) { |
||
| 57 | 1 | throw new InvalidUrlException(sprintf('"%s" is not a valid url.', $value)); |
|
| 58 | } |
||
| 59 | 37 | } |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @throws \Kerox\Messenger\Exception\InvalidLocaleException |
||
| 63 | */ |
||
| 64 | 8 | protected function isValidLocale(string $value): void |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @throws \Kerox\Messenger\Exception\InvalidCountryException |
||
| 73 | */ |
||
| 74 | 2 | protected function isValidCountry(string $value): void |
|
| 80 | |||
| 81 | /** |
||
| 82 | * @throws \Kerox\Messenger\Exception\InvalidDateTimeException |
||
| 83 | */ |
||
| 84 | 11 | protected function isValidDateTime(string $value): void |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @param int $minSize |
||
| 93 | * |
||
| 94 | * @throws \Kerox\Messenger\Exception\InvalidArrayException |
||
| 95 | */ |
||
| 96 | 32 | protected function isValidArray(array $array, int $maxSize, ?int $minSize = null): void |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @throws \Kerox\Messenger\Exception\InvalidCurrencyException |
||
| 109 | */ |
||
| 110 | 8 | protected function isValidCurrency(string $value): void |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @throws \Kerox\Messenger\Exception\InvalidExtensionException |
||
| 122 | */ |
||
| 123 | 3 | protected function isValidExtension(string $filename, array $allowedExtension): void |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @param \Kerox\Messenger\Model\Common\Button\AbstractButton[] $buttons |
||
| 133 | * |
||
| 134 | * @throws \Kerox\Messenger\Exception\InvalidClassException |
||
| 135 | */ |
||
| 136 | 8 | protected function isValidButtons(array $buttons, array $allowedButtonsType): void |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param mixed $message |
||
| 152 | * |
||
| 153 | * @throws \Exception |
||
| 154 | */ |
||
| 155 | 7 | protected function isValidMessage($message): Message |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @throws \Kerox\Messenger\Exception\InvalidKeyException |
||
| 170 | */ |
||
| 171 | 2 | protected function isValidSenderAction(string $action): void |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @throws \Kerox\Messenger\Exception\InvalidTypeException |
||
| 181 | */ |
||
| 182 | 5 | protected function isValidNotificationType(string $notificationType): void |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param mixed $message |
||
| 192 | * |
||
| 193 | * @throws \Kerox\Messenger\Exception\InvalidClassException |
||
| 194 | * @throws \Kerox\Messenger\Exception\InvalidKeyException |
||
| 195 | */ |
||
| 196 | 3 | protected function isValidTag(string $tag, $message = null): void |
|
| 197 | { |
||
| 198 | 3 | $allowedTag = $this->getAllowedTag(); |
|
| 199 | 3 | $deprecatedTag = $this->getDeprecatedTags(); |
|
| 200 | 3 | if (!\in_array($tag, $allowedTag, true)) { |
|
| 201 | 1 | throw new InvalidKeyException(sprintf('"tag" must be either "%s".', implode(', ', $allowedTag))); |
|
| 202 | } |
||
| 203 | |||
| 204 | 2 | if (\in_array($tag, $deprecatedTag, true)) { |
|
| 205 | 1 | $message = sprintf('Tag "%s" is deprecated, use one of "%s" instead.', |
|
| 206 | $tag, |
||
| 207 | 1 | implode(',', [ |
|
| 208 | 1 | SendInterface::TAG_CONFIRMED_EVENT_UPDATE, |
|
| 209 | 1 | SendInterface::TAG_POST_PURCHASE_UPDATE, |
|
| 210 | 1 | SendInterface::TAG_ACCOUNT_UPDATE, |
|
| 211 | ]) |
||
| 212 | ); |
||
| 213 | 1 | @trigger_error($message, E_USER_DEPRECATED); |
|
|
|
|||
| 214 | } |
||
| 215 | |||
| 216 | 2 | if ($tag === SendInterface::TAG_ISSUE_RESOLUTION && $message !== null && !$message instanceof GenericTemplate) { |
|
| 217 | 1 | throw new InvalidClassException(sprintf('"message" must be an instance of "%s" if tag is set to "%s".', GenericTemplate::class, SendInterface::TAG_ISSUE_RESOLUTION)); |
|
| 218 | } |
||
| 219 | 1 | } |
|
| 220 | |||
| 221 | 2 | protected function getAllowedSenderAction(): array |
|
| 229 | |||
| 230 | 5 | protected function getAllowedNotificationType(): array |
|
| 238 | |||
| 239 | 3 | protected function getAllowedTag(): array |
|
| 264 | |||
| 265 | 3 | protected function getDeprecatedTags(): array |
|
| 286 | |||
| 287 | 8 | protected function getAllowedCurrency(): array |
|
| 341 | } |
||
| 342 |
If you suppress an error, we recommend checking for the error condition explicitly: