Complex classes like WikiPlugin_VisualWiki 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 WikiPlugin_VisualWiki, and based on these observations, apply Extract Interface, too.
| 1 | <?php // -*-php-*- |
||
| 37 | class WikiPlugin_VisualWiki |
||
| 38 | extends WikiPlugin_GraphViz |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * Sets plugin type to map production |
||
| 42 | */ |
||
| 43 | function getPluginType() { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Sets the plugin's name to VisualWiki. It can be called by |
||
| 49 | * <code><?plugin VisualWiki?></code>, now. This |
||
| 50 | * name must correspond to the filename and the class name. |
||
| 51 | */ |
||
| 52 | function getName() { |
||
| 55 | |||
| 56 | function getVersion() { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Sets textual description. |
||
| 63 | */ |
||
| 64 | function getDescription() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Returns default arguments. This is put into a separate |
||
| 70 | * function to allow its usage by both <code>getDefaultArguments</code> |
||
| 71 | * and <code>checkArguments</code> |
||
| 72 | */ |
||
| 73 | function defaultarguments() { |
||
| 74 | return array('imgtype' => 'png', |
||
| 75 | 'width' => false, // was 5, scale it automatically |
||
| 76 | 'height' => false, // was 7, scale it automatically |
||
| 77 | 'colorby' => 'age', // sort by 'age' or 'revtime' |
||
| 78 | 'fillnodes' => 'off', |
||
| 79 | 'label' => 'name', |
||
| 80 | 'shape' => 'ellipse', |
||
| 81 | 'large_nb' => 5, |
||
| 82 | 'recent_nb' => 5, |
||
| 83 | 'refined_nb' => 15, |
||
| 84 | 'backlink_nb' => 5, |
||
| 85 | 'neighbour_list' => '', |
||
| 86 | 'exclude_list' => '', |
||
| 87 | 'include_list' => '', |
||
| 88 | 'fontsize' => 9, |
||
| 89 | 'debug' => false, |
||
| 90 | 'help' => false ); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Sets the default arguments. WikiPlugin also regards these as |
||
| 95 | * the allowed arguments. Since WikiPluginCached stores an image |
||
| 96 | * for each different set of parameters, there can be a lot of |
||
| 97 | * these (large) graphs if you allow different parameters. |
||
| 98 | * Set <code>VISUALWIKI_ALLOWOPTIONS</code> to <code>false</code> |
||
| 99 | * to allow no options to be set and use only the default parameters. |
||
| 100 | * This will need an disk space of about 20 Kbyte all the time. |
||
| 101 | */ |
||
| 102 | function getDefaultArguments() { |
||
| 103 | if (VISUALWIKI_ALLOWOPTIONS) |
||
| 104 | return $this->defaultarguments(); |
||
|
|
|||
| 105 | else |
||
| 106 | return array(); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Substitutes each forbidden parameter value by the default value |
||
| 111 | * defined in <code>defaultarguments</code>. |
||
| 112 | */ |
||
| 113 | function checkArguments(&$arg) { |
||
| 114 | extract($arg); |
||
| 115 | $def = $this->defaultarguments(); |
||
| 116 | if (($width < 3) || ($width > 15)) |
||
| 117 | $arg['width'] = $def['width']; |
||
| 118 | if (($height < 3) || ($height > 20)) |
||
| 119 | $arg['height'] = $def['height']; |
||
| 120 | if (($fontsize < 8) || ($fontsize > 24)) |
||
| 121 | $arg['fontsize'] = $def['fontsize']; |
||
| 122 | if (!in_array($label, array('name', 'number'))) |
||
| 123 | $arg['label'] = $def['label']; |
||
| 124 | |||
| 125 | if (!in_array($shape, array('ellipse', 'box', 'point', 'circle', |
||
| 126 | 'plaintext'))) |
||
| 127 | $arg['shape'] = $def['shape']; |
||
| 128 | if (!in_array($colorby, array('age', 'revtime'))) |
||
| 129 | $arg['colorby'] = $def['colorby']; |
||
| 130 | if (!in_array($fillnodes, array('on', 'off'))) |
||
| 131 | $arg['fillnodes'] = $def['fillnodes']; |
||
| 132 | if (($large_nb < 0) || ($large_nb > 50)) |
||
| 133 | $arg['large_nb'] = $def['large_nb']; |
||
| 134 | if (($recent_nb < 0) || ($recent_nb > 50)) |
||
| 135 | $arg['recent_nb'] = $def['recent_nb']; |
||
| 136 | if (($refined_nb < 0 ) || ( $refined_nb > 50)) |
||
| 137 | $arg['refined_nb'] = $def['refined_nb']; |
||
| 138 | if (($backlink_nb < 0) || ($backlink_nb > 50)) |
||
| 139 | $arg['backlink_nb'] = $def['backlink_nb']; |
||
| 140 | // ToDo: check if "ImageCreateFrom$imgtype"() exists. |
||
| 141 | if (!in_array($imgtype, $GLOBALS['PLUGIN_CACHED_IMGTYPES'])) |
||
| 142 | $arg['imgtype'] = $def['imgtype']; |
||
| 143 | if (empty($fontname)) |
||
| 144 | $arg['fontname'] = VISUALWIKIFONT; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Checks options, creates help page if necessary, calls both |
||
| 149 | * database access and image map production functions. |
||
| 150 | * @return array($map,$html) |
||
| 151 | */ |
||
| 152 | function getMap($dbi, $argarray, $request) { |
||
| 153 | if (!VISUALWIKI_ALLOWOPTIONS) |
||
| 154 | $argarray = $this->defaultarguments(); |
||
| 155 | $this->checkArguments($argarray); |
||
| 156 | $request->setArg('debug',$argarray['debug']); |
||
| 157 | //extract($argarray); |
||
| 158 | if ($argarray['help']) |
||
| 159 | return array($this->helpImage(), ' '); // FIXME |
||
| 160 | $this->createColors(); |
||
| 161 | $this->extract_wikipages($dbi, $argarray); |
||
| 162 | /* ($dbi, $large, $recent, $refined, $backlink, |
||
| 163 | $neighbour, $excludelist, $includelist, $color); */ |
||
| 164 | return $this->invokeDot($argarray); |
||
| 165 | /* => ($width, $height, $color, $shape, $text); */ |
||
| 166 | |||
| 167 | } |
||
| 168 | |||
| 169 | // ------------------------------------------------------------------------------------------ |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns an image containing a usage description of the plugin. |
||
| 173 | * @return string image handle |
||
| 174 | */ |
||
| 175 | function helpImage() { |
||
| 176 | $def = $this->defaultarguments(); |
||
| 177 | $other_imgtypes = $GLOBALS['PLUGIN_CACHED_IMGTYPES']; |
||
| 178 | unset ($other_imgtypes[$def['imgtype']]); |
||
| 179 | $helparr = array( |
||
| 180 | '<?plugin '.$this->getName() . |
||
| 181 | ' img' => ' = "' . $def['imgtype'] . "(default)|" . join('|',$GLOBALS['PLUGIN_CACHED_IMGTYPES']).'"', |
||
| 182 | 'width' => ' = "width in inches"', |
||
| 183 | 'height' => ' = "height in inches"', |
||
| 184 | 'fontname' => ' = "font family"', |
||
| 185 | 'fontsize' => ' = "fontsize in points"', |
||
| 186 | 'colorby' => ' = "age|revtime|none"', |
||
| 187 | 'fillnodes' => ' = "on|off"', |
||
| 188 | 'shape' => ' = "ellipse(default)|box|circle|point"', |
||
| 189 | 'label' => ' = "name|number"', |
||
| 190 | 'large_nb' => ' = "number of largest pages to be selected"', |
||
| 191 | 'recent_nb' => ' = "number of youngest pages"', |
||
| 192 | 'refined_nb' => ' = "#pages with smallest time between revisions"', |
||
| 193 | 'backlink_nb' => ' = "number of pages with most backlinks"', |
||
| 194 | 'neighbour_list' => ' = "find pages linked from and to these pages"', |
||
| 195 | 'exclude_list' => ' = "colon separated list of pages to be excluded"', |
||
| 196 | 'include_list' => ' = "colon separated list" ?>' |
||
| 197 | ); |
||
| 198 | $length = 0; |
||
| 199 | foreach($helparr as $alignright => $alignleft) { |
||
| 200 | $length = max($length, strlen($alignright)); |
||
| 201 | } |
||
| 202 | $helptext =''; |
||
| 203 | foreach($helparr as $alignright => $alignleft) { |
||
| 204 | $helptext .= substr(' ' |
||
| 205 | . $alignright, -$length).$alignleft."\n"; |
||
| 206 | } |
||
| 207 | return $this->text2img($helptext, 4, array(1, 0, 0), |
||
| 208 | array(255, 255, 255)); |
||
| 209 | } |
||
| 210 | |||
| 211 | |||
| 212 | /** |
||
| 213 | * Selects the first (smallest or biggest) WikiPages in |
||
| 214 | * a given category. |
||
| 215 | * |
||
| 216 | * @param number integer number of page names to be found |
||
| 217 | * @param category string attribute of the pages which is used |
||
| 218 | * to compare them |
||
| 219 | * @param minimum boolean true finds smallest, false finds biggest |
||
| 220 | * @return array list of page names found to be the best |
||
| 221 | */ |
||
| 222 | function findbest($number, $category, $minimum ) { |
||
| 223 | // select the $number best in the category '$category' |
||
| 224 | $pages = &$this->pages; |
||
| 225 | $names = &$this->names; |
||
| 226 | |||
| 227 | $selected = array(); |
||
| 228 | $i = 0; |
||
| 229 | foreach($names as $name) { |
||
| 230 | if ($i++>=$number) |
||
| 231 | break; |
||
| 232 | $selected[$name] = $pages[$name][$category]; |
||
| 233 | } |
||
| 234 | //echo "<pre>$category "; var_dump($selected); "</pre>"; |
||
| 235 | $compareto = $minimum ? 0x79999999 : -0x79999999; |
||
| 236 | |||
| 237 | $i = 0; |
||
| 238 | foreach ($names as $name) { |
||
| 239 | if ($i++<$number) |
||
| 240 | continue; |
||
| 241 | if ($minimum) { |
||
| 242 | if (($crit = $pages[$name][$category]) < $compareto) { |
||
| 243 | $selected[$name] = $crit; |
||
| 244 | asort($selected, SORT_NUMERIC); |
||
| 245 | array_pop($selected); |
||
| 246 | $compareto = end($selected); |
||
| 247 | } |
||
| 248 | } elseif (($crit = $pages[$name][$category]) > $compareto) { |
||
| 249 | $selected[$name] = $crit; |
||
| 250 | arsort($selected, SORT_NUMERIC); |
||
| 251 | array_pop($selected); |
||
| 252 | $compareto = end($selected); |
||
| 253 | } |
||
| 254 | } |
||
| 255 | //echo "<pre>$category "; var_dump($selected); "</pre>"; |
||
| 256 | |||
| 257 | return array_keys($selected); |
||
| 258 | } |
||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * Extracts a subset of all pages from the wiki and find their |
||
| 263 | * connections to other pages. Also collects some page features |
||
| 264 | * like size, age, revision number which are used to find the |
||
| 265 | * most attractive pages. |
||
| 266 | * |
||
| 267 | * @param dbi WikiDB database handle to access all Wiki pages |
||
| 268 | * @param LARGE integer number of largest pages which should |
||
| 269 | * be included |
||
| 270 | * @param RECENT integer number of the youngest pages to be included |
||
| 271 | * @param REFINED integer number of the pages with shortes revision |
||
| 272 | * interval |
||
| 273 | * @param BACKLINK integer number of the pages with most backlinks |
||
| 274 | * @param EXCLUDELIST string colon ':' separated list of page names which |
||
| 275 | * should not be displayed (like PhpWiki, for |
||
| 276 | * example) |
||
| 277 | * @param INCLUDELIST string colon separated list of pages which are |
||
| 278 | * always included (for example your own |
||
| 279 | * page :) |
||
| 280 | * @param COLOR string 'age', 'revtime' or 'none'; Selects which |
||
| 281 | * page feature is used to determine the |
||
| 282 | * filling color of the nodes in the graph. |
||
| 283 | * @return void |
||
| 284 | */ |
||
| 285 | function extract_wikipages($dbi, $argarray) { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Creates the text file description of the graph needed to invoke |
||
| 408 | * <code>dot</code>. |
||
| 409 | * |
||
| 410 | * @param filename string name of the dot file to be created |
||
| 411 | * @param width float width of the output graph in inches |
||
| 412 | * @param height float height of the graph in inches |
||
| 413 | * @param colorby string color sceme beeing used ('age', 'revtime', |
||
| 414 | * 'none') |
||
| 415 | * @param shape string node shape; 'ellipse', 'box', 'circle', 'point' |
||
| 416 | * @param label string 'name': label by name, |
||
| 417 | * 'number': label by unique number |
||
| 418 | * @return boolean error status; true=ok; false=error |
||
| 419 | */ |
||
| 420 | function createDotFile($filename, $argarray) { |
||
| 519 | |||
| 520 | |||
| 521 | /** |
||
| 522 | * static workaround on broken Cache or broken dot executable, |
||
| 523 | * called only if debug=static. |
||
| 524 | * |
||
| 525 | * @access private |
||
| 526 | * @param url string url pointing to the image part of the map |
||
| 527 | * @param map string <area> tags defining active |
||
| 528 | * regions in the map |
||
| 529 | * @param dbi WikiDB database abstraction class |
||
| 530 | * @param argarray array complete (!) arguments to produce |
||
| 531 | * image. It is not necessary to call |
||
| 532 | * WikiPlugin->getArgs anymore. |
||
| 533 | * @param request Request ??? |
||
| 534 | * @return string html output |
||
| 535 | */ |
||
| 536 | function embedImg($url,&$dbi,$argarray,&$request) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Prepares some rainbow colors for the nodes of the graph |
||
| 559 | * and stores them in an array which may be accessed with |
||
| 560 | * <code>getColor</code>. |
||
| 561 | */ |
||
| 562 | function createColors() { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Translates a value from 0.0 to 1.0 into rainbow color. |
||
| 595 | * red -> orange -> green -> blue -> gray |
||
| 596 | * |
||
| 597 | * @param promille float value between 0.0 and 1.0 |
||
| 598 | * @return array(red,green,blue) |
||
| 599 | */ |
||
| 600 | function getColor($promille) { |
||
| 608 | } |
||
| 609 | |||
| 697 |
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.