Complex classes like syntax_plugin_info 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 syntax_plugin_info, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class syntax_plugin_info extends DokuWiki_Syntax_Plugin |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * What kind of syntax are we? |
||
14 | */ |
||
15 | public function getType() |
||
19 | |||
20 | /** |
||
21 | * What about paragraphs? |
||
22 | */ |
||
23 | public function getPType() |
||
27 | |||
28 | /** |
||
29 | * Where to sort in? |
||
30 | */ |
||
31 | public function getSort() |
||
35 | |||
36 | |||
37 | /** |
||
38 | * Connect pattern to lexer |
||
39 | */ |
||
40 | public function connectTo($mode) |
||
44 | |||
45 | /** |
||
46 | * Handle the match |
||
47 | * |
||
48 | * @param string $match The text matched by the patterns |
||
49 | * @param int $state The lexer state for the match |
||
50 | * @param int $pos The character position of the matched text |
||
51 | * @param Doku_Handler $handler The Doku_Handler object |
||
52 | * @return array Return an array with all data you want to use in render |
||
53 | */ |
||
54 | public function handle($match, $state, $pos, Doku_Handler $handler) |
||
59 | |||
60 | /** |
||
61 | * Create output |
||
62 | * |
||
63 | * @param string $format string output format being rendered |
||
64 | * @param Doku_Renderer $renderer the current renderer object |
||
65 | * @param array $data data created by handler() |
||
66 | * @return boolean rendered correctly? |
||
67 | */ |
||
68 | public function render($format, Doku_Renderer $renderer, $data) |
||
111 | |||
112 | /** |
||
113 | * list all installed plugins |
||
114 | * |
||
115 | * uses some of the original renderer methods |
||
116 | * |
||
117 | * @param string $type |
||
118 | * @param Doku_Renderer_xhtml $renderer |
||
119 | */ |
||
120 | protected function renderPlugins($type, Doku_Renderer_xhtml $renderer) |
||
153 | |||
154 | /** |
||
155 | * list all installed plugins |
||
156 | * |
||
157 | * uses some of the original renderer methods |
||
158 | * |
||
159 | * @param Doku_Renderer_xhtml $renderer |
||
160 | */ |
||
161 | protected function renderHelperMethods(Doku_Renderer_xhtml $renderer) |
||
206 | |||
207 | /** |
||
208 | * lists all known syntax types and their registered modes |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | protected function renderSyntaxTypes() |
||
231 | |||
232 | /** |
||
233 | * lists all known syntax modes and their sorting value |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | protected function renderSyntaxModes() |
||
272 | |||
273 | /** |
||
274 | * Adds a TOC item |
||
275 | * |
||
276 | * @param string $text |
||
277 | * @param int $level |
||
278 | * @param Doku_Renderer_xhtml $renderer |
||
279 | * @return string |
||
280 | */ |
||
281 | protected function addToToc($text, $level, Doku_Renderer_xhtml $renderer) |
||
297 | } |
||
298 | |||
300 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.