Complex classes like FieldHelper 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 FieldHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | final class FieldHelper |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | private $field; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Field constructor. |
||
| 24 | */ |
||
| 25 | public function __construct(string $field) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return Expression |
||
| 32 | * @throws \InvalidArgumentException |
||
| 33 | */ |
||
| 34 | public function isNull(): Expression |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return Expression |
||
| 41 | * @throws \InvalidArgumentException |
||
| 42 | */ |
||
| 43 | public function isNotNull(): Expression |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @return Expression |
||
| 50 | * @throws \InvalidArgumentException |
||
| 51 | */ |
||
| 52 | public function isTrue(): Expression |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return Expression |
||
| 59 | * @throws \InvalidArgumentException |
||
| 60 | */ |
||
| 61 | public function isFalse(): Expression |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param array $values |
||
| 68 | * @param null|string $placeholder |
||
| 69 | * @param string $glue |
||
| 70 | * @return Expression |
||
| 71 | * @throws \InvalidArgumentException |
||
| 72 | */ |
||
| 73 | public function in(array $values, ?string $placeholder = '?', string $glue = ', '): Expression |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param array $values |
||
| 109 | * @param string $placeholder |
||
| 110 | * @param string $glue |
||
| 111 | * @return Expression |
||
| 112 | * @throws \InvalidArgumentException |
||
| 113 | */ |
||
| 114 | public function notIn(array $values, ?string $placeholder = '?', string $glue = ', '): Expression |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param $value |
||
| 150 | * @param string $placeholder |
||
| 151 | * @return Expression |
||
| 152 | * @throws \InvalidArgumentException |
||
| 153 | */ |
||
| 154 | public function equals($value, ?string $placeholder = '?'): Expression |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param $value |
||
| 175 | * @param string $placeholder |
||
| 176 | * @return Expression |
||
| 177 | * @throws \InvalidArgumentException |
||
| 178 | */ |
||
| 179 | public function notEquals($value, ?string $placeholder = '?'): Expression |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param $value |
||
| 200 | * @param string $placeholder |
||
| 201 | * @return Expression |
||
| 202 | * @throws \InvalidArgumentException |
||
| 203 | */ |
||
| 204 | public function lt($value, ?string $placeholder = '?'): Expression |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @param $value |
||
| 225 | * @param string $placeholder |
||
| 226 | * @return Expression |
||
| 227 | * @throws \InvalidArgumentException |
||
| 228 | */ |
||
| 229 | public function lte($value, ?string $placeholder = '?'): Expression |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param $value |
||
| 250 | * @param string $placeholder |
||
| 251 | * @return Expression |
||
| 252 | * @throws \InvalidArgumentException |
||
| 253 | */ |
||
| 254 | public function gt($value, ?string $placeholder = '?'): Expression |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param $value |
||
| 275 | * @param string $placeholder |
||
| 276 | * @return Expression |
||
| 277 | * @throws \InvalidArgumentException |
||
| 278 | */ |
||
| 279 | public function gte($value, ?string $placeholder = '?'): Expression |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param $start |
||
| 300 | * @param $end |
||
| 301 | * @param string $placeholder |
||
| 302 | * @return Expression |
||
| 303 | * @throws \InvalidArgumentException |
||
| 304 | */ |
||
| 305 | public function between($start, $end, ?string $placeholder = '?'): Expression |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param $start |
||
| 334 | * @param $end |
||
| 335 | * @param string $placeholder |
||
| 336 | * @return Expression |
||
| 337 | * @throws \InvalidArgumentException |
||
| 338 | */ |
||
| 339 | public function notBetween($start, $end, ?string $placeholder = '?'): Expression |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $value |
||
| 368 | * @param string $placeholder |
||
| 369 | * @param string $surroundWith |
||
| 370 | * @return Expression |
||
| 371 | * @throws \InvalidArgumentException |
||
| 372 | */ |
||
| 373 | public function like(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param string $value |
||
| 395 | * @param string $placeholder |
||
| 396 | * @param string $surroundWith |
||
| 397 | * @return Expression |
||
| 398 | * @throws \InvalidArgumentException |
||
| 399 | */ |
||
| 400 | public function notLike(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param string $value |
||
| 422 | * @param string $placeholder |
||
| 423 | * @param string $surroundWith |
||
| 424 | * @return Expression |
||
| 425 | * @throws \InvalidArgumentException |
||
| 426 | */ |
||
| 427 | public function startsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $value |
||
| 449 | * @param string $placeholder |
||
| 450 | * @param string $surroundWith |
||
| 451 | * @return Expression |
||
| 452 | * @throws \InvalidArgumentException |
||
| 453 | */ |
||
| 454 | public function notStartsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param string $value |
||
| 476 | * @param string $placeholder |
||
| 477 | * @param string $surroundWith |
||
| 478 | * @return Expression |
||
| 479 | * @throws \InvalidArgumentException |
||
| 480 | */ |
||
| 481 | public function endsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param string $value |
||
| 503 | * @param string $placeholder |
||
| 504 | * @param string $surroundWith |
||
| 505 | * @return Expression |
||
| 506 | * @throws \InvalidArgumentException |
||
| 507 | */ |
||
| 508 | public function notEndsWith(string $value, ?string $placeholder = '?', string $surroundWith = '%'): Expression |
||
| 527 | } |
||
| 528 |