SDImportJob::run()   C
last analyzed

Complexity

Conditions 11
Paths 9

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 9
nop 0
dl 0
loc 64
rs 6.6387
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * Background job to import a page into the wiki, adapted from Data Transfer
5
 *
6
 * @author Yaron Koren
7
 * @author Toni Hermoso
8
 */
9
class SDImportJob extends Job {
10
11
   function __construct( $title, $params = '', $id = 0 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
12
       parent::__construct( 'sdImport', $title, $params, $id );
13
   }
14
15
   /**
16
    * Run a dtImport job
17
    * @return boolean success
18
    */
19
   function run() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
       wfProfileIn( __METHOD__ );
21
22
       if ( is_null( $this->title ) ) {
23
           $this->error = "sdImport: Invalid title";
24
           wfProfileOut( __METHOD__ );
25
           return false;
26
       }
27
28
       if ( method_exists( 'WikiPage', 'getContent' ) ) {
29
           $wikiPage = new WikiPage( $this->title );
30
           if ( !$wikiPage ) {
31
               $this->error = 'sdImport: Wiki page not found "' . $this->title->getPrefixedDBkey() . '"';
32
               wfProfileOut( __METHOD__ );
33
               return false;
34
           }
35
       } else {
36
         // Remove old support
37
         return false;
38
       }
39
       $for_pages_that_exist = $this->params['for_pages_that_exist'];
40
       if ( $for_pages_that_exist == 'skip' && $this->title->exists() ) {
41
           return true;
42
       }
43
44
       // Change global $wgUser variable to the one specified by
45
       // the job only for the extent of this import.
46
       global $wgUser;
47
       $actual_user = $wgUser;
48
       $wgUser = User::newFromId( $this->params['user_id'] );
49
       $text = $this->params['text'];
50
       $edit_summary = $this->params['edit_summary'];
51
52
       // Act according to storage here
53
       if ( $this->params['storage'] == "json" ) {
54
          // TODO: to evaluate if check destination page here or before
55
56
          // Handle JSON new content
57
    			$contentModel = $wikiPage->getContentModel();
58
    			if ( $contentModel === "json" || ! $wikiPage->exists() ) {
59
    				$new_content = new JSONContent( $text );
60
    			}
61
62
       } else {
63
64
         if ( $this->title->exists() ) {
65
             if ( $for_pages_that_exist == 'append' ) {
66
                     // MW >= 1.21
67
               $existingText = $wikiPage->getContent()->getNativeData();
68
               $text = $existingText . "\n" . $text;
69
70
             }
71
         }
72
73
         $new_content = new WikitextContent( $text );
74
75
       }
76
77
       $wikiPage->doEditContent( $new_content, $edit_summary );
0 ignored issues
show
Bug introduced by
The variable $new_content does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
78
79
       $wgUser = $actual_user;
80
       wfProfileOut( __METHOD__ );
81
       return true;
82
   }
83
}
84