Session::setRequestId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 */
6
7
namespace flipbox\saml\core\services;
8
9
use craft\base\Component;
10
11
class Session extends Component
12
{
13
    const CORE_NAMESPACE = 'saml.core';
14
    const REQUEST_ID_KEY = 'request.id';
15
    const HASH_ALGO = 'sha256';
16
17
    /**
18
     * @param $id
19
     */
20
    public function setRequestId($id)
21
    {
22
        \Craft::$app->getSession()->set(
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...
23
            $this->getName(static::REQUEST_ID_KEY),
24
            $id
25
        );
26
    }
27
28
    /**
29
     * @return string
30
     */
31
    public function getRequestId()
32
    {
33
        return \Craft::$app->getSession()->get(
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...
34
            $this->getName(static::REQUEST_ID_KEY)
35
        );
36
    }
37
    
38
    /**
39
     * @return string
40
     */
41
    public function getId()
42
    {
43
        return hash(static::HASH_ALGO, \Craft::$app->session->getId());
44
    }
45
46
    /**
47
     * @param $key
48
     * @return string
49
     */
50
    protected function getName($key)
51
    {
52
        return static::CORE_NAMESPACE . '/' . $key;
53
    }
54
}
55