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.
Completed
Push — master ( ca9b69...35d367 )
by Axel
03:00
created

Abac::enforce()   D

Complexity

Conditions 14
Paths 137

Size

Total Lines 45
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 18.4

Importance

Changes 13
Bugs 1 Features 5
Metric Value
c 13
b 1
f 5
dl 0
loc 45
ccs 28
cts 39
cp 0.7179
rs 4.7877
cc 14
eloc 33
nc 137
nop 4
crap 18.4

How to fix   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 PhpAbac;
4
5
use PhpAbac\Manager\AttributeManager;
6
use PhpAbac\Manager\PolicyRuleManager;
7
use PhpAbac\Manager\ConfigurationManager;
8
use PhpAbac\Manager\CacheManager;
9
use PhpAbac\Manager\ComparisonManager;
10
11
use Symfony\Component\Config\FileLocator;
12
13
use PhpAbac\Model\PolicyRuleAttribute;
14
15
class Abac
16
{
17
    /** @var \PhpAbac\Manager\ConfigurationManager **/
18
    private $configuration;
19
    /** @var \PhpAbac\Manager\PolicyRuleManager **/
20
    private $policyRuleManager;
21
    /** @var \PhpAbac\Manager\AttributeManager **/
22
    private $attributeManager;
23
    /** @var \PhpAbac\Manager\CacheManager **/
24
    private $cacheManager;
25
    /** @var \PhpAbac\Manager\ComparisonManager **/
26
    private $comparisonManager;
27
28
    /**
29
     * @param array $configPaths
30
     */
31 1
    public function __construct($configPaths)
32
    {
33 1
        $this->configure($configPaths);
34 1
        $this->attributeManager = new AttributeManager($this->configuration->getAttributes());
35 1
        $this->policyRuleManager = new PolicyRuleManager($this->attributeManager, $this->configuration->getRules());
36 1
        $this->cacheManager = new CacheManager();
37 1
        $this->comparisonManager = new ComparisonManager($this->attributeManager);
38 1
    }
39
    
40
    /**
41
     * @param array $configPaths
42
     */
43 1
    public function configure($configPaths) {
44 1
        $locator = new FileLocator($configPaths);
45 1
        $this->configuration = new ConfigurationManager($locator);
46 1
        $this->configuration->parseConfigurationFile($configPaths);
47 1
    }
48
49
    /**
50
     * Return true if both user and object respects all the rules conditions
51
     * If the objectId is null, policy rules about its attributes will be ignored
52
     * In case of mismatch between attributes and expected values,
53
     * an array with the concerned attributes slugs will be returned.
54
     * 
55
     * Available options are :
56
     * * dynamic_attributes: array
57
     * * cache_result: boolean
58
     * * cache_ttl: integer
59
     * * cache_driver: string
60
     * 
61
     * Available cache drivers are :
62
     * * memory
63
     * 
64
     * @param string $ruleName
65
     * @param object $user
66
     * @param object $resource
67
     * @param array $options
68
     * @return boolean|array
69
     */
70 1
    public function enforce($ruleName, $user, $resource = null, $options = []) {
71
        // Retrieve cache value for the current rule and values if cache item is valid
72 1
        if(($cacheResult = isset($options['cache_result']) && $options['cache_result'] === true) === true) {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $cacheResult = (isset($o...ache_result'] === true), Probably Intended Meaning: ($cacheResult = isset($o...cache_result'] === true
Loading history...
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
73
            $cacheItem = $this->cacheManager->getItem(
74
                "$ruleName-{$user->getId()}-" . (($resource !== null) ? $resource->getId() : ''),
75
                (isset($options['cache_driver'])) ? $options['cache_driver'] : null,
76
                (isset($options['cache_ttl'])) ? $options['cache_ttl'] : null
77
            );
78
            // We check if the cache value s valid before returning it
79
            if(($cacheValue = $cacheItem->get()) !== null) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
80
                return $cacheValue;
81
            }
82
        }
83 1
        $policyRule = $this->policyRuleManager->getRule($ruleName);
84 1
        $rejectedAttributes = [];
85
86 1
        foreach ($policyRule->getPolicyRuleAttributes() as $pra) {
87 1
            $attribute = $pra->getAttribute();
88 1
            $attribute->setValue($this->attributeManager->retrieveAttribute($attribute, $user, $resource));
89 1
            $dynamicAttributes = (isset($options['dynamic_attributes'])) ? $options['dynamic_attributes'] : [];
90 1
            if(count($pra->getExtraData()) > 0) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
91 1
                $this->processExtraData($pra, $user, $resource);
92 1
            }
93
            $value =
94 1
                ($pra->getValue() === 'dynamic')
95 1
                ? $this->attributeManager->getDynamicAttribute($attribute->getSlug(), $dynamicAttributes)
96 1
                : $pra->getValue()
97 1
            ;
98 1
            if ($this->comparisonManager->compare(
99 1
                $pra->getComparisonType(),
100 1
                $pra->getComparison(),
101 1
                $value,
102 1
                $attribute->getValue(),
103 1
                $pra->getExtraData()
104 1
            ) !== true) {
105 1
                $rejectedAttributes[] = $attribute->getSlug();
106 1
            }
107 1
        }
108 1
        $result = (count($rejectedAttributes) === 0) ? : $rejectedAttributes;
109 1
        if($cacheResult) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
110
            $cacheItem->set($result);
0 ignored issues
show
Bug introduced by
The variable $cacheItem 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
            $this->cacheManager->save($cacheItem);
112
        }
113 1
        return $result;
114
    }
115
    
116 1
    public function processExtraData(PolicyRuleAttribute $pra, $user, $resource) {
117 1
        foreach($pra->getExtraData() as $key => $data) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
118
            switch($key) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after SWITCH keyword; 0 found
Loading history...
119 1
                case 'with':
120 1
                    $pra->removeExtraData('with');
121 1
                    $subPolicyRuleAttributes = [];
122 1
                    foreach($this->policyRuleManager->processRuleAttributes($data) as $subPolicyRuleAttribute) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
123 1
                        $subPolicyRuleAttributes[] = $subPolicyRuleAttribute;
124 1
                    }
125 1
                    $pra->setValue($subPolicyRuleAttributes);
126 1
                    $pra->addExtraData('attribute', $pra->getAttribute());
127 1
                    $pra->addExtraData('user', $user);
128 1
                    $pra->addExtraData('resource', $resource);
129 1
                    break;
130
            }
131 1
        }
132 1
    }
133
}
134