Completed
Push — nrc-comm-architecture ( 50fffb )
by
unknown
19:11
created

Recommendations.php ➔ RecommendationsInit()   C

Complexity

Conditions 11
Paths 49

Size

Total Lines 94
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
eloc 50
nc 49
nop 2
dl 0
loc 94
rs 5.2653
c 0
b 0
f 0
ccs 0
cts 80
cp 0
crap 132

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
$wgExtensionCredits['parserhook'][] = array(
4
  'name'=>'NRC - Recommendations',
5
  'url'=>'http://www.nrc.gc.ca',
6
  'author'=>'Luc Belliveau',
7
  'description'=>'Recommendation extension based on engines provided by the National Research Council.',
8
  'version'=>'0.0.1'
9
);
10
$wgHooks['OutputPageBeforeHTML'][] = 'RecommendationsInit';
11
12
function RecommendationsInit($article, &$text) {
0 ignored issues
show
Coding Style introduced by
RecommendationsInit uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
13
  global $wgTitle, $wgUser;
14
15
  $loggedIn = $wgUser->mId != 0;
16
  $email = str_replace("'", "\\'", $wgUser->mEmail);
17
  if (strpos($email, "\n") !== false) return false;
18
19
  $articleId = $wgTitle->mArticleID;
20
  $context_obj1 = '';
21
  if ($articleId == 1) {
22
    if ($loggedIn) {
23
      $context = 'article_c1';
24
    } else {
25
      $context = 'article_c2';
26
    }
27
  } else {
28
    $context_obj1 = $articleId;
29
    if ($loggedIn) {
30
      $context = 'article_c3';
31
    } else {
32
      $context = 'article_c4';
33
    }
34
  }
35
36
  /**
37
   * Automatically get the user's GCconnex GUID, solely based on email.
38
   *
39
   * TODO: This must be replaced by SSO.
40
   */
41
  $login_context = 'false';
42
  if ($loggedIn && !isset($_SESSION['GCconnex_GUID'])) {
43
    $cx = stream_context_create([
44
      'http'=> [
45
        'proxy'=>'tcp://proxy.paz.nrc.gc.ca:8080',
46
        'request_fulluri' => true,
47
      ],
48
      // 'https'=>['proxy'=>'proxy.paz.nrc.gc.ca:8080'],
49
    ]);
50
51
    $searchUrl = "http://intranet.canada.ca/search-recherche";
52
    $query = "query-recherche-eng.aspx?a=s&s=3&chk4=on";
53
    $encodedEmail = urlencode($email);
54
    $data = file_get_contents("$searchUrl/$query&q=$encodedEmail", False, $cx);
55
    $re = '/https:\/\/gcconnex.gc.ca\/profile\/(.*?)"/';
56
    if (preg_match($re, $data, $m) === 1) {
57
      $gcconnex_username = $m[1];
58
      $profile_url = "https://gcconnex.gc.ca/profile/$gcconnex_username";
59
      $profileData = file_get_contents($profile_url, False, $cx);
60
      $re = '/"guid":(.*?),/';
61
      if (preg_match($re, $profileData, $pm)) {
62
        $_SESSION['GCconnex_GUID'] = $pm[1];
63
        $login_context = 'true';
64
      }
65
    }
66
  }
67
68
  if ($loggedIn) {
69
    $gcconnex_guid = str_replace("'", "\\'", $_SESSION['GCconnex_GUID']);
70
    if (strpos($gcconnex_guid, "\n") !== false) return false;
71
  }
72
73
  $script = <<<DOC
74
<style>
75
  .nrcRecommendationContainer {
76
    overflow: hidden;
77
    padding-top: 10px;
78
  }
79
  .nrcRecommendationContainer:hover {
80
    overflow: visible;
81
  }
82
  .nrcRecommendationContainer div.fieldset-container {
83
    border: 1px solid #000;
84
    background-color: #eee;
85
    width: 500px;
86
    z-index: 1;
87
    position: relative;
88
  }
89
</style>
90
<link rel="stylesheet" type="text/css" href="/extensions/NRC_Recommendations/static/css/main.css">
91
<script type="text/javascript">
92
  var NRC_context = {
93
    context: '$context',
94
    context_obj1: '$context_obj1',
95
    email: '$email',
96
    gcconnex_guid: '$gcconnex_guid',
0 ignored issues
show
Bug introduced by
The variable $gcconnex_guid 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...
97
    login_context: $login_context,
98
  }
99
</script>
100
<script src="/extensions/NRC_Recommendations/gcpedia.js"></script>
101
DOC;
102
103
  $text = $text . "\n\n" .$script;
104
  return true;
105
}
106