Failed Conditions
Pull Request — master (#340)
by Rafael
02:25
created

AnonymousMiddleware::call()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Canvas\Middleware;
6
7
use Phalcon\Mvc\Micro;
8
9
/**
10
 * Class AuthenticationMiddleware.
11
 *
12
 * @package Niden\Middleware
13
 */
14
class AnonymousMiddleware extends AuthenticationMiddleware
15
{
16
    /**
17
     * Call me.
18
     *
19
     * @param Micro $api
20
     * @todo need to check section for auth here
21
     * @return bool
22
     */
23
    public function call(Micro $api)
0 ignored issues
show
Coding Style introduced by
function call() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
24
    {
25
        $config = $api->getService('config');
26
        $request = $api->getService('request');
27
28
        $anonymousUser = false;
29
30
        /**
31
         * This is where we will find if the user exists based on
32
         * the token passed using Bearer Authentication.
33
         */
34
        if (!empty($request->getBearerTokenFromHeader())) {
35
            $token = $this->getToken($request->getBearerTokenFromHeader());
36
        } else {
37
            $anonymousUser = true;
38
        }
39
40
        if (!$anonymousUser) {
41
            $this->sessionUser($api, $config, $token, $request);
0 ignored issues
show
Bug introduced by
The variable $token 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...
42
        } else {
43
            $this->anonymousUser($api, $config, $token, $request);
44
        }
45
46
        return true;
47
    }
48
}
49