Complex classes like SRFTimeline 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 SRFTimeline, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class SRFTimeline extends SMWResultPrinter { |
||
| 19 | |||
| 20 | protected $m_tlstart = ''; // name of the start-date property if any |
||
| 21 | protected $m_tlend = ''; // name of the end-date property if any |
||
| 22 | protected $m_tlsize = ''; // CSS-compatible size (such as 400px) |
||
| 23 | protected $m_tlbands = ''; // array of band IDs (MONTH, YEAR, ...) |
||
| 24 | protected $m_tlpos = ''; // position identifier (start, end, today, middle) |
||
| 25 | protected $mTemplate; |
||
| 26 | protected $mNamedArgs; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @see SMWResultPrinter::handleParameters |
||
| 30 | * |
||
| 31 | * @since 1.6.3 |
||
| 32 | * |
||
| 33 | * @param array $params |
||
| 34 | * @param $outputmode |
||
| 35 | */ |
||
| 36 | 1 | protected function handleParameters( array $params, $outputmode ) { |
|
| 50 | |||
| 51 | public function getName() { |
||
| 56 | |||
| 57 | 1 | protected function getResultText( SMWQueryResult $res, $outputmode ) { |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Returns the HTML for the events. |
||
| 108 | * |
||
| 109 | * @since 1.5.3 |
||
| 110 | * |
||
| 111 | * @param SMWQueryResult $res |
||
| 112 | * @param $outputmode |
||
| 113 | * @param boolean $isEventline |
||
| 114 | * |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | 1 | protected function getEventsHTML( SMWQueryResult $res, $outputmode, $isEventline ) { |
|
| 118 | 1 | global $curarticle, $cururl; // why not, code flow has reached max insanity already |
|
| 119 | |||
| 120 | 1 | $positions = []; // possible positions, collected to select one for centering |
|
| 121 | 1 | $curcolor = 0; // color cycling is used for eventline |
|
| 122 | |||
| 123 | 1 | $result = ''; |
|
| 124 | |||
| 125 | 1 | $output = false; // true if output for the popup was given on current line |
|
| 126 | 1 | if ( $isEventline ) { |
|
| 127 | $events = []; |
||
| 128 | } // array of events that are to be printed |
||
| 129 | |||
| 130 | 1 | while ( $row = $res->getNext() ) { // Loop over the objcts (pages) |
|
| 131 | 1 | $hastime = false; // true as soon as some startdate value was found |
|
| 132 | 1 | $hastitle = false; // true as soon as some label for the event was found |
|
| 133 | 1 | $curdata = ''; // current *inner* print data (within some event span) |
|
| 134 | 1 | $curmeta = ''; // current event meta data |
|
| 135 | 1 | $cururl = ''; |
|
| 136 | 1 | $curarticle = ''; // label of current article, if it was found; needed only for eventline labeling |
|
| 137 | 1 | $first_col = true; |
|
| 138 | |||
| 139 | 1 | if ( $this->mTemplate != '' ) { |
|
| 140 | $this->hasTemplates = true; |
||
| 141 | $template_text = ''; |
||
| 142 | $i = 0; |
||
| 143 | } |
||
| 144 | |||
| 145 | 1 | foreach ( $row as $field ) { // Loop over the returned properties |
|
| 146 | 1 | $first_value = true; |
|
| 147 | 1 | $pr = $field->getPrintRequest(); |
|
| 148 | 1 | $dataValue = $pr->getData(); |
|
| 149 | |||
| 150 | 1 | if ( $dataValue == '' ) { |
|
| 151 | 1 | $date_value = null; |
|
| 152 | } else { |
||
| 153 | 1 | $date_value = $dataValue->getDataItem()->getLabel(); |
|
| 154 | } |
||
| 155 | |||
| 156 | 1 | while ( ( $object = $field->getNextDataValue() ) !== false ) { // Loop over property values |
|
| 157 | 1 | $event = $this->handlePropertyValue( |
|
| 158 | 1 | $object, |
|
| 159 | $outputmode, |
||
| 160 | $pr, |
||
| 161 | $first_col, |
||
| 162 | $hastitle, |
||
| 163 | $hastime, |
||
| 164 | $first_value, |
||
| 165 | $isEventline, |
||
| 166 | $curmeta, |
||
| 167 | $curdata, |
||
| 168 | $date_value, |
||
| 169 | $output, |
||
| 170 | $positions |
||
| 171 | ); |
||
| 172 | |||
| 173 | 1 | if ( $this->mTemplate != '' ) { |
|
| 174 | $template_text .= '|' . ( $this->mNamedArgs ? '?' . $field->getPrintRequest()->getLabel( |
||
| 175 | ) : $i + 1 ) . '='; |
||
| 176 | if ( !$first_value ) { |
||
| 177 | $template_text .= ', '; |
||
| 178 | } |
||
| 179 | $template_text .= $object->getShortText( SMW_OUTPUT_WIKI, $this->getLinker( $first_value ) ); |
||
| 180 | $i++; |
||
| 181 | } |
||
| 182 | |||
| 183 | 1 | if ( $event !== false ) { |
|
| 184 | $events[] = $event; |
||
| 185 | } |
||
| 186 | |||
| 187 | 1 | $first_value = false; |
|
| 188 | } |
||
| 189 | |||
| 190 | 1 | if ( $output ) { |
|
| 191 | 1 | $curdata .= '<br />'; |
|
| 192 | } |
||
| 193 | |||
| 194 | 1 | $output = false; |
|
| 195 | 1 | $first_col = false; |
|
| 196 | } |
||
| 197 | |||
| 198 | 1 | if ( $this->mTemplate != '' ) { |
|
| 199 | $curdata = '{{' . $this->mTemplate . $template_text . '}}'; |
||
| 200 | } |
||
| 201 | |||
| 202 | 1 | if ( $hastime ) { |
|
| 203 | 1 | $result .= Html::rawElement( |
|
| 204 | 1 | 'span', |
|
| 205 | 1 | [ 'class' => 'smwtlevent', 'style' => 'display:none;' ], |
|
| 206 | 1 | $curmeta . Html::element( |
|
| 207 | 1 | 'span', |
|
| 208 | 1 | [ 'class' => 'smwtlcoloricon' ], |
|
| 209 | 1 | $curcolor |
|
| 210 | 1 | ) . $curdata |
|
| 211 | ); |
||
| 212 | } |
||
| 213 | |||
| 214 | 1 | if ( $isEventline ) { |
|
| 215 | foreach ( $events as $event ) { |
||
| 216 | $result .= '<span class="smwtlevent" style="display:none;" ><span class="smwtlstart">' . $event[0] . '</span><span class="smwtlurl">' . $curarticle . '</span><span class="smwtlcoloricon">' . $curcolor . '</span>'; |
||
| 217 | if ( $curarticle != '' ) { |
||
| 218 | $result .= '<span class="smwtlprefix">' . $curarticle . ' </span>'; |
||
| 219 | } |
||
| 220 | $result .= $curdata . '</span>'; |
||
| 221 | $positions[$event[2]] = $event[0]; |
||
| 222 | } |
||
| 223 | $events = []; |
||
| 224 | $curcolor = ( $curcolor + 1 ) % 10; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | 1 | if ( count( $positions ) > 0 ) { |
|
| 229 | 1 | ksort( $positions ); |
|
| 230 | 1 | $positions = array_values( $positions ); |
|
| 231 | |||
| 232 | 1 | switch ( $this->m_tlpos ) { |
|
| 233 | 1 | case 'start': |
|
| 234 | $result .= '<span class="smwtlposition" style="display:none;" >' . $positions[0] . '</span>'; |
||
| 235 | break; |
||
| 236 | 1 | case 'end': |
|
| 237 | $result .= '<span class="smwtlposition" style="display:none;" >' . $positions[count( |
||
| 238 | $positions |
||
| 239 | ) - 1] . '</span>'; |
||
| 240 | break; |
||
| 241 | 1 | case 'today': |
|
| 242 | break; // default |
||
| 243 | 1 | case 'middle': |
|
| 244 | default: |
||
| 245 | 1 | $result .= '<span class="smwtlposition" style="display:none;" >' . $positions[ceil( |
|
| 246 | 1 | count( $positions ) / 2 |
|
| 247 | 1 | ) - 1] . '</span>'; |
|
| 248 | 1 | break; |
|
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | 1 | return $result; |
|
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Hanldes a single property value. Returns an array with data for a single event or false. |
||
| 257 | * |
||
| 258 | * FIXME: 13 arguments, of which a whole bunch are byref... not a good design :) |
||
| 259 | * |
||
| 260 | * @since 1.5.3 |
||
| 261 | * |
||
| 262 | * @param SMWDataValue $object |
||
| 263 | * @param $outputmode |
||
| 264 | * @param SMWPrintRequest $pr |
||
| 265 | * @param boolean $first_col |
||
| 266 | * @param boolean &$hastitle |
||
| 267 | * @param boolean &$hastime |
||
| 268 | * @param boolean $first_value |
||
| 269 | * @param boolean $isEventline |
||
| 270 | * @param string &$curmeta |
||
| 271 | * @param string &$curdata |
||
| 272 | * @param &$date_value |
||
| 273 | * @param boolean &$output |
||
| 274 | * @param array &$positions |
||
| 275 | * |
||
| 276 | * @return false or array |
||
| 277 | */ |
||
| 278 | 1 | protected function handlePropertyValue( SMWDataValue $object, $outputmode, SMWPrintRequest $pr, $first_col, |
|
| 372 | |||
| 373 | /** |
||
| 374 | * @see SMWResultPrinter::getParamDefinitions |
||
| 375 | * |
||
| 376 | * @since 1.8 |
||
| 377 | * |
||
| 378 | * @param $definitions array of IParamDefinition |
||
| 379 | * |
||
| 380 | * @return array of IParamDefinition|array |
||
| 381 | */ |
||
| 382 | 1 | public function getParamDefinitions( array $definitions ) { |
|
| 426 | |||
| 427 | } |
||
| 428 |