Complex classes like Px 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 Px, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Px |
||
12 | { |
||
13 | const DEFAULT_CHARSET = '437'; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $path; |
||
19 | |||
20 | /** |
||
21 | * @var resource |
||
22 | */ |
||
23 | private $handle; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | private $keywords; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $data; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | private $dataOffset; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $charset; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $codepage; |
||
49 | |||
50 | /** |
||
51 | * Constructor |
||
52 | * |
||
53 | * @param string $path path to your PX file |
||
54 | */ |
||
55 | 8 | public function __construct($path) |
|
60 | |||
61 | /** |
||
62 | * Returns a list of all variables. |
||
63 | * |
||
64 | * @return array |
||
65 | */ |
||
66 | 3 | public function variables() |
|
70 | |||
71 | /** |
||
72 | * Returns a list of all possible values of a variable. |
||
73 | * |
||
74 | * @param string $variable |
||
75 | * |
||
76 | * @return array |
||
77 | */ |
||
78 | 3 | public function values($variable) |
|
87 | |||
88 | /** |
||
89 | * Returns a list of all possible codes of a variable. |
||
90 | * |
||
91 | * @param string $variable |
||
92 | * |
||
93 | * @return array|null |
||
94 | */ |
||
95 | 2 | public function codes($variable) |
|
105 | |||
106 | /** |
||
107 | * Computes the index within the data matrix. |
||
108 | * |
||
109 | * @param array $indices An array of all value indices |
||
110 | * |
||
111 | * @return int |
||
112 | */ |
||
113 | 2 | public function index($indices) |
|
127 | |||
128 | /** |
||
129 | * Gets a single data point. |
||
130 | * |
||
131 | * @param array $indices An array of all value indices |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | 2 | public function datum($indices) |
|
146 | |||
147 | /** |
||
148 | * Returns a list of all keywords. |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | public function keywords() |
||
158 | |||
159 | /** |
||
160 | * Returns all keywords with a given name. |
||
161 | * |
||
162 | * @param string $keyword |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | 7 | public function keywordList($keyword) |
|
176 | |||
177 | /** |
||
178 | * Checks whether a keyword exists. |
||
179 | * |
||
180 | * @param string $keyword |
||
181 | * |
||
182 | * @return bool |
||
183 | */ |
||
184 | 1 | public function hasKeyword($keyword) |
|
190 | |||
191 | /** |
||
192 | * Returns the first keyword with a given name. |
||
193 | * |
||
194 | * @param string $keyword |
||
195 | * |
||
196 | * @return object |
||
197 | */ |
||
198 | 4 | public function keyword($keyword) |
|
207 | |||
208 | /** |
||
209 | * Gets all data cells. |
||
210 | * |
||
211 | * @param array |
||
212 | */ |
||
213 | public function data() |
||
219 | |||
220 | 8 | private function parseKeywordLine($line) |
|
260 | |||
261 | 8 | private function assertKeywords() |
|
288 | |||
289 | 2 | private function assertData() |
|
330 | |||
331 | 8 | private function decodeLine($line) |
|
341 | |||
342 | 8 | private static function split($string) |
|
353 | |||
354 | 8 | private static function findQuoted($haystack, $needle) |
|
366 | |||
367 | 8 | private static function findQuotedReverse($haystack, $needle) |
|
380 | } |
||
381 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: