Total Complexity | 71 |
Total Lines | 292 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like QRmask 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 QRmask, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class QRmask { |
||
35 | |||
36 | public $runLength = array(); |
||
37 | |||
38 | //---------------------------------------------------------------------- |
||
39 | public function __construct() |
||
40 | { |
||
41 | $this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0); |
||
42 | } |
||
43 | |||
44 | //---------------------------------------------------------------------- |
||
45 | public function writeFormatInformation($width, &$frame, $mask, $level) |
||
46 | { |
||
47 | $blacks = 0; |
||
48 | $format = QRspec::getFormatInfo($mask, $level); |
||
49 | |||
50 | for($i=0; $i<8; $i++) { |
||
51 | if($format & 1) { |
||
52 | $blacks += 2; |
||
53 | $v = 0x85; |
||
54 | } else { |
||
55 | $v = 0x84; |
||
56 | } |
||
57 | |||
58 | $frame[8][$width - 1 - $i] = chr($v); |
||
59 | if($i < 6) { |
||
60 | $frame[$i][8] = chr($v); |
||
61 | } else { |
||
62 | $frame[$i + 1][8] = chr($v); |
||
63 | } |
||
64 | $format = $format >> 1; |
||
65 | } |
||
66 | |||
67 | for($i=0; $i<7; $i++) { |
||
68 | if($format & 1) { |
||
69 | $blacks += 2; |
||
70 | $v = 0x85; |
||
71 | } else { |
||
72 | $v = 0x84; |
||
73 | } |
||
74 | |||
75 | $frame[$width - 7 + $i][8] = chr($v); |
||
76 | if($i == 0) { |
||
77 | $frame[8][7] = chr($v); |
||
78 | } else { |
||
79 | $frame[8][6 - $i] = chr($v); |
||
80 | } |
||
81 | |||
82 | $format = $format >> 1; |
||
83 | } |
||
84 | |||
85 | return $blacks; |
||
86 | } |
||
87 | |||
88 | //---------------------------------------------------------------------- |
||
89 | public function mask0($x, $y) { return ($x+$y)&1; } |
||
90 | public function mask1($x, $y) { return ($y&1); } |
||
91 | public function mask2($x, $y) { return ($x%3); } |
||
92 | public function mask3($x, $y) { return ($x+$y)%3; } |
||
93 | public function mask4($x, $y) { return (((int)($y/2))+((int)($x/3)))&1; } |
||
94 | public function mask5($x, $y) { return (($x*$y)&1)+($x*$y)%3; } |
||
97 | |||
98 | //---------------------------------------------------------------------- |
||
99 | private function generateMaskNo($maskNo, $width, $frame) |
||
100 | { |
||
101 | $bitMask = array_fill(0, $width, array_fill(0, $width, 0)); |
||
102 | |||
103 | for($y=0; $y<$width; $y++) { |
||
104 | for($x=0; $x<$width; $x++) { |
||
105 | if(ord($frame[$y][$x]) & 0x80) { |
||
106 | $bitMask[$y][$x] = 0; |
||
107 | } else { |
||
108 | $maskFunc = call_user_func(array($this, 'mask'.$maskNo), $x, $y); |
||
109 | $bitMask[$y][$x] = ($maskFunc == 0)?1:0; |
||
110 | } |
||
111 | |||
112 | } |
||
113 | } |
||
114 | |||
115 | return $bitMask; |
||
116 | } |
||
117 | |||
118 | //---------------------------------------------------------------------- |
||
119 | public static function serial($bitFrame) |
||
120 | { |
||
121 | $codeArr = array(); |
||
122 | |||
123 | foreach ($bitFrame as $line) |
||
124 | $codeArr[] = join('', $line); |
||
125 | |||
126 | return gzcompress(join("\n", $codeArr), 9); |
||
127 | } |
||
128 | |||
129 | //---------------------------------------------------------------------- |
||
130 | public static function unserial($code) |
||
131 | { |
||
132 | $codeArr = array(); |
||
133 | |||
134 | $codeLines = explode("\n", gzuncompress($code)); |
||
135 | foreach ($codeLines as $line) |
||
136 | $codeArr[] = str_split($line); |
||
137 | |||
138 | return $codeArr; |
||
139 | } |
||
140 | |||
141 | //---------------------------------------------------------------------- |
||
142 | public function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false) |
||
143 | { |
||
144 | $b = 0; |
||
145 | $bitMask = array(); |
||
146 | |||
147 | $fileName = QR_CACHE_DIR.'mask_'.$maskNo.DIRECTORY_SEPARATOR.'mask_'.$width.'_'.$maskNo.'.dat'; |
||
148 | |||
149 | if (QR_CACHEABLE) { |
||
150 | if (file_exists($fileName)) { |
||
151 | $bitMask = self::unserial(file_get_contents($fileName)); |
||
152 | } else { |
||
153 | $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d); |
||
|
|||
154 | if (!file_exists(QR_CACHE_DIR.'mask_'.$maskNo)) |
||
155 | mkdir(QR_CACHE_DIR.'mask_'.$maskNo); |
||
156 | file_put_contents($fileName, self::serial($bitMask)); |
||
157 | } |
||
158 | } else { |
||
159 | $bitMask = $this->generateMaskNo($maskNo, $width, $s, $d); |
||
160 | } |
||
161 | |||
162 | if ($maskGenOnly) |
||
163 | return; |
||
164 | |||
165 | $d = $s; |
||
166 | |||
167 | for($y=0; $y<$width; $y++) { |
||
168 | for($x=0; $x<$width; $x++) { |
||
169 | if($bitMask[$y][$x] == 1) { |
||
170 | $d[$y][$x] = chr(ord($s[$y][$x]) ^ (int)$bitMask[$y][$x]); |
||
171 | } |
||
172 | $b += (int)(ord($d[$y][$x]) & 1); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | return $b; |
||
177 | } |
||
178 | |||
179 | //---------------------------------------------------------------------- |
||
180 | public function makeMask($width, $frame, $maskNo, $level) |
||
181 | { |
||
182 | $masked = array_fill(0, $width, str_repeat("\0", $width)); |
||
183 | $this->makeMaskNo($maskNo, $width, $frame, $masked); |
||
184 | $this->writeFormatInformation($width, $masked, $maskNo, $level); |
||
185 | |||
186 | return $masked; |
||
187 | } |
||
188 | |||
189 | //---------------------------------------------------------------------- |
||
190 | public function calcN1N3($length) |
||
216 | } |
||
217 | |||
218 | //---------------------------------------------------------------------- |
||
219 | public function evaluateSymbol($width, $frame) |
||
220 | { |
||
221 | $head = 0; |
||
222 | $demerit = 0; |
||
223 | |||
224 | for($y=0; $y<$width; $y++) { |
||
225 | $head = 0; |
||
282 | } |
||
283 | |||
284 | |||
285 | //---------------------------------------------------------------------- |
||
286 | public function mask($width, $frame, $level) |
||
326 | } |
||
327 | |||
328 | //---------------------------------------------------------------------- |
||
330 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.