| Total Complexity | 65 |
| Total Lines | 608 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MailtoQueryMutable 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 MailtoQueryMutable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class MailtoQueryMutable implements MutableBagInterface |
||
| 18 | { |
||
| 19 | use Accessor; |
||
| 20 | use Mutator; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * To recipients, can be more than one as |
||
| 24 | * long they are separated by a comma |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | private $to = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * CarbonCopy recipients, can be more than one |
||
| 32 | * as long as they are separated by a comma |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private $cc = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * BlindCarbonCopy recipients, can be more than |
||
| 40 | * one as long as they are separated by a comma |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $bcc = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $subject = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $body = ''; |
||
| 55 | |||
| 56 | ///////////////////////// |
||
| 57 | /// GETTER FUNCTIONS /// |
||
| 58 | //////////////////////// |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return array |
||
| 62 | */ |
||
| 63 | public function getTo(): array |
||
| 64 | { |
||
| 65 | return $this->to; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | public function getCc(): array |
||
| 72 | { |
||
| 73 | return $this->cc; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | public function getBcc(): array |
||
| 80 | { |
||
| 81 | return $this->bcc; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function getSubject(): string |
||
| 88 | { |
||
| 89 | return $this->subject; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param int $key |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | * @throws ComponentNotFoundException |
||
| 97 | */ |
||
| 98 | public function getInTo(int $key): string |
||
| 99 | { |
||
| 100 | return $this->getKeyIn($this->to, $key); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param int $key |
||
| 105 | * |
||
| 106 | * @return string |
||
| 107 | * @throws ComponentNotFoundException |
||
| 108 | */ |
||
| 109 | public function getInCc(int $key): string |
||
| 110 | { |
||
| 111 | return $this->getKeyIn($this->cc, $key); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param int $key |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | * @throws ComponentNotFoundException |
||
| 119 | */ |
||
| 120 | public function getInBcc(int $key): string |
||
| 121 | { |
||
| 122 | return $this->getKeyIn($this->bcc, $key); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function getBody(): string |
||
| 129 | { |
||
| 130 | return $this->body; |
||
| 131 | } |
||
| 132 | |||
| 133 | ///////////////////////// |
||
| 134 | /// SETTER FUNCTIONS /// |
||
| 135 | //////////////////////// |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param array $to |
||
| 139 | * |
||
| 140 | * @return MailtoQueryMutable |
||
| 141 | * @throws InvalidComponentsException |
||
| 142 | */ |
||
| 143 | public function setTo(array $to): MailtoQueryMutable |
||
| 144 | { |
||
| 145 | if (count($to) !== count($to, COUNT_RECURSIVE)) { |
||
| 146 | throw new InvalidComponentsException(sprintf('Unable to accept multidimensional arrays for $to component in %s', |
||
| 147 | __CLASS__)); |
||
| 148 | } |
||
| 149 | |||
| 150 | $this->to = array_values($to); |
||
| 151 | |||
| 152 | return $this; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param string $body |
||
| 157 | * |
||
| 158 | * @return MailtoQueryMutable |
||
| 159 | */ |
||
| 160 | public function setBody(string $body): MailtoQueryMutable |
||
| 161 | { |
||
| 162 | $this->body = $body; |
||
| 163 | |||
| 164 | return $this; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param array $cc |
||
| 169 | * |
||
| 170 | * @return MailtoQueryMutable |
||
| 171 | * @throws InvalidComponentsException |
||
| 172 | */ |
||
| 173 | public function setCc(array $cc): MailtoQueryMutable |
||
| 174 | { |
||
| 175 | if (count($cc) !== count($cc, COUNT_RECURSIVE)) { |
||
| 176 | throw new InvalidComponentsException(sprintf('Unable to accept multidimensional arrays for $cc component in %s', |
||
| 177 | __CLASS__)); |
||
| 178 | } |
||
| 179 | |||
| 180 | $this->cc = array_values($cc); |
||
| 181 | |||
| 182 | return $this; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param array $bcc |
||
| 187 | * |
||
| 188 | * @return MailtoQueryMutable |
||
| 189 | * @throws InvalidComponentsException |
||
| 190 | */ |
||
| 191 | public function setBcc(array $bcc): MailtoQueryMutable |
||
| 192 | { |
||
| 193 | if (count($bcc) !== count($bcc, COUNT_RECURSIVE)) { |
||
| 194 | throw new InvalidComponentsException(sprintf('Unable to accept multidimensional arrays for $bcc component in %s', |
||
| 195 | __CLASS__)); |
||
| 196 | } |
||
| 197 | |||
| 198 | $this->bcc = array_values($bcc); |
||
| 199 | |||
| 200 | return $this; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $subject |
||
| 205 | * |
||
| 206 | * @return MailtoQueryMutable |
||
| 207 | */ |
||
| 208 | public function setSubject(string $subject): MailtoQueryMutable |
||
| 209 | { |
||
| 210 | $this->subject = $subject; |
||
| 211 | |||
| 212 | return $this; |
||
| 213 | } |
||
| 214 | |||
| 215 | ////////////////////////// |
||
| 216 | /// MUTATOR FUNCTIONS /// |
||
| 217 | ///////////////////////// |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @return string|null |
||
| 221 | */ |
||
| 222 | public function firstInCc(): ?string |
||
| 223 | { |
||
| 224 | return $this->firstInPath($this->cc); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return string|null |
||
| 229 | */ |
||
| 230 | public function lastInCc(): ?string |
||
| 231 | { |
||
| 232 | return $this->lastInPath($this->cc); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @return string|null |
||
| 237 | */ |
||
| 238 | public function firstInTo(): ?string |
||
| 239 | { |
||
| 240 | return $this->firstInPath($this->to); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return string|null |
||
| 245 | */ |
||
| 246 | public function lastInTo(): ?string |
||
| 247 | { |
||
| 248 | return $this->lastInPath($this->to); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @return string|null |
||
| 253 | */ |
||
| 254 | public function firstInBcc(): ?string |
||
| 255 | { |
||
| 256 | return $this->firstInPath($this->bcc); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return string|null |
||
| 261 | */ |
||
| 262 | public function lastInBcc(): ?string |
||
| 263 | { |
||
| 264 | return $this->lastInPath($this->bcc); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param string $value |
||
| 269 | * |
||
| 270 | * @return MailtoQueryMutable |
||
| 271 | */ |
||
| 272 | public function putInTo(string $value): self |
||
| 273 | { |
||
| 274 | $this->mutatorAppend($this->to, $value); |
||
| 275 | |||
| 276 | return $this; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $value |
||
| 281 | * |
||
| 282 | * @return MailtoQueryMutable |
||
| 283 | */ |
||
| 284 | public function putInCc(string $value): self |
||
| 285 | { |
||
| 286 | $this->mutatorAppend($this->cc, $value); |
||
| 287 | |||
| 288 | return $this; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $value |
||
| 293 | * |
||
| 294 | * @return MailtoQueryMutable |
||
| 295 | */ |
||
| 296 | public function putInBcc(string $value): self |
||
| 297 | { |
||
| 298 | $this->mutatorAppend($this->cc, $value); |
||
| 299 | |||
| 300 | return $this; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param $keyOrValue |
||
| 305 | * |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function forgetFromTo($keyOrValue): self |
||
| 309 | { |
||
| 310 | $this->mutatorForgetKeyOrValue($this->to, $keyOrValue); |
||
| 311 | |||
| 312 | return $this; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param $keyOrValue |
||
| 317 | * |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | public function forgetFromCc($keyOrValue): self |
||
| 321 | { |
||
| 322 | $this->mutatorForgetKeyOrValue($this->cc, $keyOrValue); |
||
| 323 | |||
| 324 | return $this; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param $keyOrValue |
||
| 329 | * |
||
| 330 | * @return $this |
||
| 331 | */ |
||
| 332 | public function forgetFromBcc($keyOrValue): self |
||
| 333 | { |
||
| 334 | $this->mutatorForgetKeyOrValue($this->bcc, $keyOrValue); |
||
| 335 | |||
| 336 | return $this; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Sets to to an empty array |
||
| 341 | */ |
||
| 342 | public function forgetTo(): self |
||
| 343 | { |
||
| 344 | $this->to = []; |
||
| 345 | |||
| 346 | return $this; |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Sets cc to an empty array |
||
| 351 | */ |
||
| 352 | public function forgetCc(): self |
||
| 353 | { |
||
| 354 | $this->cc = []; |
||
| 355 | |||
| 356 | return $this; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Sets bcc to an empty array |
||
| 361 | */ |
||
| 362 | public function forgetBcc(): self |
||
| 363 | { |
||
| 364 | $this->bcc = []; |
||
| 365 | |||
| 366 | return $this; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Sets the subject to an empty string |
||
| 371 | * |
||
| 372 | * @return $this |
||
| 373 | */ |
||
| 374 | public function forgetSubject(): self |
||
| 375 | { |
||
| 376 | $this->subject = ''; |
||
| 377 | |||
| 378 | return $this; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @return MailtoQueryMutable |
||
| 383 | */ |
||
| 384 | public function forgetBody(): self |
||
| 385 | { |
||
| 386 | $this->body = ''; |
||
| 387 | |||
| 388 | return $this; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param string $value |
||
| 393 | * |
||
| 394 | * @return bool |
||
| 395 | */ |
||
| 396 | public function toHas(string $value): bool |
||
| 397 | { |
||
| 398 | return isset(array_flip($this->to)[$value]); |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param string $value |
||
| 403 | * |
||
| 404 | * @return bool |
||
| 405 | */ |
||
| 406 | public function ccHas(string $value): bool |
||
| 407 | { |
||
| 408 | return isset(array_flip($this->cc)[$value]); |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param string $value |
||
| 413 | * |
||
| 414 | * @return bool |
||
| 415 | */ |
||
| 416 | public function bccHas(string $value): bool |
||
| 417 | { |
||
| 418 | return isset(array_flip($this->bcc)[$value]); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @return MailtoQueryMutable |
||
| 423 | */ |
||
| 424 | public function forgetAll(): self |
||
| 425 | { |
||
| 426 | $this->to = []; |
||
| 427 | $this->cc = []; |
||
| 428 | $this->bcc = []; |
||
| 429 | |||
| 430 | return $this; |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param string ...$args |
||
| 435 | * |
||
| 436 | * @return array |
||
| 437 | * @throws ComponentNotFoundException |
||
| 438 | */ |
||
| 439 | public function only(string ...$args): array |
||
| 440 | { |
||
| 441 | $result = []; |
||
| 442 | |||
| 443 | foreach ($args as $key => $item) { |
||
| 444 | if (!$this->has($item)) { |
||
| 445 | throw new ComponentNotFoundException(sprintf('Component %s does not exist in %s', |
||
| 446 | $key, __CLASS__)); |
||
| 447 | } |
||
| 448 | $result[$item] = $this->$item; |
||
| 449 | } |
||
| 450 | |||
| 451 | return $result; |
||
| 452 | } |
||
| 453 | |||
| 454 | ///////////////////////////////// |
||
| 455 | /// INTERFACE IMPLEMENTATION /// |
||
| 456 | //////////////////////////////// |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @return array |
||
| 460 | */ |
||
| 461 | public function all(): array |
||
| 462 | { |
||
| 463 | return [ |
||
| 464 | 'to' => $this->to, |
||
| 465 | 'cc' => $this->cc, |
||
| 466 | 'bcc' => $this->bcc, |
||
| 467 | 'subject' => $this->subject, |
||
| 468 | 'body' => $this->body, |
||
| 469 | ]; |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param string $key |
||
| 474 | * |
||
| 475 | * @return bool |
||
| 476 | */ |
||
| 477 | public function has($key): bool |
||
| 478 | { |
||
| 479 | return property_exists($this, $key); |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param $key |
||
| 484 | * |
||
| 485 | * @return mixed |
||
| 486 | * @throws ComponentNotFoundException |
||
| 487 | */ |
||
| 488 | public function get($key) |
||
| 489 | { |
||
| 490 | if (!$this->has($key)) { |
||
| 491 | throw new ComponentNotFoundException(sprintf('Component %s does not exist in %s', |
||
| 492 | $key, __CLASS__)); |
||
| 493 | } |
||
| 494 | |||
| 495 | return $this->$key; |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * @param $key |
||
| 500 | * @param $value |
||
| 501 | * |
||
| 502 | * @return MutableBagInterface |
||
| 503 | * @throws ComponentNotFoundException |
||
| 504 | * @throws InvalidComponentsException |
||
| 505 | */ |
||
| 506 | public function set($key, $value): MutableBagInterface |
||
| 507 | { |
||
| 508 | if (!$this->has($key)) { |
||
| 509 | throw new ComponentNotFoundException(sprintf('Component %s does not exist in %s', |
||
| 510 | $key, __CLASS__)); |
||
| 511 | } |
||
| 512 | |||
| 513 | if (is_array($this->$key)) { |
||
| 514 | if (count($value) !== count($value, COUNT_RECURSIVE)) { |
||
| 515 | throw new InvalidComponentsException(sprintf('Unable to accept multidimensional arrays for %s component in %s', |
||
| 516 | $key, __CLASS__)); |
||
| 517 | } |
||
| 518 | |||
| 519 | $this->$key = array_values($value); |
||
| 520 | |||
| 521 | return $this; |
||
| 522 | } |
||
| 523 | |||
| 524 | $this->$key = $value; |
||
| 525 | |||
| 526 | return $this; |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * @return string |
||
| 531 | */ |
||
| 532 | public function encoded(): string |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * @inheritDoc |
||
| 539 | */ |
||
| 540 | public function raw(): string |
||
| 541 | { |
||
| 542 | return $this->buildQuery(false); |
||
| 543 | } |
||
| 544 | |||
| 545 | //////////////////////// |
||
| 546 | /// OTHER FUNCTIONS /// |
||
| 547 | /////////////////////// |
||
| 548 | |||
| 549 | /** |
||
| 550 | * @param bool $urlEncode |
||
| 551 | * |
||
| 552 | * @return string |
||
| 553 | */ |
||
| 554 | private function buildQuery(bool $urlEncode = false): string |
||
| 625 | } |
||
| 626 | } |
||
| 627 |