Completed
Push — master ( 317004...e90e68 )
by Marco
09:43 queued 07:21
created

GenericManagerTrait::clearNamespace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 11
loc 11
ccs 5
cts 5
cp 1
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php namespace Comodojo\Cache\Traits;
2
3
/**
4
 *
5
 *
6
 * @package     Comodojo Spare Parts
7
 * @author      Marco Giovinazzi <[email protected]>
8
 * @license     MIT
9
 *
10
 * LICENSE:
11
 *
12
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18
 * THE SOFTWARE.
19
 */
20
21
trait GenericManagerTrait {
22
23 79
    public function genericAddProvider($provider, $weight = 0) {
24
25 79
        $this->stack->add($provider, $weight);
0 ignored issues
show
Bug introduced by
The property stack does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
27 79
        return $this;
28
29
    }
30
31 2
    public function removeProvider($id) {
32
33 2
        return $this->stack->remove($id);
34
35
    }
36
37 2
    public function getProvider($id) {
38
39 2
        return $this->stack->get($id);
40
41
    }
42
43 2
    public function getProviders($enabled = false) {
44
45 2
        return $this->stack->getAll($enabled);
46
47
    }
48
49 29
    public function getSelectedProvider() {
50
51 29
        return $this->selected === null ? $this->vacuum : $this->selected;
0 ignored issues
show
Bug introduced by
The property selected does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property vacuum does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52
53
    }
54
55 11 View Code Duplication
    public function clear() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
57 11
        $result = [];
58
59 11
        foreach ( $this->stack->getAll() as $provider ) {
60
61 9
            $result[] = $provider->clear();
62
63
        }
64
65 11
        return !in_array(false, $result);
66
67
    }
68
69 6
    public function setNamespace($namespace = null) {
70
71 6
        foreach ( $this->stack->getAll(false) as $provider ) {
72 4
            $provider->setNamespace($namespace);
73
        }
74
75 6
        $this->namespace = empty($namespace) ? "GLOBAL" : $namespace;
0 ignored issues
show
Bug introduced by
The property namespace does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
76
77 6
        return $this;
78
79
    }
80
81 4 View Code Duplication
    public function clearNamespace() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
83 4
        $result = [];
84
85 4
        foreach ( $this->stack->getAll() as $provider ) {
86 2
            $result[] = $provider->clearNamespace();
87
        }
88
89 4
        return !in_array(false, $result);
90
91
    }
92
93 2
    public function getStats() {
94
95 2
        $stats = [];
96
97 2
        foreach ( $this->stack->getAll(false) as $provider ) {
98 2
            $stats[] = $provider->getStats();
99
        }
100
101 2
        return $stats;
102
103
    }
104
105 66
    protected function selectProvider() {
106
107 66
        switch ( $this->pick_mode ) {
0 ignored issues
show
Bug introduced by
The property pick_mode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
108
109 66
            case 1:
110 51
                $provider = $this->stack->getFirstProvider();
111 51
                break;
112 15
            case 2:
113 11
                $provider = $this->stack->getLastProvider();
114 11
                break;
115 4
            case 3:
116 4
                $provider = $this->stack->getRandomProvider();
117 4
                break;
118
            case 4:
119
                $provider = $this->stack->getHeavyProvider();
120
                break;
121
122
        }
123
124 66
        $this->selected = $provider == null ? $this->vacuum : $provider;
0 ignored issues
show
Bug introduced by
The variable $provider 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...
125
126 66
        return $this->selected;
127
128
    }
129
130
}
131