Complex classes like Ico 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 Ico, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Ico |
||
14 | { |
||
15 | /** |
||
16 | * Ico::bgcolor |
||
17 | * Background color on icon extraction. |
||
18 | * |
||
19 | * @var array(R, G, B) = array(255, 255, 255) |
||
20 | **/ |
||
21 | public $bgcolor = [255, 255, 255]; |
||
22 | |||
23 | /** |
||
24 | * Ico::bgcolor_transparent |
||
25 | * Is background color transparent? |
||
26 | * |
||
27 | * @var bool = false |
||
28 | **/ |
||
29 | public $bgcolorTransparent = false; |
||
30 | |||
31 | private $filename; |
||
32 | private $ico; |
||
33 | private $formats; |
||
34 | |||
35 | /** |
||
36 | * Ico constructor. |
||
37 | * |
||
38 | * @param string $path optional path to ICO file |
||
39 | */ |
||
40 | 2 | public function __construct($path = '') |
|
46 | |||
47 | /** |
||
48 | * Load an ICO file (don't need to call this is if fill the |
||
49 | * parameter in the class constructor). |
||
50 | * |
||
51 | * @param string $path Path to ICO file |
||
52 | * |
||
53 | * @return bool Success |
||
54 | **/ |
||
55 | 2 | public function loadFile($path) |
|
70 | |||
71 | /** |
||
72 | * Load an ICO data. If you prefer to open the file |
||
73 | * and return the binary data you can use this function |
||
74 | * directly. Otherwise use loadFile() instead. |
||
75 | * |
||
76 | * @param string $data Binary data of ICO file |
||
77 | * |
||
78 | * @return bool Success |
||
79 | **/ |
||
80 | 2 | private function loadData($data) |
|
81 | { |
||
82 | 2 | $this->formats = []; |
|
83 | |||
84 | /** |
||
85 | * ICO header. |
||
86 | **/ |
||
87 | 2 | $icodata = unpack('SReserved/SType/SCount', $data); |
|
88 | 2 | $this->ico = $icodata; |
|
89 | 2 | $data = substr($data, 6); |
|
90 | |||
91 | /* |
||
92 | * Extract each icon header |
||
93 | **/ |
||
94 | 2 | for ($i = 0; $i < $this->ico['Count']; ++$i) { |
|
95 | 2 | $icodata = unpack('CWidth/CHeight/CColorCount/CReserved/SPlanes/SBitCount/LSizeInBytes/LFileOffset', $data); |
|
96 | 2 | $icodata['FileOffset'] -= ($this->ico['Count'] * 16) + 6; |
|
97 | 2 | if ($icodata['ColorCount'] == 0) { |
|
98 | 1 | $icodata['ColorCount'] = 256; |
|
99 | 1 | } |
|
100 | 2 | $this->formats[] = $icodata; |
|
101 | |||
102 | 2 | $data = substr($data, 16); |
|
103 | 2 | } |
|
104 | |||
105 | /* |
||
106 | * Extract aditional headers for each extracted icon header |
||
107 | **/ |
||
108 | 2 | $formatCount=count($this->formats); |
|
109 | 2 | for ($i = 0; $i < $formatCount; ++$i) { |
|
110 | 2 | $icodata = unpack( |
|
111 | 'LSize/LWidth/LHeight/SPlanes/SBitCount/LCompression/LImageSize/'. |
||
112 | 2 | 'LXpixelsPerM/LYpixelsPerM/LColorsUsed/LColorsImportant', |
|
113 | 2 | substr($data, $this->formats[$i]['FileOffset']) |
|
114 | 2 | ); |
|
115 | |||
116 | 2 | $this->formats[$i]['header'] = $icodata; |
|
117 | 2 | $this->formats[$i]['colors'] = []; |
|
118 | |||
119 | 2 | $this->formats[$i]['BitCount'] = $this->formats[$i]['header']['BitCount']; |
|
120 | |||
121 | 2 | switch ($this->formats[$i]['BitCount']) { |
|
122 | 2 | case 32: |
|
123 | 2 | case 24: |
|
124 | 1 | $length = $this->formats[$i]['header']['Width'] * |
|
125 | 1 | $this->formats[$i]['header']['Height'] * |
|
126 | 1 | ($this->formats[$i]['BitCount'] / 8); |
|
127 | 1 | $this->formats[$i]['data'] = substr( |
|
128 | 1 | $data, |
|
129 | 1 | $this->formats[$i]['FileOffset'] + $this->formats[$i]['header']['Size'], |
|
130 | $length |
||
131 | 1 | ); |
|
132 | 1 | break; |
|
133 | 1 | case 8: |
|
134 | 1 | case 4: |
|
135 | 1 | $icodata = substr( |
|
136 | 1 | $data, |
|
137 | 1 | $this->formats[$i]['FileOffset'] + $icodata['Size'], |
|
138 | 1 | $this->formats[$i]['ColorCount'] * 4 |
|
139 | 1 | ); |
|
140 | 1 | $offset = 0; |
|
141 | 1 | for ($j = 0; $j < $this->formats[$i]['ColorCount']; ++$j) { |
|
142 | 1 | $this->formats[$i]['colors'][] = [ |
|
143 | 1 | 'blue' => ord($icodata[$offset]), |
|
144 | 1 | 'green' => ord($icodata[$offset + 1]), |
|
145 | 1 | 'red' => ord($icodata[$offset + 2]), |
|
146 | 1 | 'reserved' => ord($icodata[$offset + 3]), |
|
147 | ]; |
||
148 | 1 | $offset += 4; |
|
149 | 1 | } |
|
150 | 1 | $length = $this->formats[$i]['header']['Width'] * |
|
151 | 1 | $this->formats[$i]['header']['Height'] * |
|
152 | 1 | (1 + $this->formats[$i]['BitCount']) / $this->formats[$i]['BitCount']; |
|
153 | 1 | $this->formats[$i]['data'] = substr( |
|
154 | 1 | $data, |
|
155 | 1 | $this->formats[$i]['FileOffset'] + |
|
156 | 1 | ($this->formats[$i]['ColorCount'] * 4) + |
|
157 | 1 | $this->formats[$i]['header']['Size'], |
|
158 | $length |
||
159 | 1 | ); |
|
160 | 1 | break; |
|
161 | case 1: |
||
162 | $icodata = substr( |
||
163 | $data, |
||
164 | $this->formats[$i]['FileOffset'] + $icodata['Size'], |
||
165 | $this->formats[$i]['ColorCount'] * 4 |
||
166 | ); |
||
167 | |||
168 | $this->formats[$i]['colors'][] = [ |
||
169 | 'blue' => ord($icodata[0]), |
||
170 | 'green' => ord($icodata[1]), |
||
171 | 'red' => ord($icodata[2]), |
||
172 | 'reserved' => ord($icodata[3]), |
||
173 | ]; |
||
174 | $this->formats[$i]['colors'][] = [ |
||
175 | 'blue' => ord($icodata[4]), |
||
176 | 'green' => ord($icodata[5]), |
||
177 | 'red' => ord($icodata[6]), |
||
178 | 'reserved' => ord($icodata[7]), |
||
179 | ]; |
||
180 | |||
181 | $length = $this->formats[$i]['header']['Width'] * $this->formats[$i]['header']['Height'] / 8; |
||
182 | $this->formats[$i]['data'] = substr( |
||
183 | $data, |
||
184 | $this->formats[$i]['FileOffset'] + $this->formats[$i]['header']['Size'] + 8, |
||
185 | $length |
||
186 | ); |
||
187 | break; |
||
188 | 2 | } |
|
189 | 2 | $this->formats[$i]['data_length'] = strlen($this->formats[$i]['data']); |
|
190 | 2 | } |
|
191 | |||
192 | 2 | return true; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Return the total icons extracted at the moment. |
||
197 | * |
||
198 | * @return int Total icons |
||
199 | **/ |
||
200 | 2 | public function getTotalIcons() |
|
204 | |||
205 | /** |
||
206 | * Ico::GetIconInfo() |
||
207 | * Return the icon header corresponding to that index. |
||
208 | * |
||
209 | * @param int $index Icon index |
||
210 | * |
||
211 | * @return resource|bool Icon header or false |
||
212 | **/ |
||
213 | 2 | public function getIconInfo($index) |
|
221 | |||
222 | /** |
||
223 | * Changes background color of extraction. You can set |
||
224 | * the 3 color components or set $red = '#xxxxxx' (HTML format) |
||
225 | * and leave all other blanks. |
||
226 | * |
||
227 | * @param int $red Red component |
||
228 | * @param int $green Green component |
||
229 | * @param int $blue Blue component |
||
230 | **/ |
||
231 | 2 | public function setBackground($red = 255, $green = 255, $blue = 255) |
|
241 | |||
242 | /** |
||
243 | * Set background color to be saved as transparent. |
||
244 | * |
||
245 | * @param bool $is_transparent Is Transparent or not |
||
246 | * |
||
247 | * @return bool Is Transparent or not |
||
248 | **/ |
||
249 | public function setBackgroundTransparent($is_transparent = true) |
||
253 | |||
254 | /** |
||
255 | * Return an image resource with the icon stored |
||
256 | * on the $index position of the ICO file. |
||
257 | * |
||
258 | * @param int $index Position of the icon inside ICO |
||
259 | * |
||
260 | * @return resource|bool Image resource |
||
261 | **/ |
||
262 | 2 | public function getImage($index) |
|
424 | |||
425 | /** |
||
426 | * Ico::AllocateColor() |
||
427 | * Allocate a color on $im resource. This function prevents |
||
428 | * from allocating same colors on the same pallete. Instead |
||
429 | * if it finds that the color is already allocated, it only |
||
430 | * returns the index to that color. |
||
431 | * It supports alpha channel. |
||
432 | * |
||
433 | * @param resource $im Image resource |
||
434 | * @param int $red Red component |
||
435 | * @param int $green Green component |
||
436 | * @param int $blue Blue component |
||
437 | * @param int $alpha Alpha channel |
||
438 | * |
||
439 | * @return int Color index |
||
440 | **/ |
||
441 | 2 | private function allocateColor(&$im, $red, $green, $blue, $alpha = 0) |
|
450 | } |
||
451 |