Passed
Push — new-api ( 18d26d...074931 )
by Sebastian
04:59
created

Choose::setChildren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Data\DataList;
14
use Seboettg\CiteProc\Exception\ClassNotFoundException;
15
use Seboettg\CiteProc\Exception\InvalidStylesheetException;
16
use Seboettg\CiteProc\Rendering\HasParent;
17
use Seboettg\CiteProc\Rendering\Rendering;
18
use Seboettg\Collection\ArrayList;
19
use Seboettg\Collection\ArrayList\ArrayListInterface;
20
use SimpleXMLElement;
21
22
class Choose implements Rendering, HasParent
23
{
24
25
    /**
26
     * @var ArrayList
27
     */
28
    private $children;
29
30
    private $parent;
31
32
    /**
33
     * @param SimpleXMLElement $node
34
     * @param null $parent
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $parent is correct as it would always require null to be passed?
Loading history...
35
     * @return Choose
36
     * @throws ClassNotFoundException
37
     * @throws InvalidStylesheetException
38
     */
39 63
    public static function factory(SimpleXMLElement $node, $parent = null): Choose
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$parent" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$parent"; expected 0 but found 1
Loading history...
40
    {
41 63
        $choose = new Choose($parent);
42 63
        $children = new ArrayList();
43 63
        $elseIf = [];
44 63
        foreach ($node->children() as $child) {
45 63
            switch ($child->getName()) {
46 63
                case 'if':
47 63
                    $children->add("if", ChooseIf::factory($child, $choose));
48 63
                    break;
49 61
                case 'else-if':
50 47
                    $elseIf[] = ChooseElseIf::factory($child, $choose);
51 47
                    break;
52 61
                case 'else':
53 61
                    $children->add("else", ChooseElse::factory($child, $choose));
54 63
                    break;
55
            }
56
        }
57 63
        if (!empty($elseIf)) {
58 47
            $children->add("elseif", $elseIf);
59
        }
60 63
        $choose->setChildren($children);
61 63
        return $choose;
62
    }
63
64 63
    public function __construct($parent)
65
    {
66 63
        $this->parent = $parent;
67 63
    }
68
69
    /**
70
     * @param  array|DataList $data
71
     * @param  null|int       $citationNumber
72
     * @return mixed
73
     */
74 56
    public function render($data, $citationNumber = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$citationNumber" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$citationNumber"; expected 0 but found 1
Loading history...
75
    {
76 56
        $arr = [];
77
78
        // IF
79 56
        if ($prevCondition = $this->children->get("if")->match($data)) {
80 48
            $arr[] = $this->children->get("if")->render($data);
81 55
        } elseif (!$prevCondition && $this->children->hasKey("elseif")) { // ELSEIF
82
            /**
83
             * @var ChooseElseIf $child
84
             */
85 32
            foreach ($this->children->get("elseif") as $child) {
86 32
                $condition = $child->match($data);
87 32
                if ($condition && !$prevCondition) {
88 22
                    $arr[] = $child->render($data);
89 22
                    $prevCondition = true;
90 22
                    break; //break loop as soon as condition matches
91
                }
92 31
                $prevCondition = $condition;
93
            }
94
        }
95
96
        //ELSE
97 56
        if (!$prevCondition && $this->children->hasKey("else")) {
98 51
            $arr[] = $this->children->get("else")->render($data);
99
        }
100 56
        return implode("", $arr);
101
    }
102
103
    /**
104
     * @return mixed
105
     */
106 56
    public function getParent()
107
    {
108 56
        return $this->parent;
109
    }
110
111 39
    public function setParent($parent)
112
    {
113 39
        $this->parent = $parent;
114 39
    }
115
116 63
    private function setChildren(ArrayListInterface $children)
117
    {
118 63
        $this->children = $children;
0 ignored issues
show
Documentation Bug introduced by
$children is of type Seboettg\Collection\ArrayList\ArrayListInterface, but the property $children was declared to be of type Seboettg\Collection\ArrayList. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
119 63
    }
120
}
121