Completed
Pull Request — master (#1)
by Christian
04:52 queued 01:12
created

AbstractRule::hasPrerequisites()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RemotelyLiving\Doorkeeper\Rules;
6
7
use RemotelyLiving\Doorkeeper\Identification;
8
use RemotelyLiving\Doorkeeper\Requestor;
9
10
abstract class AbstractRule implements RuleInterface
11
{
12
    /**
13
     * @var \RemotelyLiving\Doorkeeper\Rules\RuleInterface[]
14
     */
15
    private $prerequisites = [];
16
17
    final public function addPrerequisite(RuleInterface $rule): void
18
    {
19
        $this->prerequisites[] = $rule;
20
    }
21
22
    final public function hasPrerequisites(): bool
23
    {
24
        return (bool) $this->prerequisites;
25
    }
26
27
    final public function getPrerequisites(): iterable
28
    {
29
        return $this->prerequisites;
30
    }
31
32
    public function getValue()
33
    {
34
        return null;
35
    }
36
37
    final public function canBeSatisfied(Requestor $requestor = null): bool
38
    {
39
        if ($this->hasPrerequisites()) {
40
            foreach ($this->prerequisites as $prerequisite) {
41
                if (!$prerequisite->canBeSatisfied($requestor)) {
42
                    return false;
43
                }
44
            }
45
        }
46
47
        return $this->childCanBeSatisfied($requestor);
48
    }
49
50
    public function jsonSerialize(): array
51
    {
52
        return [
53
            'type' => static::class,
54
            'value' => $this->getValue(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getValue() targeting RemotelyLiving\Doorkeepe...bstractRule::getValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
55
            'prerequisites' => $this->prerequisites,
56
        ];
57
    }
58
59
    protected function requestorHasMatchingId(
60
        Requestor $requestor = null,
61
        Identification\IdentificationInterface $identification
62
    ): bool {
63
        if (!$requestor) {
64
            return false;
65
        }
66
67
        return $requestor->hasIdentification($identification);
68
    }
69
70
    abstract protected function childCanBeSatisfied(Requestor $requestor = null): bool;
71
}
72