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.

ComparisonManager::compare()   C
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 18
cts 18
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 10
nop 2
crap 7
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\ObjectComparison;
12
use PhpAbac\Comparison\UserComparison;
13
use PhpAbac\Comparison\StringComparison;
14
15
class ComparisonManager {
16
    /** @var \PhpAbac\Manager\AttributeManager **/
17
    protected $attributeManager;
18
    /** @var array **/
19
    protected $comparisons = [
20
        'array' => ArrayComparison::class,
21
        'boolean' => BooleanComparison::class,
22
        'datetime' => DatetimeComparison::class,
23
        'numeric' => NumericComparison::class,
24
        'object' => ObjectComparison::class,
25
        'user' => UserComparison::class,
26
        'string' => StringComparison::class,
27
    ];
28
    /** @var array **/
29
    protected $rejectedAttributes = [];
30
31
    /**
32
     * @param \PhpAbac\Manager\AttributeManager $manager
33
     */
34 29
    public function __construct(AttributeManager $manager) {
35 29
        $this->attributeManager = $manager;
36 29
    }
37
38
    /**
39
     * This method retrieve the comparison class, instanciate it,
40
     * and then perform the configured comparison
41
     * It does return a control value for special operations,
42
     * but the real check is at the end of the enforce() method,
43
     * when the rejected attributes are counted.
44
     *
45
     * If the second parameter is set to true, compare will not report errors.
46
     * This is used to test a bunch of comparisons expecting not all of them true to return a granted access.
47
     * In fact, this parameter is used in comparisons which need to perform comparisons on their own.
48
     *
49
     * @param PolicyRuleAttribute $pra
50
     * @param boolean $subComparing
51
     * @return bool
52
     */
53 9
    public function compare(PolicyRuleAttribute $pra, $subComparing = false) {
54 9
        $attribute = $pra->getAttribute();
55
        // The expected value can be set in the configuration as dynamic
56
        // In this case, we retrieve the expected value in the passed options
57
        $praValue =
58 9
            ($pra->getValue() === 'dynamic')
59 9
            ? $this->getDynamicAttribute($attribute->getSlug())
60 9
            : $pra->getValue()
61 9
        ;
62
        // Checking that the configured comparison type is available
63 9
        if(!isset($this->comparisons[$pra->getComparisonType()])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
64 1
            throw new \InvalidArgumentException('The requested comparison class does not exist');
65
        }
66
        // The comparison class will perform the attribute check with the configured method
67
        // For more complex comparisons, the comparison manager is injected
68 8
        $comparison = new $this->comparisons[$pra->getComparisonType()]($this);
69 8
        if(!method_exists($comparison, $pra->getComparison())) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
70 1
            throw new \InvalidArgumentException('The requested comparison method does not exist');
71
        }
72
        // Then the comparison is performed with needed
73 7
        $result = $comparison->{$pra->getComparison()}($praValue, $attribute->getValue(), $pra->getExtraData());
74
        // If the checked attribute is not valid, the attribute slug is marked as rejected
75
        // The rejected attributes will be returned instead of the expected true boolean
76 7
        if($result !== true) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
77
            // In case of sub comparing, the error reporting is disabled
78 6
            if(!in_array($attribute->getSlug(), $this->rejectedAttributes) && $subComparing === false) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
79 5
                $this->rejectedAttributes[] = $attribute->getSlug();
80 5
            }
81 6
            return false;
82
        }
83 6
        return true;
84
    }
85
86
    /**
87
     * @param array $dynamicAttributes
88
     */
89 2
    public function setDynamicAttributes($dynamicAttributes) {
90 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...
91 2
    }
92
93
    /**
94
     * A dynamic attribute is a value given by the user code as an option
95
     * If a policy rule attribute is dynamic,
96
     * we check that the developer has given a dynamic value in the options
97
     *
98
     * Dynamic attributes are given with slugs as key
99
     *
100
     * @param string $attributeSlug
101
     * @return mixed
102
     * @throws \InvalidArgumentException
103
     */
104 3
    public function getDynamicAttribute($attributeSlug) {
105 3
        if(!isset($this->dynamicAttributes[$attributeSlug])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
106 1
            throw new \InvalidArgumentException("The dynamic value for attribute $attributeSlug was not given");
107
        }
108 2
        return $this->dynamicAttributes[$attributeSlug];
109
    }
110
111
    /**
112
     * @param string $type
113
     * @param string $class
114
     */
115
    public function addComparison($type, $class) {
116
        $this->comparisons[$type] = $class;
117
    }
118
119
    /**
120
     * @return \PhpAbac\Manager\AttributeManager
121
     */
122 6
    public function getAttributeManager() {
123 6
        return $this->attributeManager;
124
    }
125
126
    /**
127
     * This method is called when all the policy rule attributes are checked
128
     * All along the comparisons, the failing attributes slugs are stored
129
     * If the rejected attributes array is not empty, it means that the rule is not enforced
130
     *
131
     * @return array|bool
132
     */
133 6
    public function getResult() {
134
        $result =
135 6
            (count($this->rejectedAttributes) > 0)
136 6
            ? $this->rejectedAttributes
137 5
            : true
138 6
        ;
139 6
        $this->rejectedAttributes = [];
140 6
        return $result;
141
    }
142
}
143