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() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Returns a list of all possible values of a variable. |
||
| 78 | * |
||
| 79 | * @param string $variable |
||
| 80 | * |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | 3 | public function values($variable) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Returns a list of all possible codes of a variable. |
||
| 95 | * |
||
| 96 | * @param string $variable |
||
| 97 | * |
||
| 98 | * @return array|null |
||
| 99 | */ |
||
| 100 | 2 | public function codes($variable) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Computes the index within the data matrix. |
||
| 113 | * |
||
| 114 | * @param array $indices An array of all value indices |
||
| 115 | * |
||
| 116 | * @return int |
||
| 117 | */ |
||
| 118 | 2 | public function index($indices) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Gets a single data point. |
||
| 132 | * |
||
| 133 | * @param array $indices An array of all value indices |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | 2 | public function datum($indices) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Returns a list of all keywords. |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | public function keywords() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns all keywords with a given name. |
||
| 163 | * |
||
| 164 | * @param string $keyword |
||
| 165 | * |
||
| 166 | * @return array |
||
| 167 | */ |
||
| 168 | 7 | public function keywordList($keyword) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Checks whether a keyword exists. |
||
| 181 | * |
||
| 182 | * @param string $keyword |
||
| 183 | * |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | 1 | public function hasKeyword($keyword) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Returns the first keyword with a given name. |
||
| 195 | * |
||
| 196 | * @param string $keyword |
||
| 197 | * |
||
| 198 | * @return object |
||
| 199 | */ |
||
| 200 | 4 | public function keyword($keyword) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Gets all data cells. |
||
| 212 | * |
||
| 213 | * @param array |
||
| 214 | */ |
||
| 215 | public function data() |
||
| 221 | |||
| 222 | 8 | private function parseKeywordLine($line) |
|
| 262 | |||
| 263 | 8 | private function assertKeywords() |
|
| 290 | |||
| 291 | 2 | private function assertData() |
|
| 332 | |||
| 333 | 2 | private function assertIndexMultipliers() |
|
| 348 | |||
| 349 | 8 | private function decodeLine($line) |
|
| 359 | |||
| 360 | 8 | private static function split($string) |
|
| 371 | |||
| 372 | 8 | private static function findQuoted($haystack, $needle) |
|
| 384 | |||
| 385 | 8 | private static function findQuotedReverse($haystack, $needle) |
|
| 398 | } |
||
| 399 |