Passed
Push — master ( 661b73...ccb1dd )
by Sebastian
08:54 queued 05:17
created

Choose   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 81
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getParent() 0 3 1
A __construct() 0 20 6
B render() 0 27 9
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
12
use Seboettg\CiteProc\Data\DataList;
13
use Seboettg\CiteProc\Exception\ClassNotFoundException;
14
use Seboettg\CiteProc\Exception\InvalidStylesheetException;
15
use Seboettg\CiteProc\Rendering\HasParent;
16
use Seboettg\CiteProc\Rendering\Rendering;
17
use Seboettg\Collection\ArrayList;
18
use SimpleXMLElement;
19
20
21
/**
22
 * Class Choose
23
 * @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...
24
 *
25
 * @author Sebastian Böttger <[email protected]>
26
 */
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...
27
class Choose implements Rendering, HasParent
28
{
29
30
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
     * @var ArrayList
32
     */
33
    private $children;
0 ignored issues
show
Coding Style introduced by
Private member variable "children" must be prefixed with an underscore
Loading history...
34
35
    private $parent;
0 ignored issues
show
Coding Style introduced by
Private member variable "parent" must be prefixed with an underscore
Loading history...
36
37
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $parent should have a doc-comment as per coding-style.
Loading history...
38
     * Choose constructor.
39
     * @param SimpleXMLElement $node
3 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...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
40
     * @param $parent
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
41
     * @throws ClassNotFoundException
1 ignored issue
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
42
     * @throws InvalidStylesheetException
1 ignored issue
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
43
     */
44 40
    public  function __construct(SimpleXMLElement $node, $parent)
45
    {
46 40
        $this->parent = $parent;
47 40
        $this->children = new ArrayList();
48 40
        $elseIf = [];
49 40
        foreach ($node->children() as $child) {
50 40
            switch ($child->getName()) {
51 40
                case 'if':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
52 40
                    $this->children->add("if", new ChooseIf($child, $this));
53 40
                    break;
54 39
                case 'else-if':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
55 33
                    $elseIf[] = new ChooseElseIf($child, $this);
56 33
                    break;
57 39
                case 'else':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
58 39
                    $this->children->add("else", new ChooseElse($child, $this));
59 40
                    break;
60
            }
61
        }
62 40
        if (!empty($elseIf)) {
63 33
            $this->children->add("elseif", $elseIf);
64
        }
65 40
    }
66
67
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
68
     * @param array|DataList $data
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
69
     * @param null|int $citationNumber
3 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
70
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
71
     */
72 34
    public function render($data, $citationNumber = null)
73
    {
74 34
        $arr = [];
75
76
        // IF
77 34
        if ($prevCondition = $this->children->get("if")->match($data)) {
78 32
            $arr[] = $this->children->get("if")->render($data);
79
80 32
        } else if (!$prevCondition && $this->children->hasKey("elseif")) { // ELSEIF
81
            /** @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...
82 21
            foreach ($this->children->get("elseif") as $child) {
83 21
                $condition = $child->match($data);
84 21
                if ($condition && !$prevCondition) {
85 12
                    $arr[] = $child->render($data);
86 12
                    $prevCondition = true;
87 12
                    break; //break loop as soon as condition matches
88
                }
89 21
                $prevCondition = $condition;
90
            }
91
        }
92
93
        //ELSE
94 34
        if (!$prevCondition && $this->children->hasKey("else")) {
95 30
            $arr[] = $this->children->get("else")->render($data);
96
97
        }
98 34
        return implode("", $arr);
99
    }
100
101
102
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
103
     * @return mixed
104
     */
105 5
    public function getParent()
106
    {
107 5
        return $this->parent;
108
    }
109
}
110
111