Complex classes like StrObj 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 StrObj, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class StrObj implements \ArrayAccess, \Countable, \Iterator |
||
6 | { |
||
7 | // CONSTANTS |
||
8 | |||
9 | const NORMAL = 0; |
||
10 | const START = 0; |
||
11 | const END = 1; |
||
12 | const BOTH_ENDS = 2; |
||
13 | const CASE_INSENSITIVE = 4; |
||
14 | const REVERSE = 8; |
||
15 | const EXACT_POSITION = 16; |
||
16 | const CURRENT_LOCALE = 32; |
||
17 | const NATURAL_ORDER = 64; |
||
18 | const FIRST_N = 128; |
||
19 | const C_STYLE = 256; |
||
20 | const META = 512; |
||
21 | const LAZY = 1024; |
||
22 | const GREEDY = 2048; |
||
23 | const WINDOWS1252 = 4096; |
||
24 | const UTF8 = 8192; |
||
25 | |||
26 | // STATIC PROPERTIES |
||
27 | |||
28 | protected static $asciimap = [ |
||
29 | 'a' => ['à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'â', 'ấ', |
||
30 | 'ầ', 'ẩ', 'ẫ', 'ậ', 'ā', 'ą', 'å', 'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', |
||
31 | 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', |
||
32 | 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ'], |
||
33 | 'b' => ['б', 'β', 'Ъ', 'Ь', 'ب'], |
||
34 | 'c' => ['ç', 'ć', 'č', 'ĉ', 'ċ'], |
||
35 | 'd' => ['ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д', 'δ', 'د', 'ض'], |
||
36 | 'e' => ['é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ', 'ệ', 'ë', 'ē', |
||
37 | 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ', 'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', |
||
38 | 'έ', 'е', 'ё', 'э', 'є', 'ə'], |
||
39 | 'f' => ['ф', 'φ', 'ف'], |
||
40 | 'g' => ['ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ج'], |
||
41 | 'h' => ['ĥ', 'ħ', 'η', 'ή', 'ح', 'ه'], |
||
42 | 'i' => ['í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į', 'ı', 'ι', 'ί', |
||
43 | 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ', 'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ', |
||
44 | 'ῑ', 'ῒ', 'ΐ', 'ῖ', 'ῗ', 'і', 'ї', 'и'], |
||
45 | 'j' => ['ĵ', 'ј', 'Ј'], |
||
46 | 'k' => ['ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك'], |
||
47 | 'l' => ['ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل'], |
||
48 | 'm' => ['м', 'μ', 'م'], |
||
49 | 'n' => ['ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن'], |
||
50 | 'o' => ['ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ơ', 'ớ', |
||
51 | 'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő', 'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ', |
||
52 | 'ὄ', 'ὅ', 'ὸ', 'ό', 'о', 'و', 'θ'], |
||
53 | 'p' => ['п', 'π'], |
||
54 | 'r' => ['ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر'], |
||
55 | 's' => ['ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص'], |
||
56 | 't' => ['ť', 'ţ', 'т', 'τ', 'ț', 'ت', 'ط'], |
||
57 | 'u' => ['ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'ứ', 'ừ', 'ử', 'ữ', 'ự', 'û', 'ū', |
||
58 | 'ů', 'ű', 'ŭ', 'ų', 'µ', 'у'], |
||
59 | 'v' => ['в'], |
||
60 | 'w' => ['ŵ', 'ω', 'ώ'], |
||
61 | 'x' => ['χ'], |
||
62 | 'y' => ['ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ', 'ϋ', 'ύ', 'ΰ', 'ي'], |
||
63 | 'z' => ['ź', 'ž', 'ż', 'з', 'ζ', 'ز'], |
||
64 | 'aa' => ['ع'], |
||
65 | 'ae' => ['ä', 'æ'], |
||
66 | 'ch' => ['ч'], |
||
67 | 'dj' => ['ђ', 'đ'], |
||
68 | 'dz' => ['џ'], |
||
69 | 'gh' => ['غ'], |
||
70 | 'kh' => ['х', 'خ'], |
||
71 | 'lj' => ['љ'], |
||
72 | 'nj' => ['њ'], |
||
73 | 'oe' => ['ö', 'œ'], |
||
74 | 'ps' => ['ψ'], |
||
75 | 'sh' => ['ш'], |
||
76 | 'shch' => ['щ'], |
||
77 | 'ss' => ['ß'], |
||
78 | 'th' => ['þ', 'ث', 'ذ', 'ظ'], |
||
79 | 'ts' => ['ц'], |
||
80 | 'ue' => ['ü'], |
||
81 | 'ya' => ['я'], |
||
82 | 'yu' => ['ю'],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', 'Ἷ', 'Ῐ', 'Ῑ', 'Ὶ', 'Ί', 'И', |
||
100 | 'І', 'Ї'], |
||
101 | 'K' => ['К', 'Κ'], |
||
102 | 'L' => ['Ĺ', 'Ł', 'Л', 'Λ', 'Ļ'], |
||
103 | 'M' => ['М', 'Μ'], |
||
104 | 'N' => ['Ń', 'Ñ', 'Ň', 'Ņ', 'Ŋ', 'Н', 'Ν'], |
||
105 | 'O' => ['Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ố', 'Ồ', 'Ổ', 'Ỗ', 'Ộ', 'Ơ', 'Ớ', |
||
106 | 'Ờ', 'Ở', 'Ỡ', 'Ợ', 'Ø', 'Ō', 'Ő', 'Ŏ', 'Ο', 'Ό', 'Ὀ', 'Ὁ', 'Ὂ', |
||
107 | 'Ὃ', 'Ὄ', 'Ὅ', 'Ὸ', 'Ό', 'О', 'Θ', 'Ө'], |
||
108 | 'P' => ['П', 'Π'], |
||
109 | 'R' => ['Ř', 'Ŕ', 'Р', 'Ρ'], |
||
110 | 'S' => ['Ş', 'Ŝ', 'Ș', 'Š', 'Ś', 'С', 'Σ'], |
||
111 | 'T' => ['Ť', 'Ţ', 'Ŧ', 'Ț', 'Т', 'Τ'], |
||
112 | 'U' => ['Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự', 'Û', 'Ū', |
||
113 | 'Ů', 'Ű', 'Ŭ', 'Ų', 'У'], |
||
114 | 'V' => ['В'], |
||
115 | 'W' => ['Ω', 'Ώ'], |
||
116 | 'X' => ['Χ'], |
||
117 | 'Y' => ['Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ', 'Ÿ', 'Ῠ', 'Ῡ', 'Ὺ', 'Ύ', 'Ы', 'Й', 'Υ', 'Ϋ'], |
||
118 | 'Z' => ['Ź', 'Ž', 'Ż', 'З', 'Ζ'], |
||
119 | 'AE' => ['Ä', 'Æ'], |
||
120 | 'CH' => ['Ч'], |
||
121 | 'DJ' => ['Ђ'], |
||
122 | 'DZ' => ['Џ'], |
||
123 | 'KH' => ['Х'], |
||
124 | 'LJ' => ['Љ'], |
||
125 | 'NJ' => ['Њ'], |
||
126 | 'OE' => ['Ö'], |
||
127 | 'PS' => ['Ψ'], |
||
128 | 'SH' => ['Ш'], |
||
129 | 'SHCH' => ['Щ'], |
||
130 | 'SS' => ['ẞ'], |
||
131 | 'TH' => ['Þ'], |
||
132 | 'TS' => ['Ц'], |
||
133 | 'UE' => ['Ü'], |
||
134 | 'YA' => ['Я'], |
||
135 | 'YU' => ['Ю'], |
||
136 | 'ZH' => ['Ж'], |
||
137 | ' ' => ["\xC2\xA0", "\xE2\x80\x80", "\xE2\x80\x81", "\xE2\x80\x82", |
||
138 | "\xE2\x80\x83", "\xE2\x80\x84", "\xE2\x80\x85", "\xE2\x80\x86", |
||
139 | "\xE2\x80\x87", "\xE2\x80\x88", "\xE2\x80\x89", "\xE2\x80\x8A", |
||
140 | "\xE2\x80\xAF", "\xE2\x81\x9F", "\xE3\x80\x80"], |
||
141 | ]; |
||
142 | protected static $winc1umap = [ |
||
143 | 128 => 0x20AC, |
||
144 | 130 => 0x201A, |
||
145 | 131 => 0x0192, |
||
146 | 132 => 0x201E, |
||
147 | 133 => 0x2026, |
||
148 | 134 => 0x2020, |
||
149 | 135 => 0x2021, |
||
150 | 136 => 0x02C6, |
||
151 | 137 => 0x2030, |
||
152 | 138 => 0x0160, |
||
153 | 139 => 0x2039, |
||
154 | 140 => 0x0152, |
||
155 | 142 => 0x017D, |
||
156 | 145 => 0x2018, |
||
157 | 146 => 0x2019, |
||
158 | 147 => 0x201C, |
||
159 | 148 => 0x201D, |
||
160 | 149 => 0x2022, |
||
161 | 150 => 0x2013, |
||
162 | 151 => 0x2014, |
||
163 | 152 => 0x02DC, |
||
164 | 153 => 0x2122, |
||
165 | 154 => 0x0161, |
||
166 | 155 => 0x203A, |
||
167 | 156 => 0x0153, |
||
168 | 158 => 0x017E, |
||
169 | 159 => 0x0178, |
||
170 | ]; |
||
171 | |||
172 | // PROPERTIES |
||
173 | |||
174 | protected $raw; |
||
175 | protected $encoding; |
||
176 | protected $token = false; |
||
177 | protected $caret = 0; |
||
178 | |||
179 | // MAGIC METHODS |
||
180 | |||
181 | public function __construct($thing, $enc = self::WINDOWS1252) |
||
192 | |||
193 | /** |
||
194 | * @return mixed |
||
195 | */ |
||
196 | public function __get($name) |
||
200 | |||
201 | /** |
||
202 | * @return string |
||
203 | */ |
||
204 | public function __toString() |
||
208 | |||
209 | public function toArray($delim = '', $limit = null) |
||
222 | |||
223 | // INFORMATIONAL METHODS |
||
224 | |||
225 | public function charAt($offset) |
||
229 | |||
230 | public function charCodeAt($offset) |
||
238 | |||
239 | public function compareTo($str, $mode = self::NORMAL, $length = 1) |
||
256 | |||
257 | public function indexOf($needle, $offset = 0, $mode = self::NORMAL) |
||
270 | |||
271 | public function length() |
||
275 | |||
276 | // MODIFYING METHODS |
||
277 | |||
278 | public function append($str) |
||
282 | |||
283 | public function asciify($removeUnsupported = true) |
||
294 | |||
295 | public function chunk($length = 76, $ending = "\r\n") |
||
299 | |||
300 | public function concat($str) |
||
304 | |||
305 | public function escape($mode = self::NORMAL, $charlist = '') |
||
317 | |||
318 | public function insertAt($str, $offset) |
||
322 | |||
323 | public function nextToken($delim) |
||
331 | |||
332 | public function pad($newlength, $padding = ' ', $mode = self::END) |
||
336 | |||
337 | public function prepend($str) |
||
341 | |||
342 | public function remove($str, $mode = self::NORMAL) |
||
346 | |||
347 | public function removeSubstr($start, $length = null) |
||
351 | |||
352 | public function repeat($times) |
||
356 | |||
357 | public function replace($search, $replace, $mode = self::NORMAL) |
||
364 | |||
365 | public function replaceSubstr($replacement, $start, $length = null) |
||
372 | |||
373 | public function resetToken() |
||
377 | |||
378 | public function reverse() |
||
382 | |||
383 | public function shuffle() |
||
387 | |||
388 | public function substr($start, $length = 'omitted') |
||
395 | |||
396 | public function times($times) |
||
400 | |||
401 | public function translate($search, $replace = '') |
||
408 | |||
409 | public function trim($mask = " \t\n\r\0\x0B", $mode = self::BOTH_ENDS) |
||
418 | |||
419 | public function unescape($mode = self::NORMAL) |
||
428 | |||
429 | public function uuDecode() |
||
433 | |||
434 | public function uuEncode() |
||
438 | |||
439 | public function wordwrap($width = 75, $break = "\n") |
||
443 | |||
444 | public function wordwrapBreaking($width = 75, $break = "\n") |
||
448 | |||
449 | // TESTING METHODS |
||
450 | |||
451 | public function contains($needle, $offset = 0, $mode = self::NORMAL) |
||
458 | |||
459 | public function countSubstr($needle, $offset = 0, $length = null) |
||
466 | |||
467 | public function endsWith($str, $mode = self::NORMAL) |
||
473 | |||
474 | public function equals($str) |
||
481 | |||
482 | public function isAscii() |
||
493 | |||
494 | public function isEmpty() |
||
498 | |||
499 | public function startsWith($str, $mode = self::NORMAL) |
||
504 | |||
505 | // INTERFACE IMPLEMENTATION METHODS |
||
506 | |||
507 | public function count() |
||
511 | |||
512 | public function current() |
||
516 | |||
517 | public function key() |
||
521 | |||
522 | public function next() |
||
526 | |||
527 | public function rewind() |
||
531 | |||
532 | public function valid() |
||
536 | |||
537 | public function offsetExists($offset) |
||
542 | |||
543 | public function offsetGet($offset) |
||
547 | |||
548 | public function offsetSet($offset, $value) |
||
552 | |||
553 | public function offsetUnset($offset) |
||
557 | |||
558 | // PRIVATE STATIC FUNCTIONS |
||
559 | |||
560 | protected static function testStringableObject($thing) |
||
568 | |||
569 | protected function parseUtf8CharAt($offset) |
||
615 | |||
616 | /** |
||
617 | * Determines if the byte at the given offset is part of a valid UTF8 char, |
||
618 | * and returns its actual starting offset, length in bytes, validity, |
||
619 | * and the byte at the original offset. |
||
620 | */ |
||
621 | protected function findUtf8CharAt($offset) |
||
688 | |||
689 | protected static function calcUtf8CharLength($byte) |
||
696 | } |
||
697 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: