Completed
Push — master ( 78e1d9...14a0b1 )
by Dmitry
10:35 queued 02:42
created

OrientationStorage::saveStorage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 9.4285
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\base;
13
14
use Yii;
15
use yii\base\Component;
16
use yii\caching\Cache;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, hipanel\base\Cache.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use yii\di\Instance;
18
use yii\web\Session;
19
20
class OrientationStorage extends Component
21
{
22
    const ORIENTATION_HORIZONTAL = 'horizontal';
23
    const ORIENTATION_VERTICAL = 'vertical';
24
25
    public $sessionKey = 'orientationStorage';
26
27
    public $defaultOrientation = self::ORIENTATION_HORIZONTAL;
28
29
    /**
30
     * @var array
31
     */
32
    protected $storage = [];
33
34
    /**
35
     * @var string session component name
36
     */
37
    public $session = 'session';
38
39
    protected function getSession()
40
    {
41
        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...
42
    }
43
44
    protected function getStorage()
45
    {
46
        $session = $this->getSession();
47
48
        if ($session->has($this->sessionKey)) {
49
            $this->storage = $session->get($this->sessionKey);
50
        }
51
    }
52
53
    protected function saveStorage()
54
    {
55
        $session = $this->getSession();
56
        $session->set($this->sessionKey, $this->storage);
57
    }
58
59
    public function set($route, $orientation)
60
    {
61
        $this->storage[$route] = $orientation;
62
        $this->saveStorage();
63
    }
64
65
    public function get($route)
66
    {
67
        $this->getStorage();
68
        return isset($this->storage[$route]) ? $this->storage[$route] : $this->defaultOrientation;
69
    }
70
}
71