Complex classes like TreeResultPrinter 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 TreeResultPrinter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class TreeResultPrinter extends ListResultPrinter { |
||
28 | |||
29 | private $standardTemplateParameters; |
||
30 | |||
31 | /** |
||
32 | * @var SMWQueryResult | null |
||
33 | */ |
||
34 | private $queryResult = null; |
||
35 | |||
36 | /** |
||
37 | * (non-PHPdoc) |
||
38 | * @see SMWResultPrinter::getName() |
||
39 | */ |
||
40 | public function getName() { |
||
45 | |||
46 | /** |
||
47 | * @return SMWQueryResult |
||
48 | * @throws Exception |
||
49 | */ |
||
50 | 5 | public function getQueryResult() { |
|
58 | |||
59 | /** |
||
60 | * @param SMWQueryResult | null $queryResult |
||
61 | */ |
||
62 | 7 | public function setQueryResult( $queryResult ) { |
|
65 | |||
66 | /** |
||
67 | * @see ResultPrinter::postProcessParameters() |
||
68 | */ |
||
69 | 8 | protected function postProcessParameters() { |
|
84 | |||
85 | /** |
||
86 | * Return serialised results in specified format. |
||
87 | * |
||
88 | * @param SMWQueryResult $queryResult |
||
89 | * @param $outputmode |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | 7 | protected function getResultText( SMWQueryResult $queryResult, $outputmode ) { |
|
94 | |||
95 | 7 | $this->setQueryResult( $queryResult ); |
|
96 | |||
97 | 7 | if ( $this->params['parent'] === '' ) { |
|
98 | 2 | $this->addError( 'srf-tree-noparentprop' ); |
|
99 | 2 | return ''; |
|
100 | } |
||
101 | |||
102 | 5 | $rootHash = $this->getRootHash(); |
|
103 | |||
104 | 5 | if ( $rootHash === false ) { |
|
105 | $this->addError( 'srf-tree-rootinvalid', $this->params['root'] ); |
||
106 | return ''; |
||
107 | } |
||
108 | |||
109 | 5 | $this->hasTemplates = |
|
110 | 5 | $this->params['introtemplate'] !== '' || |
|
111 | 5 | $this->params['outrotemplate'] !== '' || |
|
112 | 5 | $this->params['template'] !== ''; |
|
113 | |||
114 | 5 | if ( $this->hasTemplates ) { |
|
115 | 3 | $this->initalizeStandardTemplateParameters(); |
|
116 | } |
||
117 | |||
118 | 5 | $tree = $this->buildTreeFromQueryResult( $rootHash ); |
|
119 | 5 | $lines = $this->buildLinesFromTree( $tree ); |
|
120 | |||
121 | // Display default if the result is empty |
||
122 | 5 | if ( count( $lines ) === 0 ) { |
|
123 | return $this->params['default']; |
||
124 | } |
||
125 | |||
126 | // FIXME: Linking to further events ($this->linkFurtherResults()) |
||
127 | // does not make sense for tree format. But maybe display a warning? |
||
128 | |||
129 | 5 | $resultText = join( |
|
130 | 5 | "\n", |
|
131 | 5 | array_merge( |
|
132 | 5 | [ $this->getTemplateCall( $this->params['introtemplate'] ) ], |
|
133 | 5 | $lines, |
|
134 | 5 | [ $this->getTemplateCall( $this->params['outrotemplate'] ) ] |
|
135 | ) |
||
136 | ); |
||
137 | |||
138 | 5 | $this->setQueryResult( null ); |
|
139 | |||
140 | 5 | return $this->params[ 'class' ] ? Html::rawElement( 'div', [ 'class' => $this->params[ 'class' ] ], $resultText ) : $resultText; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param string $templateName |
||
145 | * @param string[] $params |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | 5 | public function getTemplateCall( $templateName, $params = [] ) { |
|
157 | |||
158 | /** |
||
159 | * @see SMWResultPrinter::getParamDefinitions |
||
160 | * |
||
161 | * @since 1.8 |
||
162 | * |
||
163 | * @param $definitions array of IParamDefinition |
||
164 | * |
||
165 | * @return array of IParamDefinition|array |
||
166 | * @throws Exception |
||
167 | */ |
||
168 | 8 | public function getParamDefinitions( array $definitions ) { |
|
204 | |||
205 | /** |
||
206 | * @param string $rootHash |
||
207 | * |
||
208 | * @return TreeNode |
||
209 | */ |
||
210 | 5 | protected function buildTreeFromQueryResult( $rootHash ) { |
|
220 | |||
221 | /** |
||
222 | * @return string | false |
||
223 | */ |
||
224 | 5 | protected function getRootHash() { |
|
240 | |||
241 | /** |
||
242 | * @return TreeNode[] |
||
243 | */ |
||
244 | 5 | protected function getHashOfNodes() { |
|
260 | |||
261 | /** |
||
262 | * Returns a linker object for making hyperlinks |
||
263 | * |
||
264 | * @return \Linker |
||
265 | */ |
||
266 | 1 | public function getLinker( $firstcol = false ) { |
|
269 | |||
270 | /** |
||
271 | * Depending on current linking settings, returns a linker object |
||
272 | * for making hyperlinks or NULL if no links should be created. |
||
273 | * |
||
274 | * @param int $column Column number |
||
275 | * |
||
276 | * @return \Linker|null |
||
277 | */ |
||
278 | 5 | public function getLinkerForColumn( $column ) { |
|
281 | |||
282 | 3 | private function initalizeStandardTemplateParameters() { |
|
294 | |||
295 | /** |
||
296 | * @param string $rootHash |
||
297 | * @param TreeNode[] $nodes |
||
298 | * |
||
299 | * @return TreeNode |
||
300 | * @throws \Exception |
||
301 | */ |
||
302 | 5 | protected function buildTreeFromNodeList( $rootHash, $nodes ) { |
|
346 | |||
347 | /** |
||
348 | * @param TreeNode $tree |
||
349 | * |
||
350 | * @return mixed |
||
351 | */ |
||
352 | 5 | protected function buildLinesFromTree( $tree ) { |
|
365 | |||
366 | /** |
||
367 | * @param string $msgkey |
||
368 | * @param string | string[] $params |
||
369 | */ |
||
370 | 3 | protected function addError( $msgkey, $params = [] ) { |
|
378 | |||
379 | } |
||
380 | |||
381 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.