HybridSessionMiddleware::process()   A
last analyzed

Complexity

Conditions 2
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 2
nc 4
nop 2
1
<?php
2
3
namespace SilverStripe\HybridSessions\Control;
4
5
use SilverStripe\Control\Middleware\HTTPMiddleware;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\HybridSessions\HybridSession;
8
9
class HybridSessionMiddleware implements HTTPMiddleware
10
{
11
    public function process(HTTPRequest $request, callable $delegate)
12
    {
13
        try {
14
            // Start session and execute
15
            $request->getSession()->init($request);
16
17
            // Generate output
18
            $response = $delegate($request);
19
        } finally {
20
            // Save session data, even if there was an exception
21
            // Note that save() will start/resume the session if required.
22
            $request->getSession()->save($request);
23
24
            if (HybridSession::is_enabled()) {
25
                // Close the session
26
                session_write_close();
27
            }
28
        }
29
30
        return $response;
0 ignored issues
show
Bug introduced by
The variable $response 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...
31
    }
32
}
33