Total Complexity | 72 |
Total Lines | 244 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like CodePointString 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 CodePointString, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class CodePointString extends AbstractUnicodeString |
||
26 | { |
||
27 | public function __construct(string $string = '') |
||
28 | { |
||
29 | if ('' !== $string && !preg_match('//u', $string)) { |
||
30 | throw new InvalidArgumentException('Invalid UTF-8 string.'); |
||
31 | } |
||
32 | |||
33 | $this->string = $string; |
||
34 | } |
||
35 | |||
36 | public function append(string ...$suffix): AbstractString |
||
46 | } |
||
47 | |||
48 | public function chunk(int $length = 1): array |
||
49 | { |
||
50 | if (1 > $length) { |
||
51 | throw new InvalidArgumentException('The chunk length must be greater than zero.'); |
||
52 | } |
||
53 | |||
54 | if ('' === $this->string) { |
||
55 | return []; |
||
56 | } |
||
57 | |||
58 | $rx = '/('; |
||
59 | while (65535 < $length) { |
||
60 | $rx .= '.{65535}'; |
||
61 | $length -= 65535; |
||
62 | } |
||
63 | $rx .= '.{'.$length.'})/us'; |
||
64 | |||
65 | $str = clone $this; |
||
66 | $chunks = []; |
||
67 | |||
68 | foreach (preg_split($rx, $this->string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY) as $chunk) { |
||
69 | $str->string = $chunk; |
||
70 | $chunks[] = clone $str; |
||
71 | } |
||
72 | |||
73 | return $chunks; |
||
74 | } |
||
75 | |||
76 | public function codePointsAt(int $offset): array |
||
81 | } |
||
82 | |||
83 | public function endsWith($suffix): bool |
||
84 | { |
||
85 | if ($suffix instanceof AbstractString) { |
||
86 | $suffix = $suffix->string; |
||
87 | } elseif (\is_array($suffix) || $suffix instanceof \Traversable) { |
||
88 | return parent::endsWith($suffix); |
||
89 | } else { |
||
90 | $suffix = (string) $suffix; |
||
91 | } |
||
92 | |||
93 | if ('' === $suffix || !preg_match('//u', $suffix)) { |
||
94 | return false; |
||
95 | } |
||
96 | |||
97 | if ($this->ignoreCase) { |
||
98 | return preg_match('{'.preg_quote($suffix).'$}iuD', $this->string); |
||
|
|||
99 | } |
||
100 | |||
101 | return \strlen($this->string) >= \strlen($suffix) && 0 === substr_compare($this->string, $suffix, -\strlen($suffix)); |
||
102 | } |
||
103 | |||
104 | public function equalsTo($string): bool |
||
119 | } |
||
120 | |||
121 | public function indexOf($needle, int $offset = 0): ?int |
||
122 | { |
||
123 | if ($needle instanceof AbstractString) { |
||
124 | $needle = $needle->string; |
||
125 | } elseif (\is_array($needle) || $needle instanceof \Traversable) { |
||
126 | return parent::indexOf($needle, $offset); |
||
127 | } else { |
||
128 | $needle = (string) $needle; |
||
129 | } |
||
130 | |||
131 | if ('' === $needle) { |
||
132 | return null; |
||
133 | } |
||
134 | |||
135 | $i = $this->ignoreCase ? mb_stripos($this->string, $needle, $offset, 'UTF-8') : mb_strpos($this->string, $needle, $offset, 'UTF-8'); |
||
136 | |||
137 | return false === $i ? null : $i; |
||
138 | } |
||
139 | |||
140 | public function indexOfLast($needle, int $offset = 0): ?int |
||
141 | { |
||
142 | if ($needle instanceof AbstractString) { |
||
143 | $needle = $needle->string; |
||
144 | } elseif (\is_array($needle) || $needle instanceof \Traversable) { |
||
145 | return parent::indexOfLast($needle, $offset); |
||
146 | } else { |
||
147 | $needle = (string) $needle; |
||
148 | } |
||
149 | |||
150 | if ('' === $needle) { |
||
151 | return null; |
||
152 | } |
||
153 | |||
154 | $i = $this->ignoreCase ? mb_strripos($this->string, $needle, $offset, 'UTF-8') : mb_strrpos($this->string, $needle, $offset, 'UTF-8'); |
||
155 | |||
156 | return false === $i ? null : $i; |
||
157 | } |
||
158 | |||
159 | public function length(): int |
||
160 | { |
||
161 | return mb_strlen($this->string, 'UTF-8'); |
||
162 | } |
||
163 | |||
164 | public function prepend(string ...$prefix): AbstractString |
||
165 | { |
||
166 | $str = clone $this; |
||
167 | $str->string = (1 >= \count($prefix) ? ($prefix[0] ?? '') : implode('', $prefix)).$this->string; |
||
168 | |||
169 | if (!preg_match('//u', $str->string)) { |
||
170 | throw new InvalidArgumentException('Invalid UTF-8 string.'); |
||
171 | } |
||
172 | |||
173 | return $str; |
||
174 | } |
||
175 | |||
176 | public function replace(string $from, string $to): AbstractString |
||
195 | } |
||
196 | |||
197 | public function slice(int $start = 0, int $length = null): AbstractString |
||
198 | { |
||
199 | $str = clone $this; |
||
200 | $str->string = mb_substr($this->string, $start, $length, 'UTF-8'); |
||
201 | |||
202 | return $str; |
||
203 | } |
||
204 | |||
205 | public function splice(string $replacement, int $start = 0, int $length = null): AbstractString |
||
206 | { |
||
207 | if (!preg_match('//u', $replacement)) { |
||
208 | throw new InvalidArgumentException('Invalid UTF-8 string.'); |
||
209 | } |
||
210 | |||
211 | $str = clone $this; |
||
212 | $start = $start ? \strlen(mb_substr($this->string, 0, $start, 'UTF-8')) : 0; |
||
213 | $length = $length ? \strlen(mb_substr($this->string, $start, $length, 'UTF-8')) : $length; |
||
214 | $str->string = substr_replace($this->string, $replacement, $start, $length ?? \PHP_INT_MAX); |
||
215 | |||
216 | return $str; |
||
217 | } |
||
218 | |||
219 | public function split(string $delimiter, int $limit = null, int $flags = null): array |
||
248 | } |
||
249 | |||
250 | public function startsWith($prefix): bool |
||
269 | } |
||
270 | } |
||
271 |