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.

CacheAllExcept::shouldCache()   C
last analyzed

Complexity

Conditions 19
Paths 82

Size

Total Lines 56
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 6.5437
c 0
b 0
f 0
cc 19
eloc 31
nc 82
nop 1

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
 * @author Bram Gerritsen [email protected]
4
 * @copyright (c) Bram Gerritsen 2013
5
 * @license http://opensource.org/licenses/mit-license.php
6
 */
7
8
namespace StrokerCache\Strategy;
9
10
use Zend\Mvc\MvcEvent;
11
12
use StrokerCache\Exception\BadConfigurationException;
13
14
class CacheAllExcept extends AbstractStrategy
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $except;
20
21
    /**
22
     * {@inheritDoc}
23
     */
24
    public function shouldCache(MvcEvent $event)
25
    {
26
        $except = $this->getExcept();
27
28
        if (!isset($except['namespaces']) && !isset($except['controllers']) && !isset($except['actions'])) {
29
            throw new BadConfigurationException(
30
                  "At least one of ['namespaces', 'controllers', 'actions'] keys has to be set in the "
31
                . "\$config['strokercache']['strategies']['enabled']['" . __CLASS__ . "']['except'][] "
32
                . "configuration array."
33
            );
34
        }
35
36
        $routeMatch = $event->getRouteMatch();
37
38
        if (null === $routeMatch) {
39
            return false;
40
        }
41
42
        $controller  = $this->normalize($routeMatch->getParam('controller'));
43
        $action      = $this->normalize($routeMatch->getParam('action'));
44
45
        $shouldCache = true;
46
47
        if (true === $shouldCache && isset($except['namespaces'])) {
48
            foreach ($except['namespaces'] as $exceptNamespace) {
49
                if (0 === strpos($controller, $this->normalize($exceptNamespace))) {
50
                    $shouldCache = false;
51
                    break 1;
52
                }
53
            }
54
        }
55
56
        if (true === $shouldCache && isset($except['controllers'])) {
57
            foreach ($except['controllers'] as $exceptController) {
58
                if ($controller === $this->normalize($exceptController)) {
59
                    $shouldCache = false;
60
                    break 1;
61
                }
62
            }
63
        }
64
65
        if (true === $shouldCache && isset($except['actions'])) {
66
            foreach ($except['actions'] as $exceptController => $exceptActions) {
67
                if ($controller === $this->normalize($exceptController)) {
68
                    foreach ($exceptActions as $exceptAction) {
69
                        if ($action === $this->normalize($exceptAction)) {
70
                            $shouldCache = false;
71
                            break 2;
72
                        }
73
                    }
74
                }
75
            }
76
        }
77
78
        return $shouldCache;
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    public function getExcept()
85
    {
86
        return $this->except;
87
    }
88
89
    /**
90
     * @param array $except
91
     * @return $this
92
     */
93
    public function setExcept(array $except)
94
    {
95
        $this->except = $except;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Normalize names before comparing
102
     *
103
     * @param string $string
104
     * @return string
105
     */
106
    protected function normalize($string)
107
    {
108
        $string = trim($string);
109
110
        $string = strtolower($string);
111
112
        return $string;
113
    }
114
}
115