CredentialController   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 82.24 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 7
dl 88
loc 107
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 6 1
B addCredentialAction() 44 44 6
B editCredentialAction() 44 44 6
A deleteCredentialAction() 0 7 1

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 PRReviewWatcher\Controller;
4
5
use Silex\Application;
6
use Symfony\Component\HttpFoundation\Request;
7
use PRReviewWatcher\Entity\Credential;
8
use PRReviewWatcher\Form\Type\CredentialType;
9
use GuzzleHttp\Client;
10
use GuzzleHttp\Exception\ClientException;
11
12
class CredentialController
13
{
14
    public function indexAction(Application $app)
15
    {
16
        $credentials = $app['credential_repository']->findAll();
17
18
        return $app['twig']->render('credList.html.twig', array('credentials' => $credentials));
19
    }
20
21 View Code Duplication
    public function addCredentialAction(Request $request, Application $app)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
22
    {
23
        $credential     = new Credential();
24
        $credentialForm = $app['form.factory']->create(new CredentialType(), $credential, ['mapped' => true, 'disable' => false]);
25
        $credentialForm->handleRequest($request);
26
27
        if ($credentialForm->isSubmitted() && $credentialForm->isValid()) {
28
            $token      = $credential->getToken();
29
            $name       = $credential->getNameCred();
30
31
            try {
32
                $client   = new Client();
33
                $res      = $client->get('https://api.github.com/user', [
34
                    'auth' => [
35
                        'token',
36
                        $token
37
                    ]
38
                ]);
39
                $status   = $res->getStatusCode();
40
                $testName = json_decode($res->getBody()->getContents(), true);
41
42
            } catch (ClientException $e) {
43
                $status = $e->getResponse()->getStatusCode();
44
            }
45
            if ($status == 200) {
46
                if ($name == $testName['login']) {
0 ignored issues
show
Bug introduced by
The variable $testName 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...
47
                    $app['credential_repository']->save($credential);
48
                    $app['session']->getFlashBag()->add('success', 'The credential was successfully created.');
49
                } else {
50
                    $app['session']->getFlashBag()->add('danger',
51
                        'This is not the user assigned at this token.');
52
                }
53
            } else {
54
                $app['session']->getFlashBag()->add('danger',
55
                    'The token is incorect. (GitHub API error ' . $status . ')');
56
            }
57
        }
58
59
        return $app['twig']->render('credList_form.html.twig', array(
60
            'title'          => 'New credential',
61
            'legend'         => 'New credential',
62
            'credentialForm' => $credentialForm->createView(),
63
        ));
64
    }
65
66 View Code Duplication
    public function editCredentialAction($id, Request $request, Application $app)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
67
    {
68
        $credential     = $app['credential_repository']->find($id);
69
        $credentialForm = $app['form.factory']->create(new CredentialType(), $credential, ['mapped' => false, 'disable' => true]);
70
        $credentialForm->handleRequest($request);
71
72
        if ($credentialForm->isSubmitted() && $credentialForm->isValid()) {
73
            $token      = $credential->getToken();
74
            $name       = $credential->getNameCred();
75
76
            try {
77
                $client   = new Client();
78
                $res      = $client->get('https://api.github.com/user', [
79
                    'auth' => [
80
                        'token',
81
                        $token
82
                    ]
83
                ]);
84
                $status   = $res->getStatusCode();
85
                $testName = json_decode($res->getBody()->getContents(), true);
86
87
            } catch (ClientException $e) {
88
                $status = $e->getResponse()->getStatusCode();
89
            }
90
            if ($status == 200) {
91
                if ($name == $testName['login']) {
0 ignored issues
show
Bug introduced by
The variable $testName 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...
92
                    $app['credential_repository']->save($credential);
93
                    $app['session']->getFlashBag()->add('success', 'The credential was successfully updated.');
94
                } else {
95
                    $app['session']->getFlashBag()->add('danger',
96
                        'This is not the user assigned at this token.');
97
                }
98
            } else {
99
                $app['session']->getFlashBag()->add('danger',
100
                    'The token is incorect. (GitHub API error ' . $status . ')');
101
            }
102
        }
103
104
        return $app['twig']->render('credList_form.html.twig', array(
105
            'title'          => 'Edit credential',
106
            'legend'         => 'Edit credential',
107
            'credentialForm' => $credentialForm->createView(),
108
        ));
109
    }
110
111
    public function deleteCredentialAction($id, Application $app)
112
    {
113
        $app['credential_repository']->delete($id);
114
        $app['session']->getFlashBag()->add('success', 'The credential was successfully removed.');
115
116
        return $app->redirect('/admin/credential');
117
    }
118
}
119