Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like UStrObj 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 UStrObj, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class UStrObj extends StrObj |
||
6 | { |
||
7 | protected $chars = []; |
||
8 | protected $uhandler; |
||
9 | |||
10 | protected static $masks = [ |
||
11 | 2 => 0b00011111, |
||
12 | 3 => 0b00001111, |
||
13 | 4 => 0b00000111, |
||
14 | ]; |
||
15 | protected static $winc1umap = [ |
||
16 | 128 => 0x20AC, |
||
17 | 130 => 0x201A, |
||
18 | 131 => 0x0192, |
||
19 | 132 => 0x201E, |
||
20 | 133 => 0x2026, |
||
21 | 134 => 0x2020, |
||
22 | 135 => 0x2021, |
||
23 | 136 => 0x02C6, |
||
24 | 137 => 0x2030, |
||
25 | 138 => 0x0160, |
||
26 | 139 => 0x2039, |
||
27 | 140 => 0x0152, |
||
28 | 142 => 0x017D, |
||
29 | 145 => 0x2018, |
||
30 | 146 => 0x2019, |
||
31 | 147 => 0x201C, |
||
32 | 148 => 0x201D, |
||
33 | 149 => 0x2022, |
||
34 | 150 => 0x2013, |
||
35 | 151 => 0x2014, |
||
36 | 152 => 0x02DC, |
||
37 | 153 => 0x2122, |
||
38 | 154 => 0x0161, |
||
39 | 155 => 0x203A, |
||
40 | 156 => 0x0153, |
||
41 | 158 => 0x017E, |
||
42 | 159 => 0x0178, |
||
43 | ]; |
||
44 | |||
45 | protected static $c1umap = [ |
||
46 | 0xC280 => 0x20AC, |
||
47 | 0xC282 => 0x201A, |
||
48 | 0xC283 => 0x0192, |
||
49 | 0xC284 => 0x201E, |
||
50 | 0xC285 => 0x2026, |
||
51 | 0xC286 => 0x2020, |
||
52 | 0xC287 => 0x2021, |
||
53 | 0xC288 => 0x02C6, |
||
54 | 0xC289 => 0x2030, |
||
55 | 0xC28A => 0x0160, |
||
56 | 0xC28B => 0x2039, |
||
57 | 0xC28C => 0x0152, |
||
58 | 0xC28E => 0x017D, |
||
59 | 0xC291 => 0x2018, |
||
60 | 0xC292 => 0x2019, |
||
61 | 0xC293 => 0x201C, |
||
62 | 0xC294 => 0x201D, |
||
63 | 0xC295 => 0x2022, |
||
64 | 0xC296 => 0x2013, |
||
65 | 0xC297 => 0x2014, |
||
66 | 0xC298 => 0x02DC, |
||
67 | 0xC299 => 0x2122, |
||
68 | 0xC29A => 0x0161, |
||
69 | 0xC29B => 0x203A, |
||
70 | 0xC29C => 0x0153, |
||
71 | 0xC29E => 0x017E, |
||
72 | 0xC29F => 0x0178, |
||
73 | ]; |
||
74 | |||
75 | public function __construct($thing, $uhandler) |
||
80 | |||
81 | View Code Duplication | public function toArray($delim = '', $limit = null) |
|
96 | |||
97 | public function charAt($index) |
||
102 | |||
103 | public function charCodeAt($index) |
||
134 | |||
135 | private function loadToArray() |
||
167 | |||
168 | protected function parseUtf8CharAt($offset) |
||
209 | |||
210 | /** |
||
211 | * Determines if the byte at the given offset is part of a valid UTF8 char, |
||
212 | * and returns its actual starting offset, length in bytes, validity, |
||
213 | * and the byte at the original offset. |
||
214 | */ |
||
215 | protected function findUtf8CharAt($offset) |
||
284 | |||
285 | /** |
||
286 | * @param integer $byte |
||
287 | */ |
||
288 | protected static function charLength($byte) |
||
301 | } |
||
302 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.