Passed
Push — master ( 9db44e...ff01a4 )
by Sebastian
03:38 queued 10s
created

Group   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Test Coverage

Coverage 96.61%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 62
c 1
b 0
f 0
dl 0
loc 156
ccs 57
cts 59
cp 0.9661
rs 9.68
wmc 34

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getParent() 0 3 1
A __construct() 0 11 2
A formatting() 0 20 6
D render() 0 52 23
A getDelimiter() 0 3 1
A hasDelimiter() 0 3 1
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;
11
12
use Seboettg\CiteProc\Exception\InvalidStylesheetException;
13
use Seboettg\CiteProc\Styles\AffixesTrait;
14
use Seboettg\CiteProc\Styles\ConsecutivePunctuationCharacterTrait;
15
use Seboettg\CiteProc\Styles\DelimiterTrait;
16
use Seboettg\CiteProc\Styles\DisplayTrait;
17
use Seboettg\CiteProc\Styles\FormattingTrait;
18
use Seboettg\CiteProc\Util\Factory;
19
use Seboettg\Collection\ArrayList;
20
use SimpleXMLElement;
21
use stdClass;
22
23
/**
24
 * Class Group
25
 * The cs:group rendering element must contain one or more rendering elements (with the exception of cs:layout).
26
 * cs:group may carry the delimiter attribute to separate its child elements, as well as affixes and display attributes
27
 * (applied to the output of the group as a whole) and formatting attributes (transmitted to the enclosed elements).
28
 * cs:group implicitly acts as a conditional: cs:group and its child elements are suppressed if a) at least one
29
 * rendering element in cs:group calls a variable (either directly or via a macro), and b) all variables that are
30
 * called are empty. This accommodates descriptive cs:text elements.
31
 *
32
 * @package Seboettg\CiteProc\Rendering
33
 *
34
 * @author Sebastian Böttger <[email protected]>
35
 */
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...
36
class Group implements Rendering, HasParent
37
{
38
    use DelimiterTrait,
0 ignored issues
show
Bug introduced by
The trait Seboettg\CiteProc\Styles\AffixesTrait requires the property $single which is not provided by Seboettg\CiteProc\Rendering\Group.
Loading history...
39
        AffixesTrait,
40
        DisplayTrait,
41
        FormattingTrait,
42
        ConsecutivePunctuationCharacterTrait;
43
44
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @var ArrayList
46
     */
47
    private $children;
0 ignored issues
show
Coding Style introduced by
Private member variable "children" must be prefixed with an underscore
Loading history...
48
49
    /**
50
     * cs:group may carry the delimiter attribute to separate its child elements
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
51
     * @var
1 ignored issue
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
52
     */
53
    private $delimiter = "";
0 ignored issues
show
Coding Style introduced by
Private member variable "delimiter" must be prefixed with an underscore
Loading history...
54
55
    private $parent;
0 ignored issues
show
Coding Style introduced by
Private member variable "parent" must be prefixed with an underscore
Loading history...
56
57
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
58
     * @var array
59
     */
60
    private $renderedChildsWithVariable = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "renderedChildsWithVariable" must be prefixed with an underscore
Loading history...
61
62
63
    /**
64
     * Group constructor.
65
     * @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...
66
     * @param $parent
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...
67
     * @throws InvalidStylesheetException
1 ignored issue
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
68
     */
69 73
    public function __construct(SimpleXMLElement $node, $parent)
70
    {
71 73
        $this->parent = $parent;
72 73
        $this->children = new ArrayList();
73 73
        foreach ($node->children() as $child) {
74 73
            $this->children->append(Factory::create($child, $this));
75
        }
76 73
        $this->initDisplayAttributes($node);
77 73
        $this->initAffixesAttributes($node);
78 73
        $this->initDelimiterAttributes($node);
79 73
        $this->initFormattingAttributes($node);
80 73
    }
81
82
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
83
     * @param $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...
84
     * @param int|null $citationNumber
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...
85
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
86
     */
87 66
    public function render($data, $citationNumber = null)
88
    {
89 66
        $textParts = array();
90 66
        $terms = $variables = $haveVariables = $elementCount = 0;
91 66
        foreach ($this->children as $child) {
92 66
            $elementCount++;
93
94 66
            if (($child instanceof Text) &&
95 46
                ($child->getSource() == 'term' ||
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
96 46
                    $child->getSource() == 'value')) {
0 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 16 spaces but found 20
Loading history...
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
97 26
                ++$terms;
98
            }
99
100 66
            if (($child instanceof Label)) {
101 12
                ++$terms;
102
            }
103 66
            if (method_exists($child, "getSource") && $child->getSource() == 'variable' &&
104 39
                !empty($child->getVariable()) && $child->getVariable() != "date" &&
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
105 39
                !empty($data->{$child->getVariable()})
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
106
            ) {
107 29
                ++$variables;
108
            }
109
110
            /** @var stdClass $data */
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...
111 66
            $text = $child->render($data, $citationNumber);
112 66
            $delimiter = $this->delimiter;
113 66
            if (!empty($text)) {
114 65
                if ($delimiter && ($elementCount < count($this->children))) {
115
                    //check to see if the delimiter is already the last character of the text string
116
                    //if so, remove it so we don't have two of them when the group will be merged
117 58
                    $stext = strip_tags(trim($text));
118 58
                    if ((strrpos($stext, $delimiter[0]) + 1) == strlen($stext) && strlen($stext) > 1) {
119 12
                        $text = str_replace($stext, '----REPLACE----', $text);
120 12
                        $stext = substr($stext, 0, -1);
121 12
                        $text = str_replace('----REPLACE----', $stext, $text);
122
                    }
123
                }
124 65
                $textParts[] = $text;
125
126 65
                if (method_exists($child, "getSource") && $child->getSource() == 'variable' ||
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: (method_exists($child, '...($child->getVariable()), Probably Intended Meaning: method_exists($child, 'g...$child->getVariable()))
Loading history...
127 64
                    (method_exists($child,
0 ignored issues
show
Coding Style introduced by
Each line in a multi-line IF statement must begin with a boolean operator
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
128 64
                            "getVariable") && $child->getVariable() != "date" && !empty($child->getVariable()))) {
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 28.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
Coding Style introduced by
Closing parenthesis of a multi-line IF statement must be on a new line
Loading history...
129
130 54
                    $haveVariables++;
131
                }
132
133 65
                if (method_exists($child, "getSource") && $child->getSource() == 'macro') {
134 29
                    $haveVariables++;
135
                }
136
            }
137
        }
138 66
        return $this->formatting($textParts, $variables, $haveVariables, $terms);
139
    }
140
141
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
142
     * @return mixed
143
     */
144 10
    public function getParent()
145
    {
146 10
        return $this->parent;
147
    }
148
149
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
150
     * @param $textParts
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...
151
     * @param $variables
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...
152
     * @param $haveVariables
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...
153
     * @param $terms
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...
154
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
155
     */
156 66
    protected function formatting($textParts, $variables, $haveVariables, $terms)
157
    {
158 66
        if (empty($textParts)) {
159 20
            return "";
160
        }
161
162 65
        if ($variables && !$haveVariables) {
163
            return ""; // there has to be at least one other none empty value before the term is output
164
        }
165
166 65
        if (count($textParts) == $terms) {
167 16
            return ""; // there has to be at least one other none empty value before the term is output
168
        }
169
170 63
        $text = implode($this->delimiter, $textParts); // insert the delimiter if supplied.
171 63
        if (!empty($text)) {
172 63
            return $this->wrapDisplayBlock($this->addAffixes($this->format(($text))));
173
        }
174
175
        return "";
176
    }
177
178
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
179
     * @return bool
180
     */
181 17
    public function hasDelimiter()
182
    {
183 17
        return !empty($this->delimiter);
184
    }
185
186
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
187
     * @return string
188
     */
189 59
    public function getDelimiter()
190
    {
191 59
        return $this->delimiter;
192
    }
193
}
194