GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SecurityController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 8.14 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 0
dl 7
loc 86
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 12 1
B login() 7 39 4
A logout() 0 9 1
A confirm() 0 20 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace BitPrepared\Bundle\D1b0Workspace\Controller\V1;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Silex\Application;
9
use Silex\Api\ControllerProviderInterface;
10
use RedBeanPHP\Facade as R;
11
12
class SecurityController implements ControllerProviderInterface
13
{
14
15
    private $app;
16
17
    public function connect(Application $app)
18
    {
19
        $this->app = $app;
20
        $factory = $app['controllers_factory'];
21
        # il mount point e' precedente e non serve prima
22
        $this->app['db'];
23
        //R::fancyDebug( TRUE );
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
24
        $factory->post('/login', array($this, 'login'));
25
        $factory->get('/logout', array($this, 'logout'));
26
        $factory->get('/confirm', array($this, 'confirm'));
27
        return $factory;
28
    }
29
    public function login(Request $request)
30
    {
31
        /*TODO remove this line in producton DBG DATA {"authMode":"Email","email":"[email protected]","name":"ugo","surname":"ugo","password":"cane"}*/
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
32
        $data = json_decode($request->getContent(), true);
33
        if ($data === NULL) {
34
            $headers = [];
35
            $response = JsonResponse::create($res, 403, $headers)->setSharedMaxAge(300);
0 ignored issues
show
Bug introduced by
The variable $res seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
36
            return $response;
37
        }
38
39
        $authMode = $data['authMode'];
40
41
        if ($authMode === 'Email') {
42
            $email = $data['email'];
43
            $password = $data['password'];
44
            $name = $data['name'];
45
            $surname = $data['surname'];
46
            $user = R::findOne('user', "WHERE email = ? AND name = ? AND surname = ?", [$email, $name, $surname]);
47
            if ($user->pwd === hash("sha256", $user->salt.$password)) {
48
                //LOGGED IN!
49
                $this->app['session']->set('user', ['id' => $user->id]);
50
                $headers = [];
51
                $res = [
52
                        "token"=>"blablabla", //TODO CREATE token
53
                        "clientId"=>$user->id
54
                ];
55
                $response = JsonResponse::create($res, 200, $headers)->setSharedMaxAge(300);
56 View Code Duplication
            }else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
                $headers = [];
58
                $res = [
59
                        "errore"=>"sbagliato password o user" //TODO roba
60
                ];
61
                $response = JsonResponse::create($res, 401, $headers)->setSharedMaxAge(300);
62
            }
63
        }else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
64
            //Facebook Redirect
65
        }
66
        return $response; // JsonResponse::create($output, 200, $headers)->setSharedMaxAge(300);
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...
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
67
    }
68
    public function logout(Request $request)
69
    {
70
        $this->app['session']->clear();
71
        $response = new Response();
72
        $response->headers->set('Content-Type', 'text/html');
73
        $response->setStatusCode(Response::HTTP_NO_CONTENT);
74
        $response->setSharedMaxAge(300);
75
        return $response;
76
    }
77
    public function confirm(Request $request)
78
    {
79
            $confirmKey = $request->request->get('confirmKey');
80
            $verify = R::findOne('verify', "WHERE key = ?", [$confirmKey]);
81
            if (!$bean->id) {
0 ignored issues
show
Bug introduced by
The variable $bean does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
82
                //TODO mettere un controllo agli IP che forzano le richieste di token falsi
83
                $response = "<html><head></head><body>Token non esistente!</body></html>";
84
            }else {
85
                if (strtotime($verify->inserttime) < strtotime("-15 minutes")) {
86
                    $user = R::load('user', $verify->user);
87
                    $user->status = "enabled";
88
                    $user->updatetime = date('Y-m-d H:i:s');
89
                    $id = R::store($user);
0 ignored issues
show
Unused Code introduced by
$id 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...
90
                    $response = "<html><head></head><body>Account attivato complimenti!</body></html>";
91
                }else {
92
                    $response = "<html><head></head><body>Impossibile attivare account inserire mail e password per richiedere un nuovo token!</body></html>";
93
                }
94
            }
95
            return $response;
96
    }
97
}
98