Middleware   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 9
Bugs 6 Features 1
Metric Value
wmc 4
c 9
b 6
f 1
lcom 0
cbo 3
dl 0
loc 25
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 22 4
1
<?php
2
/**
3
 * @author   Temitope Olotin <[email protected]>
4
 * @license  <https://opensource.org/license/MIT> MIT
5
 */
6
namespace Laztopaz\EmojiRestfulAPI;
7
8
use Exception;
9
use Firebase\JWT\JWT;
10
use Psr\Http\Message\ResponseInterface as Response;
11
use Psr\Http\Message\ServerRequestInterface as Request;
12
13
class Middleware
14
{
15
    public function __invoke(Request $request, Response $response, $next)
16
    {
17
        $loadEnv = DatabaseConnection::loadEnv();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $loadEnv is correct as \Laztopaz\EmojiRestfulAP...seConnection::loadEnv() (which targets Laztopaz\EmojiRestfulAPI...seConnection::loadEnv()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$loadEnv is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
18
19
        $authHeader = $request->getHeader('HTTP_AUTHORIZATION');
20
21
        try {
22
            if (is_array($authHeader) && ! empty($authHeader)) {
23
                $secretKey = base64_decode(getenv('secret'));
24
                $jwt = json_decode($authHeader[0], true);
25
26
                //decode the JWT using the key from config
27
                $decodedToken = JWT::decode($jwt['jwt'], $secretKey, ['HS512']);
0 ignored issues
show
Unused Code introduced by
$decodedToken is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
28
29
                return $next($request, $response);
30
            }
31
        } catch (Exception $e) {
32
            return $response->withJson(['message' => $e->getMessage()], 401);
33
        }
34
35
        return $response->withJson(['message' => 'User unauthorized due to invalid token'], 401);
36
    }
37
}
38