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 ( d1c731...ca9b69 )
by Axel
02:45
created

Abac::enforce()   C

Complexity

Conditions 13
Paths 73

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 19.8615

Importance

Changes 12
Bugs 1 Features 4
Metric Value
c 12
b 1
f 4
dl 0
loc 38
ccs 21
cts 32
cp 0.6563
rs 5.1234
cc 13
eloc 27
nc 73
nop 4
crap 19.8615

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
10
use Symfony\Component\Config\FileLocator;
11
12
class Abac
13
{
14
    /** @var \PhpAbac\Manager\ConfigurationManager **/
15
    private $configuration;
16
    /** @var \PhpAbac\Manager\PolicyRuleManager **/
17
    private $policyRuleManager;
18
    /** @var \PhpAbac\Manager\AttributeManager **/
19
    private $attributeManager;
20
    /** @var \PhpAbac\Manager\CacheManager **/
21
    private $cacheManager;
22
23
    /**
24
     * @param array $configPaths
25
     */
26 1
    public function __construct($configPaths)
27
    {
28 1
        $this->configure($configPaths);
29 1
        $this->attributeManager = new AttributeManager($this->configuration->getAttributes());
30 1
        $this->policyRuleManager = new PolicyRuleManager($this->attributeManager, $this->configuration->getRules());
31 1
        $this->cacheManager = new CacheManager();
32 1
    }
33
    
34
    /**
35
     * @param array $configPaths
36
     */
37 1
    public function configure($configPaths) {
38 1
        $locator = new FileLocator($configPaths);
39 1
        $this->configuration = new ConfigurationManager($locator);
40 1
        $this->configuration->parseConfigurationFile($configPaths);
41 1
    }
42
43
    /**
44
     * Return true if both user and object respects all the rules conditions
45
     * If the objectId is null, policy rules about its attributes will be ignored
46
     * In case of mismatch between attributes and expected values,
47
     * an array with the concerned attributes slugs will be returned.
48
     * 
49
     * Available options are :
50
     * * dynamic_attributes: array
51
     * * cache_result: boolean
52
     * * cache_ttl: integer
53
     * * cache_driver: string
54
     * 
55
     * Available cache drivers are :
56
     * * memory
57
     * 
58
     * @param string $ruleName
59
     * @param object $user
60
     * @param object $object
61
     * @param array $options
62
     * @return boolean|array
63
     */
64 1
    public function enforce($ruleName, $user, $object = null, $options = []) {
65
        // Retrieve cache value for the current rule and values if cache item is valid
66 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...
67
            $cacheItem = $this->cacheManager->getItem(
68
                "$ruleName-{$user->getId()}-" . (($object !== null) ? $object->getId() : ''),
69
                (isset($options['cache_driver'])) ? $options['cache_driver'] : null,
70
                (isset($options['cache_ttl'])) ? $options['cache_ttl'] : null
71
            );
72
            // We check if the cache value s valid before returning it
73
            if(($cacheValue = $cacheItem->get()) !== null) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
74
                return $cacheValue;
75
            }
76
        }
77 1
        $policyRule = $this->policyRuleManager->getRule($ruleName);
78 1
        $rejectedAttributes = [];
79
80 1
        foreach ($policyRule->getPolicyRuleAttributes() as $pra) {
81 1
            $attribute = $pra->getAttribute();
82 1
            $attribute->setValue($this->attributeManager->retrieveAttribute($attribute, $user, $object));
83 1
            $comparisonClass = 'PhpAbac\\Comparison\\'.ucfirst($pra->getComparisonType()).'Comparison';
84 1
            $comparison = new $comparisonClass();
85 1
            $dynamicAttributes = (isset($options['dynamic_attributes'])) ? $options['dynamic_attributes'] : [];
86
            $value =
87 1
                ($pra->getValue() === 'dynamic')
88 1
                ? $this->attributeManager->getDynamicAttribute($attribute->getSlug(), $dynamicAttributes)
89 1
                : $pra->getValue()
90 1
            ;
91 1
            if ($comparison->{$pra->getComparison()}($value, $attribute->getValue()) !== true) {
92 1
                $rejectedAttributes[] = $attribute->getSlug();
93 1
            }
94 1
        }
95 1
        $result = (count($rejectedAttributes) === 0) ? : $rejectedAttributes;
96 1
        if($cacheResult) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
97
            $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...
98
            $this->cacheManager->save($cacheItem);
99
        }
100 1
        return $result;
101
    }
102
}
103