Passed
Push — master ( d2184e...ee1eb5 )
by Sebastian
04:46
created

Choose::__construct()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 10
nop 2
dl 0
loc 20
ccs 17
cts 17
cp 1
crap 6
rs 9.1111
c 0
b 0
f 0
1
<?php
2
/*
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Rendering\Choose;
11
use Seboettg\CiteProc\Rendering\HasParent;
12
use Seboettg\CiteProc\Rendering\Rendering;
13
use Seboettg\CiteProc\Rendering\Text;
14
use Seboettg\CiteProc\Style\Macro;
15
use Seboettg\Collection\ArrayList;
16
17
18
/**
19
 * Class Choose
20
 * @package Seboettg\CiteProc\Node
1 ignored issue
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
21
 *
22
 * @author Sebastian Böttger <[email protected]>
23
 */
3 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
24
class Choose implements Rendering, HasParent
25
{
26
27
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
28
     * @var ArrayList
29
     */
30
    private $children;
0 ignored issues
show
Coding Style introduced by
Private member variable "children" must be prefixed with an underscore
Loading history...
31
32
    private $parent;
0 ignored issues
show
Coding Style introduced by
Private member variable "parent" must be prefixed with an underscore
Loading history...
33
34
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $parent should have a doc-comment as per coding-style.
Loading history...
35
     * Choose constructor.
36
     * @param \SimpleXMLElement $node
2 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
37
     * @param $parent
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
38
     */
39 39
    public  function __construct(\SimpleXMLElement $node, $parent)
40
    {
41 39
        $this->parent = $parent;
42 39
        $this->children = new ArrayList();
43 39
        $elseIf = [];
44 39
        foreach ($node->children() as $child) {
45 39
            switch ($child->getName()) {
46 39
                case 'if':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
47 39
                    $this->children->add("if", new ChooseIf($child, $this));
48 39
                    break;
49 38
                case 'else-if':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
50 32
                    $elseIf[] = new ChooseElseIf($child, $this);
51 32
                    break;
52 38
                case 'else':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
53 38
                    $this->children->add("else", new ChooseElse($child, $this));
54 39
                    break;
55
            }
56
        }
57 39
        if (!empty($elseIf)) {
58 32
            $this->children->add("elseif", $elseIf);
59
        }
60 39
    }
61
62
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
63
     * @param array|\Seboettg\CiteProc\Data\DataList $data
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
64
     * @param null|int $citationNumber
3 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 31 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
65
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
66
     */
67 33
    public function render($data, $citationNumber = null)
68
    {
69 33
        $arr = [];
70
71
        // IF
72 33
        if ($prevCondition = $this->children->get("if")->match($data)) {
73 31
            $arr[] = $this->children->get("if")->render($data);
74
75 31
        } else if (!$prevCondition && $this->children->hasKey("elseif")) { // ELSEIF
76
            /** @var ChooseElseIf $child */
3 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
77 20
            foreach ($this->children->get("elseif") as $child) {
78 20
                $condition = $child->match($data);
79 20
                if ($condition && !$prevCondition) {
80 12
                    $arr[] = $child->render($data);
81 12
                    $prevCondition = true;
82 12
                    break; //break loop as soon as condition matches
83
                }
84 20
                $prevCondition = $condition;
85
            }
86
        }
87
88
        //ELSE
89 33
        if (!$prevCondition && $this->children->hasKey("else")) {
90 29
            $arr[] = $this->children->get("else")->render($data);
91
92
        }
93 33
        return implode("", $arr);
94
    }
95
96
97
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
98
     * @return mixed
99
     */
100 1
    public function getParent()
101
    {
102 1
        return $this->parent;
103
    }
104
}
105
106