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 | 8 | public function __construct($path) |
|
65 | |||
66 | /** |
||
67 | * Returns a list of all variables. |
||
68 | * |
||
69 | * @return array |
||
70 | */ |
||
71 | 3 | public function variables() |
|
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 | 7 | public function keywordList($keyword) |
|
184 | |||
185 | /** |
||
186 | * Checks whether a keyword exists. |
||
187 | * |
||
188 | * @param string $keyword |
||
189 | * |
||
190 | * @return bool |
||
191 | */ |
||
192 | 1 | 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 | 4 | public function keyword($keyword) |
|
216 | |||
217 | /** |
||
218 | * Gets all data cells. |
||
219 | * |
||
220 | * @param array |
||
221 | */ |
||
222 | public function data() |
||
228 | |||
229 | 8 | private function parseKeywordLine($line) |
|
269 | |||
270 | 8 | private function assertKeywords() |
|
297 | |||
298 | 2 | private function assertData() |
|
339 | |||
340 | 2 | private function assertIndexMultipliers() |
|
355 | |||
356 | 8 | private function decodeLine($line) |
|
366 | |||
367 | 8 | private static function split($string) |
|
378 | |||
379 | 8 | private static function findQuoted($haystack, $needle) |
|
391 | |||
392 | 8 | private static function findQuotedReverse($haystack, $needle) |
|
405 | } |
||
406 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.