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() { |
||
41 | // Give grep a chance to find the usages: |
||
42 | // srf-printername-tree, srf-printername-ultree, srf-printername-oltree |
||
43 | return \Message::newFromKey( 'srf-printername-' . $this->mFormat )->text(); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @return SMWQueryResult |
||
48 | * @throws Exception |
||
49 | */ |
||
50 | 5 | public function getQueryResult() { |
|
51 | |||
52 | 5 | if ( $this->queryResult === null ) { |
|
53 | throw new Exception( __METHOD__ . ' called outside of ' . __CLASS__ . '::getResultText().' ); |
||
54 | } |
||
55 | |||
56 | 5 | return $this->queryResult; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param SMWQueryResult | null $queryResult |
||
61 | */ |
||
62 | 7 | public function setQueryResult( $queryResult ) { |
|
63 | 7 | $this->queryResult = $queryResult; |
|
64 | 7 | } |
|
65 | |||
66 | /** |
||
67 | * @see ResultPrinter::postProcessParameters() |
||
68 | */ |
||
69 | 8 | protected function postProcessParameters() { |
|
70 | |||
71 | 8 | parent::postProcessParameters(); |
|
72 | |||
73 | // Don't support pagination in trees |
||
74 | 8 | $this->mSearchlabel = null; |
|
75 | |||
76 | // Allow "_" for encoding spaces, as documented |
||
77 | 8 | $this->params['sep'] = str_replace( '_', ' ', $this->params['sep'] ); |
|
78 | |||
79 | 8 | if ( !ctype_digit( strval( $this->params['start level'] ) ) || $this->params['start level'] < 1 ) { |
|
80 | $this->params['start level'] = 1; |
||
81 | } |
||
82 | |||
83 | 8 | } |
|
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 Html::rawElement( 'div', [ 'class' => 'srf-tree' ], $resultText ); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param string $templateName |
||
145 | * @param string[] $params |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | 5 | public function getTemplateCall( $templateName, $params = [] ) { |
|
150 | |||
151 | 5 | if ( $templateName === '' ) { |
|
152 | 5 | return ''; |
|
153 | } |
||
154 | |||
155 | 3 | return '{{' . $templateName . '|' . join( '|', $params ) . $this->standardTemplateParameters . '}}'; |
|
156 | } |
||
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 ) { |
|
169 | 8 | $params = parent::getParamDefinitions( $definitions ); |
|
170 | |||
171 | 8 | $params['parent'] = [ |
|
172 | 'default' => '', |
||
173 | 'message' => 'srf-paramdesc-tree-parent', |
||
174 | ]; |
||
175 | |||
176 | 8 | $params['root'] = [ |
|
177 | 'default' => '', |
||
178 | 'message' => 'srf-paramdesc-tree-root', |
||
179 | ]; |
||
180 | |||
181 | 8 | $params['start level'] = [ |
|
182 | 'default' => 1, |
||
183 | 'message' => 'srf-paramdesc-tree-startlevel', |
||
184 | 'type' => 'integer', |
||
185 | ]; |
||
186 | |||
187 | 8 | $params['sep'] = [ |
|
188 | 'default' => ', ', |
||
189 | 'message' => 'smw-paramdesc-sep', |
||
190 | ]; |
||
191 | |||
192 | 8 | $params['template arguments'] = [ |
|
193 | 'default' => '', |
||
194 | 'message' => 'smw-paramdesc-template-arguments', |
||
195 | ]; |
||
196 | |||
197 | 8 | return $params; |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | * @param string $rootHash |
||
202 | * |
||
203 | * @return TreeNode |
||
204 | */ |
||
205 | 5 | protected function buildTreeFromQueryResult( $rootHash ) { |
|
206 | |||
207 | 5 | $nodes = $this->getHashOfNodes(); |
|
208 | |||
209 | 5 | if ( $rootHash !== '' && !array_key_exists( $rootHash, $nodes ) ) { |
|
210 | return new TreeNode(); |
||
211 | } |
||
212 | |||
213 | 5 | return $this->buildTreeFromNodeList( $rootHash, $nodes ); |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return string | false |
||
218 | */ |
||
219 | 5 | protected function getRootHash() { |
|
220 | |||
221 | 5 | if ( $this->params['root'] === '' ) { |
|
222 | 5 | return ''; |
|
223 | } |
||
224 | |||
225 | // get the title object of the root page |
||
226 | 1 | $rootTitle = Title::newFromText( $this->params['root'] ); |
|
227 | |||
228 | 1 | if ( $rootTitle !== null ) { |
|
229 | 1 | return DIWikiPage::newFromTitle( $rootTitle )->getSerialization(); |
|
230 | } |
||
231 | |||
232 | return false; |
||
233 | |||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @return TreeNode[] |
||
238 | */ |
||
239 | 5 | protected function getHashOfNodes() { |
|
240 | |||
241 | /** @var TreeNode[] $nodes */ |
||
242 | 5 | $nodes = []; |
|
243 | |||
244 | 5 | $queryResult = $this->getQueryResult(); |
|
245 | |||
246 | 5 | $row = $queryResult->getNext(); |
|
247 | 5 | while ( $row !== false ) { |
|
248 | 5 | $node = new TreeNode( $row ); |
|
249 | 5 | $nodes[$node->getHash()] = $node; |
|
250 | 5 | $row = $queryResult->getNext(); |
|
251 | } |
||
252 | |||
253 | 5 | return $nodes; |
|
254 | } |
||
255 | |||
256 | /** |
||
257 | * Returns a linker object for making hyperlinks |
||
258 | * |
||
259 | * @return \Linker |
||
260 | */ |
||
261 | 1 | public function getLinker( $firstcol = false ) { |
|
264 | |||
265 | /** |
||
266 | * Depending on current linking settings, returns a linker object |
||
267 | * for making hyperlinks or NULL if no links should be created. |
||
268 | * |
||
269 | * @param int $column Column number |
||
270 | * |
||
271 | * @return \Linker|null |
||
272 | */ |
||
273 | 5 | public function getLinkerForColumn( $column ) { |
|
274 | 5 | return parent::getLinker( $column === 0 ); |
|
276 | |||
277 | 3 | private function initalizeStandardTemplateParameters() { |
|
289 | |||
290 | /** |
||
291 | * @param string $rootHash |
||
292 | * @param TreeNode[] $nodes |
||
293 | * |
||
294 | * @return TreeNode |
||
295 | * @throws \Exception |
||
296 | */ |
||
297 | 5 | protected function buildTreeFromNodeList( $rootHash, $nodes ) { |
|
341 | |||
342 | /** |
||
343 | * @param TreeNode $tree |
||
344 | * |
||
345 | * @return mixed |
||
346 | */ |
||
347 | 5 | protected function buildLinesFromTree( $tree ) { |
|
360 | |||
361 | /** |
||
362 | * @param string $msgkey |
||
363 | * @param string | string[] $params |
||
364 | */ |
||
365 | 3 | protected function addError( $msgkey, $params = [] ) { |
|
373 | |||
374 | } |
||
375 | |||
376 |
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.