Total Complexity | 63 |
Total Lines | 277 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like QRsplit 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 QRsplit, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class QRsplit { |
||
36 | |||
37 | public $dataStr = ''; |
||
38 | public $input; |
||
39 | public $modeHint; |
||
40 | |||
41 | //---------------------------------------------------------------------- |
||
42 | public function __construct($dataStr, $input, $modeHint) |
||
43 | { |
||
44 | $this->dataStr = $dataStr; |
||
45 | $this->input = $input; |
||
46 | $this->modeHint = $modeHint; |
||
47 | } |
||
48 | |||
49 | //---------------------------------------------------------------------- |
||
50 | public static function isdigitat($str, $pos) |
||
51 | { |
||
52 | if ($pos >= strlen($str)) |
||
53 | return false; |
||
54 | |||
55 | return ((ord($str[$pos]) >= ord('0'))&&(ord($str[$pos]) <= ord('9'))); |
||
56 | } |
||
57 | |||
58 | //---------------------------------------------------------------------- |
||
59 | public static function isalnumat($str, $pos) |
||
60 | { |
||
61 | if ($pos >= strlen($str)) |
||
62 | return false; |
||
63 | |||
64 | return (QRinput::lookAnTable(ord($str[$pos])) >= 0); |
||
65 | } |
||
66 | |||
67 | //---------------------------------------------------------------------- |
||
68 | public function identifyMode($pos) |
||
69 | { |
||
70 | if ($pos >= strlen($this->dataStr)) |
||
71 | return QR_MODE_NUL; |
||
72 | |||
73 | $c = $this->dataStr[$pos]; |
||
74 | |||
75 | if(self::isdigitat($this->dataStr, $pos)) { |
||
76 | return QR_MODE_NUM; |
||
77 | } else if(self::isalnumat($this->dataStr, $pos)) { |
||
78 | return QR_MODE_AN; |
||
79 | } else if($this->modeHint == QR_MODE_KANJI) { |
||
80 | |||
81 | if ($pos+1 < strlen($this->dataStr)) |
||
82 | { |
||
83 | $d = $this->dataStr[$pos+1]; |
||
84 | $word = (ord($c) << 8) | ord($d); |
||
85 | if(($word >= 0x8140 && $word <= 0x9ffc) || ($word >= 0xe040 && $word <= 0xebbf)) { |
||
86 | return QR_MODE_KANJI; |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | |||
91 | return QR_MODE_8; |
||
92 | } |
||
93 | |||
94 | //---------------------------------------------------------------------- |
||
95 | public function eatNum() |
||
129 | } |
||
130 | |||
131 | //---------------------------------------------------------------------- |
||
132 | public function eatAn() |
||
133 | { |
||
134 | $la = QRspec::lengthIndicator(QR_MODE_AN, $this->input->getVersion()); |
||
135 | $ln = QRspec::lengthIndicator(QR_MODE_NUM, $this->input->getVersion()); |
||
136 | |||
137 | $p = 0; |
||
138 | |||
139 | while(self::isalnumat($this->dataStr, $p)) { |
||
140 | if(self::isdigitat($this->dataStr, $p)) { |
||
141 | $q = $p; |
||
142 | while(self::isdigitat($this->dataStr, $q)) { |
||
143 | $q++; |
||
144 | } |
||
145 | |||
146 | $dif = QRinput::estimateBitsModeAn($p) // + 4 + la |
||
147 | + QRinput::estimateBitsModeNum($q - $p) + 4 + $ln |
||
148 | - QRinput::estimateBitsModeAn($q); // - 4 - la |
||
149 | |||
150 | if($dif < 0) { |
||
151 | break; |
||
152 | } else { |
||
153 | $p = $q; |
||
154 | } |
||
155 | } else { |
||
156 | $p++; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | $run = $p; |
||
161 | |||
162 | if(!self::isalnumat($this->dataStr, $p)) { |
||
163 | $dif = QRinput::estimateBitsModeAn($run) + 4 + $la |
||
164 | + QRinput::estimateBitsMode8(1) // + 4 + l8 |
||
165 | - QRinput::estimateBitsMode8($run + 1); // - 4 - l8 |
||
166 | if($dif > 0) { |
||
167 | return $this->eat8(); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | $ret = $this->input->append(QR_MODE_AN, $run, str_split($this->dataStr)); |
||
172 | if($ret < 0) |
||
173 | return -1; |
||
174 | |||
175 | return $run; |
||
176 | } |
||
177 | |||
178 | //---------------------------------------------------------------------- |
||
179 | public function eatKanji() |
||
180 | { |
||
181 | $p = 0; |
||
182 | |||
183 | while($this->identifyMode($p) == QR_MODE_KANJI) { |
||
184 | $p += 2; |
||
185 | } |
||
186 | |||
187 | $ret = $this->input->append(QR_MODE_KANJI, $p, str_split($this->dataStr)); |
||
188 | if($ret < 0) |
||
189 | return -1; |
||
190 | |||
191 | return $run; |
||
|
|||
192 | } |
||
193 | |||
194 | //---------------------------------------------------------------------- |
||
195 | public function eat8() |
||
247 | } |
||
248 | |||
249 | //---------------------------------------------------------------------- |
||
250 | public function splitString() |
||
251 | { |
||
252 | while (strlen($this->dataStr) > 0) |
||
253 | { |
||
254 | if($this->dataStr == '') |
||
255 | return 0; |
||
256 | |||
257 | $mode = $this->identifyMode(0); |
||
258 | |||
259 | switch ($mode) { |
||
260 | case QR_MODE_NUM: $length = $this->eatNum(); break; |
||
261 | case QR_MODE_AN: $length = $this->eatAn(); break; |
||
262 | case QR_MODE_KANJI: |
||
263 | if ($hint == QR_MODE_KANJI) |
||
264 | $length = $this->eatKanji(); |
||
265 | else $length = $this->eat8(); |
||
266 | break; |
||
267 | default: $length = $this->eat8(); break; |
||
268 | |||
269 | } |
||
270 | |||
271 | if($length == 0) return 0; |
||
272 | if($length < 0) return -1; |
||
273 | |||
274 | $this->dataStr = substr($this->dataStr, $length); |
||
275 | } |
||
276 | } |
||
277 | |||
278 | //---------------------------------------------------------------------- |
||
279 | public function toUpper() |
||
280 | { |
||
281 | $stringLen = strlen($this->dataStr); |
||
282 | $p = 0; |
||
283 | |||
284 | while ($p<$stringLen) { |
||
285 | $mode = self::identifyMode(substr($this->dataStr, $p), $this->modeHint); |
||
286 | if($mode == QR_MODE_KANJI) { |
||
287 | $p += 2; |
||
288 | } else { |
||
289 | if (ord($this->dataStr[$p]) >= ord('a') && ord($this->dataStr[$p]) <= ord('z')) { |
||
290 | $this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32); |
||
291 | } |
||
292 | $p++; |
||
293 | } |
||
294 | } |
||
295 | |||
296 | return $this->dataStr; |
||
297 | } |
||
298 | |||
299 | //---------------------------------------------------------------------- |
||
300 | public static function splitStringToQRinput($string, QRinput $input, $modeHint, $casesensitive = true) |
||
312 | } |
||
313 | } |