ChooseIf::render()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php
5
 *
6
 * @link        http://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc\Rendering\Choose;
12
13
use Seboettg\CiteProc\Constraint\Constraint;
14
use Seboettg\CiteProc\Constraint\Factory;
15
use Seboettg\CiteProc\Data\DataList;
16
use Seboettg\CiteProc\Exception\ClassNotFoundException;
17
use Seboettg\CiteProc\Exception\InvalidStylesheetException;
18
use Seboettg\CiteProc\Rendering\Group;
19
use Seboettg\CiteProc\Rendering\HasParent;
20
use Seboettg\CiteProc\Rendering\Rendering;
21
use Seboettg\Collection\ArrayList;
22
use SimpleXMLElement;
23
use function Seboettg\Collection\Lists\emptyList;
24
25
class ChooseIf implements Rendering, HasParent
26
{
27
    /**
28
     * @var ArrayList<Constraint>|Constraint[]
29
     */
30
    private $constraints;
31
32
    /**
33
     * @var ArrayList
34
     */
35
    protected $children;
36
37
    /**
38
     * @var string
39
     */
40
    private $match;
41
42
    /**
43
     * @var
44
     */
45
    protected $parent;
46
    /**
47
     * @param SimpleXMLElement $node
48
     * @param Choose $parent
49
     * @throws InvalidStylesheetException
50
     * @throws ClassNotFoundException
51
     */
52
    public function __construct(SimpleXMLElement $node, Choose $parent)
53
    {
54
        $this->parent = $parent;
55
        $this->constraints = emptyList();
0 ignored issues
show
Documentation Bug introduced by
It seems like emptyList() of type anonymous//vendor/seboet...c/Lists/Functions.php$0 is incompatible with the declared type Seboettg\Collection\ArrayList of property $constraints.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
        $this->children = emptyList();
0 ignored issues
show
Documentation Bug introduced by
It seems like emptyList() of type anonymous//vendor/seboet...c/Lists/Functions.php$0 is incompatible with the declared type Seboettg\Collection\ArrayList of property $children.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
57
        $this->match = (string) $node["match"];
58
        if (empty($this->match)) {
59
            $this->match = Constraint::MATCH_ALL;
60
        }
61
        foreach ($node->attributes() as $name => $value) {
62
            if ("match" !== $name) {
63
                $this->constraints->add(Factory::createConstraint((string) $name, (string) $value, $this->match));
64
            }
65
        }
66
        foreach ($node->children() as $child) {
67
            $this->children->add(Factory::create($child, $this));
68
        }
69
    }
70
    /**
71
     * @param array|DataList $data
72
     * @param null|int $citationNumber
73
     * @return string
74
     */
75
    public function render($data, $citationNumber = null): string
76
    {
77
        $ret = [];
78
        /** @var Rendering $child */
79
        foreach ($this->children as $child) {
80
            $ret[] = $child->render($data, $citationNumber);
81
        }
82
        $glue = "";
83
        $parent = $this->parent->getParent();
84
        if ($parent instanceof Group && $parent->hasDelimiter()) {
85
            $glue = $parent->getDelimiter();
86
        }
87
        return implode($glue, array_filter($ret));
88
    }
89
    /**
90
     * @param $data
91
     * @param null|int $citationNumber
92
     * @return bool
93
     */
94
    public function match($data, int $citationNumber = null): bool
0 ignored issues
show
Unused Code introduced by
The parameter $citationNumber is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

94
    public function match($data, /** @scrutinizer ignore-unused */ int $citationNumber = null): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
    {
96
        if ($this->constraints->count() === 1) {
97
            return $this->constraints->current()->validate($data);
98
        }
99
100
        switch ($this->match) {
101
            case Constraint::MATCH_ANY:
102
                return $this->constraints
103
                    ->map(fn (Constraint $constraint) => $constraint->validate($data))
104
                    ->filter(fn (bool $match) => $match == true)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
105
                    ->count() > 0;
106
            case Constraint::MATCH_ALL:
107
                return $this->constraints
108
                    ->map(fn (Constraint $constraint) => $constraint->validate($data))
109
                    ->filter(fn (bool $match) => $match == true)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
110
                    ->count() == $this->constraints->count();
111
            case Constraint::MATCH_NONE:
112
                return !$this->constraints
113
                    ->map(fn (Constraint $constraint) => $constraint->validate($data))
114
                    ->filter(fn (bool $match) => $match == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
115
                    ->count() == $this->constraints->count();
116
        }
117
        return false;
118
    }
119
120
121
    /**
122
     * @noinspection PhpUnused
123
     * @return Choose
124
     */
125
    public function getParent(): Choose
126
    {
127
        return $this->parent;
128
    }
129
}
130