| 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 |
||
| 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 | $queryResult->addErrors( ["Error: Mermaid Extension needs to be installed."] ); |
||
| 198 | return ''; |
||
| 199 | } |
||
| 200 | |||
| 201 | // Load general Modules |
||
| 202 | // First load the dependent modules of Mermaid ext |
||
| 203 | SMWOutputs::requireResource( 'ext.mermaid' ); |
||
| 204 | SMWOutputs::requireResource( 'ext.mermaid.styles' ); |
||
| 205 | SMWOutputs::requireResource( 'ext.srf.gantt' ); |
||
| 206 | |||
| 207 | //Add Tasks & Sections |
||
| 208 | while ( $row = $queryResult->getNext() ) {
|
||
| 209 | |||
| 210 | $status = []; |
||
| 211 | $priority = []; |
||
| 212 | $startDate = ""; |
||
| 213 | $endDate = ""; |
||
| 214 | $taskID = ""; |
||
| 215 | $taskTitle = ""; |
||
| 216 | $sections = []; |
||
| 217 | |||
| 218 | // Loop through all field of a row |
||
| 219 | foreach ( $row as $field ) {
|
||
| 220 | |||
| 221 | $fieldLabel = $field->getPrintRequest()->getLabel(); |
||
| 222 | |||
| 223 | //get values |
||
| 224 | foreach ( $field->getContent() as $dataItem ) {
|
||
| 225 | |||
| 226 | switch ( $fieldLabel ) {
|
||
| 227 | case "section": |
||
| 228 | $sections[$dataItem->getTitle()->getPrefixedDBKey()] = $dataItem->getSortKey(); |
||
| 229 | break; |
||
| 230 | case "task": |
||
| 231 | if ( $dataItem instanceof SMWDIBlob ) {
|
||
| 232 | $taskTitle = $dataItem->getString(); |
||
| 233 | $taskID = $field->getResultSubject()->getTitle()->getPrefixedDBKey(); |
||
| 234 | } |
||
| 235 | break; |
||
| 236 | case "startdate": |
||
| 237 | if ( $dataItem instanceof SMWDITime ) {
|
||
| 238 | $startDate = $dataItem->getMwTimestamp(); |
||
| 239 | } |
||
| 240 | break; |
||
| 241 | case "enddate": |
||
| 242 | if ( $dataItem instanceof SMWDITime ) {
|
||
| 243 | $endDate = $dataItem->getMwTimestamp(); |
||
| 244 | } |
||
| 245 | break; |
||
| 246 | case "status": |
||
| 247 | if ( $dataItem instanceof SMWDIBlob ) {
|
||
| 248 | array_push( $status, $dataItem->getString() ); |
||
| 249 | } |
||
| 250 | break; |
||
| 251 | case "priority": |
||
| 252 | if ( $dataItem instanceof SMWDIBlob ) {
|
||
| 253 | array_push( $priority, $dataItem->getString() ); |
||
| 254 | } |
||
| 255 | break; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | // Add section/Task |
||
| 261 | // Title, TaskID, StartDate and EndDate are required |
||
| 262 | if ( $taskID != "" && $taskTitle != "" && $startDate != "" && $endDate != "" ) {
|
||
| 263 | $this->mGantt->addTask( $taskID, $taskTitle, $status, $priority, $startDate, $endDate ); |
||
| 264 | |||
| 265 | // If no section was found, put task into a dummy section object |
||
| 266 | // "gantt-no-section#21780240" is used to identify Tasks that with no section (dummy section) |
||
| 267 | if ( count( $sections ) == 0 ) {
|
||
| 268 | $this->mGantt->addSection( "gantt-no-section#21780240", "", $startDate, $endDate, $taskID ); |
||
| 269 | } else {
|
||
| 270 | foreach ( $sections as $sectionID => $sectionTitle ) {
|
||
| 271 | $this->mGantt->addSection( $sectionID, $sectionTitle, $startDate, $endDate, $taskID ); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | // Improve unique id by adding a random number |
||
| 278 | $id = uniqid( 'srf-gantt-' . rand( 1, 10000 ) ); |
||
| 279 | |||
| 280 | // Add gantt configurations |
||
| 281 | $config = [ |
||
| 282 | 'theme' => $this->params['theme'], |
||
| 283 | 'gantt' => [ |
||
| 284 | 'leftPadding' => intval( $this->params['leftpadding'] ), |
||
| 285 | 'titleTopMargin' => intval( $this->params['titletopmargin'] ), |
||
| 286 | 'barHeight' => intval( $this->params['barheight'] ), |
||
| 287 | 'barGap' => intval( $this->params['bargap'] ) |
||
| 288 | ] |
||
| 289 | ]; |
||
| 290 | |||
| 291 | // Manage Output |
||
| 292 | if ( !empty( $this->mErrors ) ) {
|
||
| 293 | return $queryResult->addErrors( $this->mErrors ); |
||
| 294 | } else {
|
||
| 295 | return Html::rawElement( 'div', [ |
||
| 296 | 'id' => $id, |
||
| 297 | 'class' => 'srf-gantt', |
||
| 298 | 'data-mermaid' => json_encode( [ |
||
| 299 | 'content' => $this->mGantt->getGanttOutput(), |
||
| 300 | 'config' => $config |
||
| 301 | ], JSON_UNESCAPED_UNICODE ) |
||
| 302 | ], Html::rawElement( 'div', [ |
||
| 303 | 'class' => 'mermaid-dots', |
||
| 304 | ] ) ); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | } |
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.