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 ( dc130e...3adfb7 )
by Axel
02:46
created

ComparisonManager::getDynamicAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace PhpAbac\Manager;
4
5
use PhpAbac\Model\PolicyRuleAttribute;
6
7
use PhpAbac\Comparison\ArrayComparison;
8
use PhpAbac\Comparison\BooleanComparison;
9
use PhpAbac\Comparison\DatetimeComparison;
10
use PhpAbac\Comparison\NumericComparison;
11
use PhpAbac\Comparison\StringComparison;
12
13
class ComparisonManager {
14
    /** @var \PhpAbac\Manager\AttributeManager **/
15
    protected $attributeManager;
16
    /** @var array **/
17
    protected $comparisons = [
18
        'array' => ArrayComparison::class,
19
        'boolean' => BooleanComparison::class,
20
        'datetime' => DatetimeComparison::class,
21
        'numeric' => NumericComparison::class,
22
        'string' => StringComparison::class,
23
    ];
24
    /** @var array **/
25
    protected $rejectedAttributes = [];
26
    
27
    /**
28
     * @param \PhpAbac\Manager\AttributeManager $manager
29
     */
30 24
    public function __construct(AttributeManager $manager) {
31 24
        $this->attributeManager = $manager;
32 24
    }
33
    
34
    /**
35
     * This method retrieve the comparison class, instanciate it,
36
     * and then perform the configured comparison
37
     * It does return a control value for special operations,
38
     * but the real check is at the end of the enforce() method,
39
     * when the rejected attributes are counted
40
     * 
41
     * @param PolicyRuleAttribute $pra
42
     * @return bool
43
     */
44 6
    public function compare(PolicyRuleAttribute $pra) {
45 6
        $attribute = $pra->getAttribute();
46
        // The expected value can be set in the configuration as dynamic
47
        // In this case, we retrieve the expected value in the passed options
48
        $praValue =
49 6
            ($pra->getValue() === 'dynamic')
50 6
            ? $this->getDynamicAttribute($attribute->getSlug())
51 6
            : $pra->getValue()
52 6
        ;
53
        // Checking that the configured comparison type is available
54 6
        if(!isset($this->comparisons[$pra->getComparisonType()])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
55 1
            throw new \InvalidArgumentException('The requested comparison class does not exist');
56
        }
57
        // The comparison class will perform the attribute check with the configured method
58
        // For more complex comparisons, the comparison manager is injected
59 5
        $comparison = new $this->comparisons[$pra->getComparisonType()]($this);
60 5
        if(!method_exists($comparison, $pra->getComparison())) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
61 1
            throw new \InvalidArgumentException('The requested comparison method does not exist');
62
        }
63
        // Then the comparison is performed with needed
64 4
        $result = $comparison->{$pra->getComparison()}($praValue, $attribute->getValue(), $pra->getExtraData());
65
        // If the checked attribute is not valid, the attribute slug is marked as rejected
66
        // The rejected attributes will be returned instead of the expected true boolean
67 4
        if($result !== true) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
68 3
            if(!in_array($attribute->getSlug(), $this->rejectedAttributes)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
69 3
                $this->rejectedAttributes[] = $attribute->getSlug();
70 3
            }
71 3
            return false;
72
        }
73 3
        return true;
74
    }
75
    
76
    /**
77
     * @param array $dynamicAttributes
78
     */
79 2
    public function setDynamicAttributes($dynamicAttributes) {
80 2
        $this->dynamicAttributes = $dynamicAttributes;
0 ignored issues
show
Bug introduced by
The property dynamicAttributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
81 2
    }
82
    
83
    /**
84
     * A dynamic attribute is a value given by the user code as an option
85
     * If a policy rule attribute is dynamic,
86
     * we check that the developer has given a dynamic value in the options
87
     * 
88
     * Dynamic attributes are given with slugs as key
89
     * 
90
     * @param string $attributeSlug
91
     * @return mixed
92
     * @throws \InvalidArgumentException
93
     */
94 3
    public function getDynamicAttribute($attributeSlug) {
95 3
        if(!isset($this->dynamicAttributes[$attributeSlug])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
96 1
            throw new \InvalidArgumentException("The dynamic value for attribute $attributeSlug was not given");
97
        }
98 2
        return $this->dynamicAttributes[$attributeSlug];
99
    }
100
    
101
    /**
102
     * @param string $type
103
     * @param string $class
104
     */
105
    public function addComparison($type, $class) {
106
        $this->comparisons[$type] = $class;
107
    }
108
    
109
    /**
110
     * @return \PhpAbac\Manager\AttributeManager
111
     */
112 2
    public function getAttributeManager() {
113 2
        return $this->attributeManager;
114
    }
115
    
116
    /**
117
     * This method is called when all the policy rule attributes are checked
118
     * All along the comparisons, the failing attributes slugs are stored
119
     * If the rejected attributes array is not empty, it means that the rule is not enforced
120
     * 
121
     * @return array|bool
122
     */
123 3
    public function getResult() {
124
        $result =
125 3
            (count($this->rejectedAttributes) > 0)
126 3
            ? $this->rejectedAttributes
127 2
            : true
128 3
        ;
129 3
        $this->rejectedAttributes = [];
130 3
        return $result;
131
    }
132
}