| Conditions | 24 |
| Paths | 87 |
| Total Lines | 117 |
| 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 |
||
| 192 | protected function getResultText( SMWQueryResult $queryResult, $outputmode ) {
|
||
| 193 | |||
| 194 | // Show warning if Extension:Mermaid is not available |
||
| 195 | if ( !class_exists( 'Mermaid' ) && !class_exists( 'Mermaid\\MermaidParserFunction' ) ) {
|
||
| 196 | //wfWarn( 'The SRF Mermaid format needs the Mermaid extension to be installed.' ); |
||
| 197 | //catch ( Exception $exception ) {
|
||
| 198 | $queryResult->addErrors( ["Error: Mermaid Extension needs to be installed."] ); |
||
| 199 | //} |
||
| 200 | return ''; |
||
| 201 | } |
||
| 202 | |||
| 203 | // Load general Modules |
||
| 204 | // First load the dependent modules of Mermaid ext |
||
| 205 | SMWOutputs::requireResource( 'ext.mermaid' ); |
||
| 206 | SMWOutputs::requireResource( 'ext.mermaid.styles' ); |
||
| 207 | SMWOutputs::requireResource( 'ext.srf.gantt' ); |
||
| 208 | |||
| 209 | //Add Tasks & Sections |
||
| 210 | while ( $row = $queryResult->getNext() ) {
|
||
| 211 | |||
| 212 | $status = []; |
||
| 213 | $priority = []; |
||
| 214 | $startDate = ""; |
||
| 215 | $endDate = ""; |
||
| 216 | $taskID = ""; |
||
| 217 | $taskTitle = ""; |
||
| 218 | $sections = []; |
||
| 219 | |||
| 220 | // Loop through all field of a row |
||
| 221 | foreach ( $row as $field ) {
|
||
| 222 | |||
| 223 | $fieldLabel = $field->getPrintRequest()->getLabel(); |
||
| 224 | |||
| 225 | //get values |
||
| 226 | foreach ( $field->getContent() as $dataItem ) {
|
||
| 227 | |||
| 228 | switch ( $fieldLabel ) {
|
||
| 229 | case "section": |
||
| 230 | $sections[$dataItem->getTitle()->getPrefixedDBKey()] = $dataItem->getSortKey(); |
||
| 231 | break; |
||
| 232 | case "task": |
||
| 233 | if ( $dataItem instanceof SMWDIBlob ) {
|
||
| 234 | $taskTitle = $dataItem->getString(); |
||
| 235 | $taskID = $field->getResultSubject()->getTitle()->getPrefixedDBKey(); |
||
| 236 | } |
||
| 237 | break; |
||
| 238 | case "startdate": |
||
| 239 | if ( $dataItem instanceof SMWDITime ) {
|
||
| 240 | $startDate = $dataItem->getMwTimestamp(); |
||
| 241 | } |
||
| 242 | break; |
||
| 243 | case "enddate": |
||
| 244 | if ( $dataItem instanceof SMWDITime ) {
|
||
| 245 | $endDate = $dataItem->getMwTimestamp(); |
||
| 246 | } |
||
| 247 | break; |
||
| 248 | case "status": |
||
| 249 | if ( $dataItem instanceof SMWDIBlob ) {
|
||
| 250 | array_push( $status, $dataItem->getString() ); |
||
| 251 | } |
||
| 252 | break; |
||
| 253 | case "priority": |
||
| 254 | if ( $dataItem instanceof SMWDIBlob ) {
|
||
| 255 | array_push( $priority, $dataItem->getString() ); |
||
| 256 | } |
||
| 257 | break; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | // Add section/Task |
||
| 263 | // Title, TaskID, StartDate and EndDate are required |
||
| 264 | if ( $taskID != "" && $taskTitle != "" && $startDate != "" && $endDate != "" ) {
|
||
| 265 | $this->mGantt->addTask( $taskID, $taskTitle, $status, $priority, $startDate, $endDate ); |
||
| 266 | |||
| 267 | // If no section was found, put task into a dummy section object |
||
| 268 | // "gantt-no-section#21780240" is used to identify Tasks that with no section (dummy section) |
||
| 269 | if ( count( $sections ) == 0 ) {
|
||
| 270 | $this->mGantt->addSection( "gantt-no-section#21780240", "", $startDate, $endDate, $taskID ); |
||
| 271 | } else {
|
||
| 272 | foreach ( $sections as $sectionID => $sectionTitle ) {
|
||
| 273 | $this->mGantt->addSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID ); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | // Improve unique id by adding a random number |
||
| 280 | $id = uniqid( 'srf-gantt-' . rand( 1, 10000 ) ); |
||
| 281 | |||
| 282 | // Add gantt configurations |
||
| 283 | $config = [ |
||
| 284 | 'theme' => $this->params['theme'], |
||
| 285 | 'gantt' => [ |
||
| 286 | 'leftPadding' => intval( $this->params['leftpadding'] ), |
||
| 287 | 'titleTopMargin' => intval( $this->params['titletopmargin'] ), |
||
| 288 | 'barHeight' => intval( $this->params['barheight'] ), |
||
| 289 | 'barGap' => intval( $this->params['bargap'] ) |
||
| 290 | ] |
||
| 291 | ]; |
||
| 292 | |||
| 293 | // Manage Output |
||
| 294 | if ( !empty( $this->mErrors ) ) {
|
||
| 295 | return $queryResult->addErrors( $this->mErrors ); |
||
| 296 | } else {
|
||
| 297 | return Html::rawElement( 'div', [ |
||
| 298 | 'id' => $id, |
||
| 299 | 'class' => 'srf-gantt', |
||
| 300 | 'data-mermaid' => json_encode( [ |
||
| 301 | 'content' => $this->mGantt->getGanttOutput(), |
||
| 302 | 'config' => $config |
||
| 303 | ], JSON_UNESCAPED_UNICODE ) |
||
| 304 | ], Html::rawElement( 'div', [ |
||
| 305 | 'class' => 'mermaid-dots', |
||
| 306 | ] ) ); |
||
| 307 | } |
||
| 308 | } |
||
| 309 | } |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.