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 |
||
| 20 | class SRFWord extends FileExportPrinter { |
||
| 21 | /** |
||
| 22 | * set to true for debug output |
||
| 23 | */ |
||
| 24 | private $debug = true; |
||
| 25 | |||
| 26 | // show imageinformation in the word file |
||
| 27 | private $imageDebug=false; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * |
||
| 31 | * @var int |
||
| 32 | */ |
||
| 33 | private $rowNum; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | private $colNum; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Some printers do not mainly produce embeddable HTML or Wikitext, but |
||
| 43 | * produce stand-alone files. |
||
| 44 | * An example is RSS or iCalendar. This function |
||
| 45 | * returns the mimetype string that this file would have, or FALSE if no |
||
| 46 | * standalone files are produced. |
||
| 47 | * |
||
| 48 | * If this function returns something other than FALSE, then the printer will |
||
| 49 | * not be regarded as a printer that displays in-line results. This is used to |
||
| 50 | * determine if a file output should be generated in Special:Ask. |
||
| 51 | * |
||
| 52 | * @param SMWQueryResult $queryResult |
||
| 53 | * |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | public function getMimeType(SMWQueryResult $queryResult) { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * get a file name for the Word file |
||
| 62 | * |
||
| 63 | * if the filename parameter is not specified a filename is generated |
||
| 64 | * from the current time |
||
| 65 | * |
||
| 66 | * @param SMWQueryResult $queryResult |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | * |
||
| 70 | */ |
||
| 71 | public function getFileName(SMWQueryResult $queryResult) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * output the given query result with the given params as a file |
||
| 79 | * |
||
| 80 | * @param SMWQueryResult $queryResult |
||
| 81 | * |
||
| 82 | * @param array $params |
||
| 83 | * |
||
| 84 | */ |
||
| 85 | public function outputAsFile(SMWQueryResult $queryResult, array $params) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * return the parameter definitions |
||
| 96 | * searchlabel, templatefile and filename are possible |
||
| 97 | * |
||
| 98 | * @param $definitions \ParamProcessor\ParamDefinition[] |
||
| 99 | * |
||
| 100 | * @return array |
||
| 101 | */ |
||
| 102 | public function getParamDefinitions(array $definitions) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Return serialised results in specified format. |
||
| 115 | * |
||
| 116 | * @param $res - |
||
| 117 | * the query result |
||
| 118 | * @param $outputMode - |
||
| 119 | * how to output - HTML or to a file? |
||
| 120 | */ |
||
| 121 | protected function getResultText(SMWQueryResult $res, $outputMode) { |
||
| 138 | |||
| 139 | /* |
||
| 140 | * Turns the PHPWord document object into a string |
||
| 141 | * @param document - the document |
||
| 142 | */ |
||
| 143 | private function writeDocumentToString($document) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Populates the PHPWord document with the query data |
||
| 154 | * |
||
| 155 | * @param $res SMWQueryResult |
||
| 156 | * the query result |
||
| 157 | */ |
||
| 158 | private function populateDocumentWithQueryData($res) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * get the local ImageFilePath for the given filePageTitle |
||
| 170 | * |
||
| 171 | * @param $p_FilePageTitle - |
||
| 172 | * the title of the File: page without prefix |
||
| 173 | * @return the local file path to the image file |
||
| 174 | */ |
||
| 175 | function getImageFilePath($p_FilePageTitle) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Creates a new PHPWord document and returns it |
||
| 194 | * |
||
| 195 | * @return PHPWord |
||
| 196 | */ |
||
| 197 | private function createWordDocument() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * filter labels |
||
| 230 | * |
||
| 231 | * @param |
||
| 232 | * $label |
||
| 233 | * @return bool |
||
| 234 | */ |
||
| 235 | private function showLabel($label) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * check that a string starts with a given other string |
||
| 244 | * |
||
| 245 | * @param |
||
| 246 | * haystack - the string to search in |
||
| 247 | * @param |
||
| 248 | * needle - the string to search for |
||
| 249 | */ |
||
| 250 | function startsWith($haystack, $needle) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * set the Value for the given name and value in the word document |
||
| 257 | * |
||
| 258 | * @param |
||
| 259 | * p_name the name of the field (needle) |
||
| 260 | * @param |
||
| 261 | * p_value the content of the field (value) |
||
| 262 | */ |
||
| 263 | private function setValue($p_name, $p_value) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * get the Value for the given dataValue |
||
| 272 | * |
||
| 273 | * @param |
||
| 274 | * dataValue - the dataValue to read the value from |
||
| 275 | * @param |
||
| 276 | * plabel - the label |
||
| 277 | */ |
||
| 278 | private function readValue(/* SMWDataValue */ $dataValue, $plabel) { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * read data from the given row |
||
| 335 | * |
||
| 336 | * @param $row - |
||
| 337 | * SMWResultArray |
||
| 338 | */ |
||
| 339 | private function readRowData($row) { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * check whether PHP Word is installed |
||
| 373 | */ |
||
| 374 | private function isPHPWordInstalled() { |
||
| 381 | } |
||
| 382 | |||
| 383 |
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.