Completed
Push — master ( 7e7da0...18ecc7 )
by Georges
01:55
created

DefaultController::indexAction()   D

Complexity

Conditions 12
Paths 204

Size

Total Lines 115
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 115
rs 4.6216
c 0
b 0
f 0
cc 12
eloc 71
nc 204
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AppBundle\Controller;
4
5
use Phpfastcache\Bundle\Service\Phpfastcache;
6
use Symfony\Component\Routing\Annotation\Route;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
10
11
class DefaultController extends Controller
12
{
13
    /**
14
     * @Route("/", name="homepage")
15
     */
16
    public function indexAction(Request $request, Phpfastcache $phpfastcache)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
17
    {
18
        $pfc_test = null;
0 ignored issues
show
Unused Code introduced by
$pfc_test 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...
19
20
        $cache = $phpfastcache->get('filecache');
21
        $cache2 = $phpfastcache->get('memcachecache');
22
        $cache3 = $phpfastcache->get('ssdbcache');
23
        $cache4 = $phpfastcache->get('sqlitecache');
24
        $cache5 = $phpfastcache->get('rediscache');
25
        $cache6 = $phpfastcache->get('mongodbcache');
26
        $cache7 = $phpfastcache->get('couchbasecache');
27
        $cache8 = $phpfastcache->get('leveldbcache');
28
29
        /**
30
         * Xcache and APC cannot coexists
31
         */
32
        try{
33
            $cache9 = $phpfastcache->get('apccache');
34
            $cache10 = $phpfastcache->get('apcucache');
35
        }catch(PhpfastcacheDriverCheckException $e){
36
            $cache11 = $phpfastcache->get('xcachecache');
37
        }
38
39
        $cache12 = $phpfastcache->get('devnullcache');
40
41
42
        $item = $cache->getItem('test');
43
        $item1 = $cache->getItem('test2');
44
        $item2 = $cache2->getItem('test');
45
        $item3 = $cache3->getItem('test');
46
        $item4 = $cache4->getItem('test');
47
        $item5 = $cache5->getItem('test');
48
        $item6 = $cache6->getItem('test');
49
        $item7 = $cache7->getItem('test');
50
        $item8 = $cache7->getItem('test2');
51
        $item9 = $cache8->getItem('test2');
52
53
        if(isset($cache9) && isset($cache10))
54
        {
55
            $item10 = $cache9->getItem('test2');
56
            $item11 = $cache10->getItem('test2');
57
        }
58
59
        if(isset($cache11))
60
        {
61
            $item12 = $cache11->getItem('test2');
62
        }
63
64
        $item13 = $cache12->getItem('test2');
65
66
67
68
        if ($item->isHit()) {
69
            $pfc_test = $item->get();
70
        } else {
71
72
            $item->set('Loaded from cache')->expiresAfter(10);
73
            $item1->set('Loaded from cache2')->expiresAfter(10);
74
            $item2->set('Loaded from cache +' . str_repeat('*', rand(1000, 5000)))->expiresAfter(10);
75
            $item3->set('Loaded from cache +' . str_repeat('+', rand(1000, 5000)))->expiresAfter(10);
76
            $item4->set('Loaded from cache +' . str_repeat('-', rand(1000, 5000)))->expiresAfter(10);
77
            $item5->set('Loaded from cache +' . str_repeat('-', rand(1000, 5000)))->expiresAfter(10);
78
            $item6->set('Loaded from cache +' . str_repeat('-', rand(1000, 5000)))->expiresAfter(10);
79
            $item7->set('Loaded from cache +' . str_repeat('-', rand(1000, 5000)))->expiresAfter(10);
80
            $item8->set('Loaded from cache2')->expiresAfter(10);
81
            $item9->set('Loaded from cache +' . str_repeat('-', rand(1000, 5000)))->expiresAfter(10);
82
83
            if(isset($item10) && isset($item11))
84
            {
85
                $item10->set('Loaded from cache +' . str_repeat('-', rand(1000, 5000)))->expiresAfter(10);
86
                $item11->set('Loaded from cache +' . str_repeat('-', rand(1000, 5000)))->expiresAfter(10);
87
            }
88
89
            if(isset($item12))
90
            {
91
                $item12->set('Loaded from cache +' . str_repeat('-', rand(1000, 5000)))->expiresAfter(10);
92
            }
93
94
            $item13->set('Loaded from cache +' . str_repeat('-', rand(1000, 5000)))->expiresAfter(10);
95
96
97
            $cache->save($item);
98
            $cache->save($item1);
99
            $cache2->save($item2);
100
            $cache3->save($item3);
101
            $cache4->save($item4);
102
            $cache5->save($item5);
103
            $cache6->save($item6);
104
            $cache7->save($item7);
105
            $cache7->save($item8);
106
            $cache8->save($item9);
107
108
            if(isset($cache9) && isset($cache10))
109
            {
110
                $cache9->save($item10);
0 ignored issues
show
Bug introduced by
The variable $item10 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...
111
                $cache10->save($item11);
0 ignored issues
show
Bug introduced by
The variable $item11 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...
112
            }
113
114
            if(isset($cache11))
115
            {
116
                $cache11->save($item12);
0 ignored issues
show
Bug introduced by
The variable $item12 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...
Bug introduced by
It seems like $item12 can be null; however, save() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
117
            }
118
119
            $cache12->save($item13);
120
121
122
            $pfc_test = 'Not loaded from cache';
123
        }
124
125
        // replace this example code with whatever you need
126
        return $this->render('default/index.html.twig', [
127
          'pfc_test' => $pfc_test,
128
          'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
129
        ]);
130
    }
131
}