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 | * @var array |
||
52 | */ |
||
53 | private $indexMultipliers; |
||
54 | |||
55 | /** |
||
56 | * Constructor |
||
57 | * |
||
58 | * @param string $path path to your PX file |
||
59 | */ |
||
60 | 9 | public function __construct($path) |
|
65 | |||
66 | /** |
||
67 | * Returns a list of all variables. |
||
68 | * |
||
69 | * @return array |
||
70 | */ |
||
71 | 4 | public function variables() |
|
72 | { |
||
73 | 4 | if (!$this->hasKeyword('STUB')) { |
|
74 | 1 | return $this->keyword('HEADING')->values; |
|
75 | 3 | } elseif (!$this->hasKeyword('HEADING')) { |
|
76 | return $this->keyword('STUB')->values; |
||
77 | } else { |
||
78 | 3 | return array_merge($this->keyword('STUB')->values, $this->keyword('HEADING')->values); |
|
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Returns a list of all possible values of a variable. |
||
84 | * |
||
85 | * @param string $variable |
||
86 | * |
||
87 | * @return array |
||
88 | */ |
||
89 | 3 | public function values($variable) |
|
98 | |||
99 | /** |
||
100 | * Returns a list of all possible codes of a variable. |
||
101 | * |
||
102 | * @param string $variable |
||
103 | * |
||
104 | * @return array|null |
||
105 | */ |
||
106 | 2 | public function codes($variable) |
|
116 | |||
117 | /** |
||
118 | * Computes the index within the data matrix. |
||
119 | * |
||
120 | * @param array $indices An array of all value indices |
||
121 | * |
||
122 | * @return int |
||
123 | */ |
||
124 | 2 | public function index($indices) |
|
135 | |||
136 | /** |
||
137 | * Gets a single data point. |
||
138 | * |
||
139 | * @param array $indices An array of all value indices |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | 2 | public function datum($indices) |
|
154 | |||
155 | /** |
||
156 | * Returns a list of all keywords. |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | public function keywords() |
||
166 | |||
167 | /** |
||
168 | * Returns all keywords with a given name. |
||
169 | * |
||
170 | * @param string $keyword |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | 8 | public function keywordList($keyword) |
|
184 | |||
185 | /** |
||
186 | * Checks whether a keyword exists. |
||
187 | * |
||
188 | * @param string $keyword |
||
189 | * |
||
190 | * @return bool |
||
191 | */ |
||
192 | 5 | public function hasKeyword($keyword) |
|
198 | |||
199 | /** |
||
200 | * Returns the first keyword with a given name. |
||
201 | * |
||
202 | * @param string $keyword |
||
203 | * |
||
204 | * @return object |
||
205 | */ |
||
206 | 5 | public function keyword($keyword) |
|
215 | |||
216 | /** |
||
217 | * Gets all data cells. |
||
218 | * |
||
219 | * @param array |
||
220 | */ |
||
221 | public function data() |
||
227 | |||
228 | 9 | private function parseKeywordLine($line) |
|
268 | |||
269 | 9 | private function assertKeywords() |
|
296 | |||
297 | 2 | private function assertData() |
|
338 | |||
339 | 2 | private function assertIndexMultipliers() |
|
354 | |||
355 | 9 | private function decodeLine($line) |
|
365 | |||
366 | 9 | private static function split($string) |
|
377 | |||
378 | 9 | private static function findQuoted($haystack, $needle) |
|
390 | |||
391 | 9 | private static function findQuotedReverse($haystack, $needle) |
|
404 | } |
||
405 |