Total Complexity | 47 |
Total Lines | 172 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like RangeParser 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 RangeParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | final class RangeParser |
||
13 | { |
||
14 | private const RANGES = [ |
||
15 | Schema::TYPE_INT_RANGE, |
||
16 | Schema::TYPE_BIGINT_RANGE, |
||
17 | Schema::TYPE_NUM_RANGE, |
||
18 | Schema::TYPE_TS_RANGE, |
||
19 | Schema::TYPE_TS_TZ_RANGE, |
||
20 | Schema::TYPE_DATE_RANGE, |
||
21 | ]; |
||
22 | |||
23 | private ?string $type; |
||
24 | |||
25 | public function __construct(?string $type = null) |
||
26 | { |
||
27 | $this->type = $type; |
||
28 | } |
||
29 | |||
30 | public function withType(?string $type): self |
||
31 | { |
||
32 | $new = clone $this; |
||
33 | $new->type = $type; |
||
34 | |||
35 | return $new; |
||
36 | } |
||
37 | |||
38 | public function asInt(): self |
||
39 | { |
||
40 | return $this->withType(Schema::TYPE_INT_RANGE); |
||
41 | } |
||
42 | |||
43 | public function asBigInt(): self |
||
44 | { |
||
45 | return $this->withType(Schema::TYPE_BIGINT_RANGE); |
||
46 | } |
||
47 | |||
48 | public function asNumeric(): self |
||
49 | { |
||
50 | return $this->withType(Schema::TYPE_NUM_RANGE); |
||
51 | } |
||
52 | |||
53 | public function asDate(): self |
||
54 | { |
||
55 | return $this->withType(Schema::TYPE_DATE_RANGE); |
||
56 | } |
||
57 | |||
58 | public function asTimestamp(): self |
||
59 | { |
||
60 | return $this->withType(Schema::TYPE_TS_RANGE); |
||
61 | } |
||
62 | |||
63 | public function asTimestampTz(): self |
||
64 | { |
||
65 | return $this->withType(Schema::TYPE_TS_TZ_RANGE); |
||
66 | } |
||
67 | |||
68 | public function asCustom(): self |
||
69 | { |
||
70 | return $this->withType(null); |
||
71 | } |
||
72 | |||
73 | public function parse(?string $value): ?array |
||
74 | { |
||
75 | if ($value === null || $value === 'empty') { |
||
76 | return null; |
||
77 | } |
||
78 | |||
79 | if (!preg_match('/^(?P<open>\[|\()(?P<lower>[^,]*),(?P<upper>[^\)\]]*)(?P<close>\)|\])$/', $value, $matches)) { |
||
80 | throw new InvalidArgumentException('Unsupported range format'); |
||
81 | } |
||
82 | |||
83 | $lower = $matches['lower'] ? trim($matches['lower'], '"') : null; |
||
84 | $upper = $matches['upper'] ? trim($matches['upper'], '"') : null; |
||
85 | $includeLower = $matches['open'] === '['; |
||
86 | $includeUpper = $matches['close'] === ']'; |
||
87 | |||
88 | if ($lower === null && $upper === null) { |
||
89 | return [null, null]; |
||
90 | } |
||
91 | |||
92 | return match($this->type) { |
||
93 | Schema::TYPE_INT_RANGE => self::parseIntRange($lower, $upper, $includeLower, $includeUpper), |
||
94 | Schema::TYPE_BIGINT_RANGE => self::parseBigIntRange($lower, $upper, $includeLower, $includeUpper), |
||
95 | Schema::TYPE_NUM_RANGE => self::parseNumRange($lower, $upper), |
||
96 | Schema::TYPE_DATE_RANGE => self::parseDateRange($lower, $upper, $includeLower, $includeUpper), |
||
97 | Schema::TYPE_TS_RANGE => self::parseTsRange($lower, $upper), |
||
98 | Schema::TYPE_TS_TZ_RANGE => self::parseTsTzRange($lower, $upper), |
||
99 | default => [$lower, $upper] |
||
100 | }; |
||
101 | } |
||
102 | |||
103 | private static function parseIntRange(?string $lower, ?string $upper, bool $includeLower, bool $includeUpper): array |
||
104 | { |
||
105 | $min = $lower === null ? null : (int) $lower; |
||
106 | $max = $upper === null ? null : (int) $upper; |
||
107 | |||
108 | if ($min !== null && $includeLower === false) { |
||
109 | $min += 1; |
||
110 | } |
||
111 | |||
112 | if ($max !== null && $includeUpper === false) { |
||
113 | $max -= 1; |
||
114 | } |
||
115 | |||
116 | return [$min, $max]; |
||
117 | } |
||
118 | |||
119 | private static function parseBigIntRange(?string $lower, ?string $upper, bool $includeLower, bool $includeUpper): array |
||
138 | } |
||
139 | |||
140 | private static function parseNumRange(?string $lower, ?string $upper): array |
||
141 | { |
||
142 | $min = $lower === null ? null : (float) $lower; |
||
143 | $max = $upper === null ? null : (float) $upper; |
||
144 | |||
145 | return [$min, $max]; |
||
146 | } |
||
147 | |||
148 | private static function parseDateRange(?string $lower, ?string $upper, bool $includeLower, bool $includeUpper): array |
||
163 | } |
||
164 | |||
165 | private static function parseTsRange(?string $lower, ?string $upper): array |
||
166 | { |
||
167 | $min = $lower ? DateTime::createFromFormat('Y-m-d H:i:s', $lower) : null; |
||
168 | $max = $upper ? DateTime::createFromFormat('Y-m-d H:i:s', $upper) : null; |
||
169 | |||
170 | return [$min, $max]; |
||
171 | } |
||
172 | |||
173 | private static function parseTsTzRange(?string $lower, ?string $upper): array |
||
174 | { |
||
175 | $min = $lower ? DateTime::createFromFormat('Y-m-d H:i:sP', $lower) : null; |
||
176 | $max = $upper ? DateTime::createFromFormat('Y-m-d H:i:sP', $upper) : null; |
||
177 | |||
178 | return [$min, $max]; |
||
179 | } |
||
180 | |||
181 | public static function isAllowedType(string $type): bool |
||
184 | } |
||
185 | } |
||
186 |