Completed
Push — master ( e09500...86ebd4 )
by Sebastian
02:59
created

Group::render()   C

Complexity

Conditions 22
Paths 201

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
eloc 34
nc 201
nop 2
dl 0
loc 53
rs 5.6266
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
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
use Seboettg\CiteProc\Styles\AffixesTrait;
12
use Seboettg\CiteProc\Styles\ConsecutivePunctuationCharacterTrait;
13
use Seboettg\CiteProc\Styles\DelimiterTrait;
14
use Seboettg\CiteProc\Styles\DisplayTrait;
15
use Seboettg\CiteProc\Styles\FormattingTrait;
16
use Seboettg\CiteProc\Util\Factory;
17
use Seboettg\Collection\ArrayList;
18
19
20
/**
21
 * Class Group
22
 * @package Seboettg\CiteProc\Rendering
23
 *
24
 * @author Sebastian Böttger <[email protected]>
25
 */
26
class Group implements RenderingInterface, HasParent
27
{
28
    use DelimiterTrait,
29
        AffixesTrait,
30
        DisplayTrait,
31
        FormattingTrait,
32
        ConsecutivePunctuationCharacterTrait;
33
34
    const CLASS_PATH = 'Seboettg\CiteProc\Rendering';
35
36
    private static $suppressableElements = [
0 ignored issues
show
Unused Code introduced by
The property $suppressableElements is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
37
        self::CLASS_PATH . '\Number',
38
        self::CLASS_PATH . '\Group',
39
        self::CLASS_PATH . '\Date\Date'
40
    ]; 
41
42
    /**
43
     * @var ArrayList
44
     */
45
    private $children;
46
47
    /**
48
     * cs:group may carry the delimiter attribute to separate its child elements
49
     * @var
50
     */
51
    private $delimiter = "";
52
53
    private $parent;
54
55
    public function __construct(\SimpleXMLElement $node, $parent)
56
    {
57
        $this->parent = $parent;
58
        $this->children = new ArrayList();
59
        foreach ($node->children() as $child) {
60
            $this->children->append(Factory::create($child, $this));
61
        }
62
        $this->initDisplayAttributes($node);
63
        $this->initAffixesAttributes($node);
64
        $this->initDelimiterAttributes($node);
65
        $this->initFormattingAttributes($node);
66
    }
67
68
    /**
69
     * @param $data
70
     * @param int|null $citationNumber
71
     * @return string
72
     */
73
    public function render($data, $citationNumber = null)
74
    {
75
        $textParts = array();
76
        $terms = $variables = $haveVariables = $elementCount = 0;
77
        foreach ($this->children as $child) {
78
            $elementCount++;
79
            if (($child instanceof Text) &&
80
                ($child->getSource() == 'term' ||
81
                    $child->getSource() == 'value')) {
82
                $terms++;
83
            }
84
            if (($child instanceof Label)) {
85
                ++$terms;
86
            }
87
            if (method_exists($child, "getSource") && $child->getSource() == 'variable' &&
88
                !empty($child->getVariable()) &&
89
                !empty($data->{$child->getVariable()})
90
            ) {
91
                ++$variables;
92
            }
93
            $text = $child->render($data, $citationNumber);
94
            $delimiter = $this->delimiter;
95
            if (!empty($text)) {
96
                if ($delimiter && ($elementCount < count($this->children))) {
97
                    //check to see if the delimiter is already the last character of the text string
98
                    //if so, remove it so we don't have two of them when the group will be merged
99
                    $stext = strip_tags(trim($text));
100
                    if ((strrpos($stext, $delimiter[0]) + 1) == strlen($stext) && strlen($stext) > 1) {
101
                        $text = str_replace($stext, '----REPLACE----', $text);
102
                        $stext = substr($stext, 0, -1);
103
                        $text = str_replace('----REPLACE----', $stext, $text);
104
                    }
105
                }
106
                //give the text parts a name
107
                if ($child instanceof Text) {
108
                    $textParts[$child->getVariable()] = $text;
109
                } else {
110
                    $textParts[$elementCount] = $text;
111
                }
112
113
                if (method_exists($child, "getSource") && $child->getSource() == 'variable' ||
114
                   (method_exists($child, "getVariable") && !empty($child->getVariable()))) {
115
116
                    $haveVariables++;
117
                }
118
119
                if (method_exists($child, "getSource") && $child->getSource() == 'macro') {
120
                    $haveVariables++;
121
                }
122
            }
123
        }
124
        return $this->formatting($textParts, $variables, $haveVariables, $terms);
125
    }
126
127
    /**
128
     * @return mixed
129
     */
130
    public function getParent()
131
    {
132
        return $this->parent;
133
    }
134
135
    /**
136
     * @param $textParts
137
     * @param $variables
138
     * @param $haveVariables
139
     * @param $terms
140
     * @return string
141
     */
142
    protected function formatting($textParts, $variables, $haveVariables, $terms)
143
    {
144
        if (empty($textParts)) {
145
            return "";
146
        }
147
        if ($variables && !$haveVariables) {
148
            return ""; // there has to be at least one other none empty value before the term is output
149
        }
150
151
        if (count($textParts) == $terms) {
152
            return ""; // there has to be at least one other none empty value before the term is output
153
        }
154
155
        $text = implode($this->delimiter, $textParts); // insert the delimiter if supplied.
156
        if (!empty($text)) {
157
            return $this->wrapDisplayBlock($this->addAffixes($this->format(($text))));
158
        }
159
160
        return "";
161
    }
162
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
163