| Conditions | 24 |
| Paths | 87 |
| Total Lines | 115 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 178 | protected function getResultText( SMWQueryResult $queryResult, $outputmode ) {
|
||
| 179 | |||
| 180 | // Show warning if Extension:Mermaid is not available |
||
| 181 | if ( !class_exists( 'Mermaid' ) && !class_exists( 'Mermaid\\MermaidParserFunction' ) ) {
|
||
| 182 | //wfWarn( 'The SRF Mermaid format needs the Mermaid extension to be installed.' ); |
||
| 183 | $queryResult->addErrors( [wfMessage('')->text()] );
|
||
| 184 | return ''; |
||
| 185 | } |
||
| 186 | |||
| 187 | // Load general Modules |
||
| 188 | // First load the dependent modules of Mermaid ext |
||
| 189 | SMWOutputs::requireResource( 'ext.mermaid' ); |
||
| 190 | SMWOutputs::requireResource( 'ext.mermaid.styles' ); |
||
| 191 | SMWOutputs::requireResource( 'ext.srf.gantt' ); |
||
| 192 | |||
| 193 | //Add Tasks & Sections |
||
| 194 | while ( $row = $queryResult->getNext() ) {
|
||
| 195 | |||
| 196 | $status = []; |
||
| 197 | $priority = []; |
||
| 198 | $startDate = ''; |
||
| 199 | $endDate = ''; |
||
| 200 | $taskID = ''; |
||
| 201 | $taskTitle = ''; |
||
| 202 | $sections = []; |
||
| 203 | |||
| 204 | // Loop through all field of a row |
||
| 205 | foreach ( $row as $field ) {
|
||
| 206 | |||
| 207 | $fieldLabel = $field->getPrintRequest()->getLabel(); |
||
| 208 | |||
| 209 | //get values |
||
| 210 | foreach ( $field->getContent() as $dataItem ) {
|
||
| 211 | |||
| 212 | switch ( $fieldLabel ) {
|
||
| 213 | case 'section': |
||
| 214 | $sections[$dataItem->getTitle()->getPrefixedDBKey()] = $dataItem->getSortKey(); |
||
| 215 | break; |
||
| 216 | case 'task': |
||
| 217 | if ( $dataItem instanceof SMWDIBlob ) {
|
||
| 218 | $taskTitle = $dataItem->getString(); |
||
| 219 | $taskID = $field->getResultSubject()->getTitle()->getPrefixedDBKey(); |
||
| 220 | } |
||
| 221 | break; |
||
| 222 | case 'startdate': |
||
| 223 | if ( $dataItem instanceof SMWDITime ) {
|
||
| 224 | $startDate = $dataItem->getMwTimestamp(); |
||
| 225 | } |
||
| 226 | break; |
||
| 227 | case 'enddate': |
||
| 228 | if ( $dataItem instanceof SMWDITime ) {
|
||
| 229 | $endDate = $dataItem->getMwTimestamp(); |
||
| 230 | } |
||
| 231 | break; |
||
| 232 | case 'status': |
||
| 233 | if ( $dataItem instanceof SMWDIBlob ) {
|
||
| 234 | $status[] = $dataItem->getString(); |
||
| 235 | } |
||
| 236 | break; |
||
| 237 | case 'priority': |
||
| 238 | if ( $dataItem instanceof SMWDIBlob ) {
|
||
| 239 | $priority[] = $dataItem->getString(); |
||
| 240 | } |
||
| 241 | break; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | // Add section/Task |
||
| 247 | // Title, TaskID, StartDate and EndDate are required |
||
| 248 | if ( $taskID !== '' && $taskTitle !== '' && $startDate !== '' && $endDate !== '' ) {
|
||
| 249 | $this->mGantt->addTask( $taskID, $taskTitle, $status, $priority, $startDate, $endDate ); |
||
| 250 | |||
| 251 | // If no section was found, put task into a dummy section object |
||
| 252 | // "gantt-no-section#21780240" is used to identify Tasks that with no section (dummy section) |
||
| 253 | if ( count( $sections ) == 0 ) {
|
||
| 254 | $this->mGantt->addSection( 'gantt-no-section#21780240', '', $startDate, $endDate, $taskID ); |
||
| 255 | } else {
|
||
| 256 | foreach ( $sections as $sectionID => $sectionTitle ) {
|
||
| 257 | $this->mGantt->addSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID ); |
||
| 258 | } |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | // Improve unique id by adding a random number |
||
| 264 | $id = uniqid( 'srf-gantt-' . rand( 1, 10000 ) ); |
||
| 265 | |||
| 266 | // Add gantt configurations |
||
| 267 | $config = [ |
||
| 268 | 'theme' => $this->params['theme'], |
||
| 269 | 'gantt' => [ |
||
| 270 | 'leftPadding' => intval( $this->params['leftpadding'] ), |
||
| 271 | 'titleTopMargin' => intval( $this->params['titletopmargin'] ), |
||
| 272 | 'barHeight' => intval( $this->params['barheight'] ), |
||
| 273 | 'barGap' => intval( $this->params['bargap'] ) |
||
| 274 | ] |
||
| 275 | ]; |
||
| 276 | |||
| 277 | // Manage Output |
||
| 278 | if ( !empty( $this->mErrors ) ) {
|
||
| 279 | return $queryResult->addErrors( $this->mErrors ); |
||
| 280 | } else {
|
||
| 281 | return Html::rawElement( 'div', [ |
||
| 282 | 'id' => $id, |
||
| 283 | 'class' => 'srf-gantt', |
||
| 284 | 'data-mermaid' => json_encode( [ |
||
| 285 | 'content' => $this->mGantt->getGanttOutput(), |
||
| 286 | 'config' => $config |
||
| 287 | ], JSON_UNESCAPED_UNICODE ) |
||
| 288 | ], Html::rawElement( 'div', [ |
||
| 289 | 'class' => 'mermaid-dots', |
||
| 290 | ] ) ); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 297 | } |