CookieMakerExtension   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 30 6
A addSessions() 0 4 1
1
<?php
2
3
namespace whm\Smoke\Extensions\CookieMaker;
4
5
use Koalamon\CookieMakerHelper\CookieMaker;
6
use Koalamon\FallbackHelper\FallbackHelper;
7
use whm\Smoke\Http\Session;
8
use whm\Smoke\Scanner\SessionContainer;
9
10
class CookieMakerExtension
11
{
12
    private $executable = 'CookieMaker';
13
14
    private $sessionContainer;
15
16
    public function init(array $sessions, $executable = null)
17
    {
18
        if ($executable) {
19
            $this->executable = $executable;
20
        }
21
22
        $this->sessionContainer = new SessionContainer();
23
24
        foreach ($sessions as $sessionName => $session) {
25
26
            try {
27
                $cookieMaker = new CookieMaker($this->executable);
28
                $cookies = $cookieMaker->getCookies($session);
29
            } catch (\Exception $e) {
30
                $fallbackHelper = new FallbackHelper();
31
                if (!$fallbackHelper->isFallbackServer()) {
32
                    $fallbackHelper->doFallback($e->getMessage());
33
                    exit(1);
34
                }
35
            }
36
37
            $session = new Session();
38
39
            foreach ($cookies as $key => $value) {
0 ignored issues
show
Bug introduced by
The variable $cookies 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...
40
                $session->addCookie($key, $value);
41
            }
42
43
            $this->sessionContainer->addSession($sessionName, $session);
44
        }
45
    }
46
47
    /**
48
     * @Event("ResponseRetriever.setSessionContainer.before")
49
     */
50
    public function addSessions(SessionContainer $sessionContainer)
51
    {
52
        $sessionContainer->addContainer($this->sessionContainer);
53
    }
54
}
55