Passed
Push — issue-70 ( ae9446 )
by Sebastian
05:29
created

Group::hasDelimiter()   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
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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;
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
/**
25
 * Class Group
26
 * The cs:group rendering element must contain one or more rendering elements (with the exception of cs:layout).
27
 * cs:group may carry the delimiter attribute to separate its child elements, as well as affixes and display attributes
28
 * (applied to the output of the group as a whole) and formatting attributes (transmitted to the enclosed elements).
29
 * cs:group implicitly acts as a conditional: cs:group and its child elements are suppressed if a) at least one
30
 * rendering element in cs:group calls a variable (either directly or via a macro), and b) all variables that are
31
 * called are empty. This accommodates descriptive cs:text elements.
32
 *
33
 * @package Seboettg\CiteProc\Rendering
34
 *
35
 * @author Sebastian Böttger <[email protected]>
36
 */
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...
37
class Group implements Rendering, HasParent
38
{
39
    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...
40
        AffixesTrait,
41
        DisplayTrait,
42
        FormattingTrait,
43
        ConsecutivePunctuationCharacterTrait;
44
45
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     * @var ArrayList
47
     */
48
    private $children;
0 ignored issues
show
Coding Style introduced by
Private member variable "children" must be prefixed with an underscore
Loading history...
49
50
    /**
51
     * 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...
52
     * @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...
53
     */
54
    private $delimiter = "";
0 ignored issues
show
Coding Style introduced by
Private member variable "delimiter" must be prefixed with an underscore
Loading history...
55
56
    private $parent;
0 ignored issues
show
Coding Style introduced by
Private member variable "parent" must be prefixed with an underscore
Loading history...
57
58
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
59
     * @var array
60
     */
61
    private $renderedChildsWithVariable = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "renderedChildsWithVariable" must be prefixed with an underscore
Loading history...
62
63
64
    /**
65
     * Group constructor.
66
     * @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...
67
     * @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...
68
     * @throws InvalidStylesheetException
1 ignored issue
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
69
     */
70 73
    public function __construct(SimpleXMLElement $node, $parent)
71
    {
72 73
        $this->parent = $parent;
73 73
        $this->children = new ArrayList();
74 73
        foreach ($node->children() as $child) {
75 73
            $this->children->append(Factory::create($child, $this));
76
        }
77 73
        $this->initDisplayAttributes($node);
78 73
        $this->initAffixesAttributes($node);
79 73
        $this->initDelimiterAttributes($node);
80 73
        $this->initFormattingAttributes($node);
81 73
    }
82
83
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
84
     * @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...
85
     * @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...
86
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
87
     */
88 66
    public function render($data, $citationNumber = null)
89
    {
90 66
        $textParts = array();
91 66
        $terms = $variables = $haveVariables = $elementCount = 0;
92 66
        foreach ($this->children as $child) {
93 66
            $elementCount++;
94
95 66
            if (($child instanceof Text) &&
96 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...
97 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...
98 26
                ++$terms;
99
            }
100
101 66
            if (($child instanceof Label)) {
102 12
                ++$terms;
103
            }
104 66
            if (method_exists($child, "getSource") && $child->getSource() == 'variable' &&
105 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...
106 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...
107
            ) {
108 29
                ++$variables;
109
            }
110
111
            /** @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...
112 66
            $text = $child->render($data, $citationNumber);
113 66
            $delimiter = $this->delimiter;
114 66
            if (!empty($text)) {
115 65
                if ($delimiter && ($elementCount < count($this->children))) {
116
                    //check to see if the delimiter is already the last character of the text string
117
                    //if so, remove it so we don't have two of them when the group will be merged
118 58
                    $stext = strip_tags(trim($text));
119 58
                    if ((strrpos($stext, $delimiter[0]) + 1) == strlen($stext) && strlen($stext) > 1) {
120 12
                        $text = str_replace($stext, '----REPLACE----', $text);
121 12
                        $stext = substr($stext, 0, -1);
122 12
                        $text = str_replace('----REPLACE----', $stext, $text);
123
                    }
124
                }
125 65
                $textParts[] = $text;
126
127 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...
128 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...
129 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...
130
131 54
                    $haveVariables++;
132
                }
133
134 65
                if (method_exists($child, "getSource") && $child->getSource() == 'macro') {
135 29
                    $haveVariables++;
136
                }
137
            }
138
        }
139 66
        return $this->formatting($textParts, $variables, $haveVariables, $terms);
140
    }
141
142
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
143
     * @return mixed
144
     */
145 10
    public function getParent()
146
    {
147 10
        return $this->parent;
148
    }
149
150
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
151
     * @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...
152
     * @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...
153
     * @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...
154
     * @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...
155
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
156
     */
157 66
    protected function formatting($textParts, $variables, $haveVariables, $terms)
158
    {
159 66
        if (empty($textParts)) {
160 20
            return "";
161
        }
162
163 65
        if ($variables && !$haveVariables) {
164
            return ""; // there has to be at least one other none empty value before the term is output
165
        }
166
167 65
        if (count($textParts) == $terms) {
168 16
            return ""; // there has to be at least one other none empty value before the term is output
169
        }
170
171 63
        $text = implode($this->delimiter, $textParts); // insert the delimiter if supplied.
172 63
        if (!empty($text)) {
173 63
            return $this->wrapDisplayBlock($this->addAffixes($this->format(($text))));
174
        }
175
176
        return "";
177
    }
178
179 17
    public function hasDelimiter()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function hasDelimiter()
Loading history...
180
    {
181 17
        return !empty($this->delimiter);
182
    }
183
184 59
    public function getDelimiter()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getDelimiter()
Loading history...
185
    {
186 59
        return $this->delimiter;
187
    }
188
}
189