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

Group::formatting()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.2163

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 5
nop 4
dl 0
loc 20
ccs 9
cts 11
cp 0.8182
crap 6.2163
rs 9.2222
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\Styles\AffixesTrait;
13
use Seboettg\CiteProc\Styles\ConsecutivePunctuationCharacterTrait;
14
use Seboettg\CiteProc\Styles\DelimiterTrait;
15
use Seboettg\CiteProc\Styles\DisplayTrait;
16
use Seboettg\CiteProc\Styles\FormattingTrait;
17
use Seboettg\CiteProc\Util\Factory;
18
use Seboettg\Collection\ArrayList;
19
20
21
/**
22
 * Class Group
23
 * The cs:group rendering element must contain one or more rendering elements (with the exception of cs:layout).
24
 * cs:group may carry the delimiter attribute to separate its child elements, as well as affixes and display attributes
25
 * (applied to the output of the group as a whole) and formatting attributes (transmitted to the enclosed elements).
26
 * cs:group implicitly acts as a conditional: cs:group and its child elements are suppressed if a) at least one
27
 * rendering element in cs:group calls a variable (either directly or via a macro), and b) all variables that are
28
 * called are empty. This accommodates descriptive cs:text elements.
29
 *
30
 * @package Seboettg\CiteProc\Rendering
31
 *
32
 * @author Sebastian Böttger <[email protected]>
33
 */
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...
34
class Group implements Rendering, HasParent
35
{
36
    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...
37
        AffixesTrait,
38
        DisplayTrait,
39
        FormattingTrait,
40
        ConsecutivePunctuationCharacterTrait;
41
42
    const CLASS_PATH = 'Seboettg\CiteProc\Rendering';
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 66
    public function __construct(\SimpleXMLElement $node, $parent)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
64
    {
65 66
        $this->parent = $parent;
66 66
        $this->children = new ArrayList();
67 66
        foreach ($node->children() as $child) {
68 66
            $this->children->append(Factory::create($child, $this));
69
        }
70 66
        $this->initDisplayAttributes($node);
71 66
        $this->initAffixesAttributes($node);
72 66
        $this->initDelimiterAttributes($node);
73 66
        $this->initFormattingAttributes($node);
74 66
    }
75
76
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $data should have a doc-comment as per coding-style.
Loading history...
77
     * @param $data
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
78
     * @param int|null $citationNumber
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...
79
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
80
     */
81 59
    public function render($data, $citationNumber = null)
82
    {
83 59
        $textParts = array();
84 59
        $terms = $variables = $haveVariables = $elementCount = 0;
85 59
        foreach ($this->children as $child) {
86 59
            $elementCount++;
87
88 59
            if (($child instanceof Text) &&
89 40
                ($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...
90 40
                    $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...
91 21
                ++$terms;
92
            }
93
94 59
            if (($child instanceof Label)) {
95 11
                ++$terms;
96
            }
97 59
            if (method_exists($child, "getSource") && $child->getSource() == 'variable' &&
98 35
                !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...
99 35
                !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...
100
            ) {
101 26
                ++$variables;
102
            }
103
104 59
            $text = $child->render($data, $citationNumber);
105 59
            $delimiter = $this->delimiter;
106 59
            if (!empty($text)) {
107 58
                if ($delimiter && ($elementCount < count($this->children))) {
108
                    //check to see if the delimiter is already the last character of the text string
109
                    //if so, remove it so we don't have two of them when the group will be merged
110 54
                    $stext = strip_tags(trim($text));
111 54
                    if ((strrpos($stext, $delimiter[0]) + 1) == strlen($stext) && strlen($stext) > 1) {
112 10
                        $text = str_replace($stext, '----REPLACE----', $text);
113 10
                        $stext = substr($stext, 0, -1);
114 10
                        $text = str_replace('----REPLACE----', $stext, $text);
115
                    }
116
                }
117 58
                $textParts[] = $text;
118
119 58
                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...
120 57
                   (method_exists($child, "getVariable") && $child->getVariable() != "date" && !empty($child->getVariable()))) {
0 ignored issues
show
Coding Style introduced by
Multi-line IF statement not indented correctly; expected 20 spaces but found 19
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...
121
122 48
                    $haveVariables++;
123
                }
124
125 58
                if (method_exists($child, "getSource") && $child->getSource() == 'macro') {
126 27
                    $haveVariables++;
127
                }
128
            }
129
        }
130 59
        return $this->formatting($textParts, $variables, $haveVariables, $terms);
131
    }
132
133
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
134
     * @return mixed
135
     */
136 9
    public function getParent()
137
    {
138 9
        return $this->parent;
139
    }
140
141
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $textParts should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $variables should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $haveVariables should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $terms should have a doc-comment as per coding-style.
Loading history...
142
     * @param $textParts
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
143
     * @param $variables
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
144
     * @param $haveVariables
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
145
     * @param $terms
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
146
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
147
     */
148 59
    protected function formatting($textParts, $variables, $haveVariables, $terms)
149
    {
150 59
        if (empty($textParts)) {
151 15
            return "";
152
        }
153
154 58
        if ($variables && !$haveVariables) {
155
            return ""; // there has to be at least one other none empty value before the term is output
156
        }
157
158 58
        if (count($textParts) == $terms) {
159 11
            return ""; // there has to be at least one other none empty value before the term is output
160
        }
161
162 58
        $text = implode($this->delimiter, $textParts); // insert the delimiter if supplied.
163 58
        if (!empty($text)) {
164 58
            return $this->wrapDisplayBlock($this->addAffixes($this->format(($text))));
165
        }
166
167
        return "";
168
    }
169
}
170