PipController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 51
ccs 0
cts 25
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A actionInstall() 0 4 1
A actionUpdate() 0 4 1
A actionDoInstall() 0 6 2
A actionDoUpdate() 0 4 1
A run() 0 12 3
A getName() 0 4 1
A getConfiguration() 0 6 1
1
<?php
2
3
/*
4
 * PyPI plugin for HiDev
5
 *
6
 * @link      https://github.com/hiqdev/hidev-pypi
7
 * @package   hidev-pypi
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hidev\pypi\controllers;
13
14
use Yii;
15
16
/**
17
 * Goal for Pip.
18
 */
19
class PipController extends \hidev\controllers\CommonController
20
{
21
    protected $_before = ['setup.py'];
22
23
    public function actionInstall()
24
    {
25
        return $this->runActions(['before', 'do-install', 'after']);
26
    }
27
28
    public function actionUpdate()
29
    {
30
        return $this->runActions(['before', 'do-update', 'after']);
31
    }
32
33
    public function actionDoInstall()
34
    {
35
        $dir = Yii::getAlias('@prjdir/env');
36
37
        return is_dir($dir) ? 0 : $this->run('install');
38
    }
39
40
    public function actionDoUpdate()
41
    {
42
        return $this->run('update');
43
    }
44
45
    public function run($command, $dir = null)
46
    {
47
        if ($dir === null) {
48
            $dir = Yii::getAlias('@prjdir');
49
        }
50
        if ($dir) {
51
            $args[] = '-d';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$args was never initialized. Although not strictly required by PHP, it is generally a good practice to add $args = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key 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.

Loading history...
52
            $args[] = $dir;
53
        }
54
        $args[] = $command;
0 ignored issues
show
Bug introduced by
The variable $args 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...
55
        return $this->passthru('pip', $args);
0 ignored issues
show
Documentation introduced by
$args is of type array<integer,?>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
    }
57
58
    public function getName()
59
    {
60
        return $this->getConfiguration()->getName();
61
    }
62
63
    public function getConfiguration()
64
    {
65
        $conf = $this->takeGoal('setup.py');
66
        $conf->runAction('load');
67
        return $conf;
68
    }
69
}
70