Complex classes like SRFWord 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 SRFWord, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class SRFWord extends FileExportPrinter { |
||
| 23 | /** |
||
| 24 | * set to true for debug output |
||
| 25 | */ |
||
| 26 | private $debug = true; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | private $rowNum; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | private $colNum; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Some printers do not mainly produce embeddable HTML or Wikitext, but |
||
| 42 | * produce stand-alone files. |
||
| 43 | * An example is RSS or iCalendar. This function |
||
| 44 | * returns the mimetype string that this file would have, or FALSE if no |
||
| 45 | * standalone files are produced. |
||
| 46 | * |
||
| 47 | * If this function returns something other than FALSE, then the printer will |
||
| 48 | * not be regarded as a printer that displays in-line results. This is used to |
||
| 49 | * determine if a file output should be generated in Special:Ask. |
||
| 50 | * |
||
| 51 | * @param SMWQueryResult $queryResult |
||
| 52 | * |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | public function getMimeType(SMWQueryResult $queryResult) { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * get a file name for the Word file |
||
| 61 | * |
||
| 62 | * if the filename parameter is not specified a filename is generated |
||
| 63 | * from the current time |
||
| 64 | * |
||
| 65 | * @param SMWQueryResult $queryResult |
||
| 66 | * |
||
| 67 | * @return string |
||
| 68 | * |
||
| 69 | */ |
||
| 70 | public function getFileName(SMWQueryResult $queryResult) { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * output the given query result with the given params as a file |
||
| 78 | * |
||
| 79 | * @param SMWQueryResult $queryResult |
||
| 80 | * |
||
| 81 | * @param array $params |
||
| 82 | * |
||
| 83 | */ |
||
| 84 | public function outputAsFile(SMWQueryResult $queryResult, array $params) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * return the parameter definitions |
||
| 95 | * searchlabel, templatefile and filename are possible |
||
| 96 | * |
||
| 97 | * @param $definitions \ParamProcessor\ParamDefinition[] |
||
| 98 | * |
||
| 99 | * @return array |
||
| 100 | */ |
||
| 101 | public function getParamDefinitions(array $definitions) { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Return serialised results in specified format. |
||
| 114 | * |
||
| 115 | * @param $res - |
||
| 116 | * the query result |
||
| 117 | * @param $outputMode - |
||
| 118 | * how to output - HTML or to a file? |
||
| 119 | */ |
||
| 120 | protected function getResultText(SMWQueryResult $res, $outputMode) { |
||
| 137 | |||
| 138 | /* |
||
| 139 | * Turns the PHPWord document object into a string |
||
| 140 | * @param document - the document |
||
| 141 | */ |
||
| 142 | private function writeDocumentToString($document) { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Populates the PHPWord document with the query data |
||
| 153 | * |
||
| 154 | * @param $res SMWQueryResult |
||
| 155 | * the query result |
||
| 156 | */ |
||
| 157 | private function populateDocumentWithQueryData($res) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * get the local ImageFilePath for the given filePageTitle |
||
| 169 | * |
||
| 170 | * @param $p_FilePageTitle - |
||
| 171 | * the title of the File: page without prefix |
||
| 172 | * @return the local file path to the image file |
||
| 173 | */ |
||
| 174 | function getImageFilePath($p_FilePageTitle) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Creates a new PHPWord document and returns it |
||
| 193 | * |
||
| 194 | * @return PHPWord |
||
| 195 | */ |
||
| 196 | private function createWordDocument() { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * filter labels |
||
| 229 | * |
||
| 230 | * @param |
||
| 231 | * $label |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | private function showLabel($label) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * check that a string starts with a given other string |
||
| 243 | * |
||
| 244 | * @param |
||
| 245 | * haystack - the string to search in |
||
| 246 | * @param |
||
| 247 | * needle - the string to search for |
||
| 248 | */ |
||
| 249 | function startsWith($haystack, $needle) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * set the Value for the given name and value in the word document |
||
| 256 | * |
||
| 257 | * @param |
||
| 258 | * p_name the name of the field (needle) |
||
| 259 | * @param |
||
| 260 | * p_value the content of the field (value) |
||
| 261 | */ |
||
| 262 | private function setValue($p_name, $p_value) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * get the Value for the given dataValue |
||
| 271 | * |
||
| 272 | * @param |
||
| 273 | * dataValue - the dataValue to read the value from |
||
| 274 | * @param |
||
| 275 | * plabel - the label |
||
| 276 | */ |
||
| 277 | private function readValue(/* SMWDataValue */ $dataValue, $plabel) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * read data from the given row |
||
| 329 | * |
||
| 330 | * @param $row - |
||
| 331 | * SMWResultArray |
||
| 332 | */ |
||
| 333 | private function readRowData($row) { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * check whether PHP Word is installed |
||
| 367 | */ |
||
| 368 | private function isPHPWordInstalled() { |
||
| 375 | } |
||
| 376 | |||
| 377 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.