Completed
Push — master ( d69358...27ffc6 )
by Dmitry
04:53 queued 53s
created

OrientationStorage   A

Complexity

Total Complexity 7

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
c 1
b 0
f 0
dl 0
loc 51
ccs 0
cts 26
cp 0
rs 10
wmc 7
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSession() 0 4 1
A getStorage() 0 8 2
A saveStorage() 0 5 1
A set() 0 5 1
A get() 0 5 2
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\components;
13
14
use Yii;
15
use yii\base\Component;
16
17
class OrientationStorage extends Component
18
{
19
    const ORIENTATION_HORIZONTAL = 'horizontal';
20
    const ORIENTATION_VERTICAL = 'vertical';
21
22
    public $sessionKey = 'orientationStorage';
23
24
    public $defaultOrientation = self::ORIENTATION_HORIZONTAL;
25
26
    /**
27
     * @var array
28
     */
29
    protected $storage = [];
30
31
    /**
32
     * @var string session component name
33
     */
34
    public $session = 'session';
35
36
    protected function getSession()
37
    {
38
        return Yii::$app->getSession();
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
39
    }
40
41
    protected function getStorage()
42
    {
43
        $session = $this->getSession();
44
45
        if ($session->has($this->sessionKey)) {
46
            $this->storage = $session->get($this->sessionKey);
47
        }
48
    }
49
50
    protected function saveStorage()
51
    {
52
        $session = $this->getSession();
53
        $session->set($this->sessionKey, $this->storage);
54
    }
55
56
    public function set($route, $orientation)
57
    {
58
        $this->storage[$route] = $orientation;
59
        $this->saveStorage();
60
    }
61
62
    public function get($route)
63
    {
64
        $this->getStorage();
65
        return isset($this->storage[$route]) ? $this->storage[$route] : $this->defaultOrientation;
66
    }
67
}
68