Completed
Push — master ( 6fd4c0...5423a1 )
by Sebastian
03:25
created

Label::render()   F

Complexity

Conditions 17
Paths 672

Size

Total Lines 57
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 57
rs 3.5087
cc 17
eloc 39
nc 672
nop 2

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\CiteProc;
12
use Seboettg\CiteProc\Styles\AffixesTrait;
13
use Seboettg\CiteProc\Styles\FormattingTrait;
14
use Seboettg\CiteProc\Styles\TextCaseTrait;
15
16
17
/**
18
 * Class Label
19
 * @package Seboettg\CiteProc\Rendering
20
 *
21
 * @author Sebastian Böttger <[email protected]>
22
 */
23
class Label implements RenderingInterface
24
{
25
26
    use AffixesTrait,
27
        FormattingTrait,
28
        TextCaseTrait;
29
30
    private $variable;
31
32
    /**
33
     * Selects the form of the term, with allowed values:
34
     *
35
     *   - “long” - (default), e.g. “page”/”pages” for the “page” term
36
     *   - “short” - e.g. “p.”/”pp.” for the “page” term
37
     *   - “symbol” - e.g. “§”/”§§” for the “section” term
38
     *
39
     * @var string
40
     */
41
    private $form = "";
42
43
    /**
44
     * Sets pluralization of the term, with allowed values:
45
     *
46
     *   - “contextual” - (default), the term plurality matches that of the variable content. Content is considered
47
     *     plural when it contains multiple numbers (e.g. “page 1”, “pages 1-3”, “volume 2”, “volumes 2 & 4”), or, in
48
     *     the case of the “number-of-pages” and “number-of-volumes” variables, when the number is higher than 1
49
     *     (“1 volume” and “3 volumes”).
50
     *   - “always” - always use the plural form, e.g. “pages 1” and “pages 1-3”
51
     *   - “never” - always use the singular form, e.g. “page 1” and “page 1-3”
52
     *
53
     * @var string
54
     */
55
    private $plural = "contextual";
56
57
    /**
58
     * Label constructor.
59
     * @param \SimpleXMLElement $node
60
     */
61
    public function __construct(\SimpleXMLElement $node)
62
    {
63
        /** @var \SimpleXMLElement $attribute */
64 View Code Duplication
        foreach ($node->attributes() as $attribute) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
            switch ($attribute->getName()) {
66
                case "variable":
67
                    $this->variable = (string) $attribute;
68
                    break;
69
                case "form":
70
                    $this->form = (string) $attribute;
71
                    break;
72
                case "plural":
73
                    $this->plural = (string) $attribute;
74
                    break;
75
            }
76
        }
77
78
        $this->initFormattingAttributes($node);
79
        $this->initAffixesAttributes($node);
80
        $this->initTextCaseAttributes($node);
81
    }
82
83
    /**
84
     * @param \stdClass $data
85
     * @param int|null $citationNumber
86
     * @return string
87
     */
88
    public function render($data, $citationNumber = null)
89
    {
90
        $lang = (isset($data->language) && $data->language != 'en') ? $data->language : 'en';
91
92
        $text = '';
93
        $variables = explode(' ', $this->variable);
94
        $form = !empty($this->form) ? $this->form : 'long';
95
        $plural = "";
96
        switch ($this->plural) {
97
            case 'never':
98
                $plural = 'single';
99
                break;
100
            case 'always':
101
                $plural = 'multiple';
102
                break;
103
            case 'contextual':
104
            default:
105
                //$this->detectPlural($data);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
106
        }
107
108
        if ($this->variable === "editortranslator") {
109
            if (isset($data->editor) && isset($data->translator)) {
110
                $plural = $this->getPlural($data, $plural, "editortranslator");
111
                $term = CiteProc::getContext()->getLocale()->filter('terms', "editortranslator", $form);
112
                $pluralForm = $term->{$plural};
113
                if (!empty($pluralForm)) {
114
                    $text = $pluralForm;
115
                }
116
            }
117
        } else {
118
            foreach ($variables as $variable) {
119
120
                if (isset($data->{$variable})) {
121
                    $plural = $this->getPlural($data, $plural, $variable);
122
                    $term = CiteProc::getContext()->getLocale()->filter('terms', $variable, $form);
123
                    $var = $data->{$variable};
124
                    $pluralForm = $term->{$plural};
125
                    if (!empty($var) && !empty($pluralForm)) {
126
                        $text = $pluralForm;
127
                        break;
128
                    }
129
                }
130
            }
131
        }
132
133
        if (empty($text)) {
134
            return "";
135
        }
136
        if ($this->stripPeriods) {
137
            $text = str_replace('.', '', $text);
138
        }
139
140
        $text = preg_replace("/\s\&\s/", " &#38; ", $text); //replace ampersands by html entity
141
142
        $text = $this->format($this->applyTextCase($text, $lang));
143
        return $this->addAffixes($text);
144
    }
145
146
147
    private function evaluateStringPluralism($data, $variable)
148
    {
149
        $str = $data->{$variable};
150
        $plural = 'single';
151
        if (!empty($str)) {
152
//      $regex = '/(?:[0-9],\s*[0-9]|\s+and\s+|&|([0-9]+)\s*[\-\x2013]\s*([0-9]+))/';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
153
            switch ($variable) {
154
                case 'page':
155
                    $pageRegex = "/([a-zA-Z]*)([0-9]+)\s*(?:–|-)\s*([a-zA-Z]*)([0-9]+)/";
156
                    $err = preg_match($pageRegex, $str, $m);
157
                    if ($err !== false && count($m) == 0) {
158
                        $plural = 'single';
159
                    } elseif ($err !== false && count($m)) {
160
                        $plural = 'multiple';
161
                    }
162
                    break;
163
                default:
164
                    if (is_numeric($str)) {
165
                        return $str > 1 ? 'multiple' : 'single';
166
                    }
167
            }
168
        }
169
        return $plural;
170
    }
171
172
    /**
173
     * @param string $variable
174
     */
175
    public function setVariable($variable)
176
    {
177
        $this->variable = $variable;
178
    }
179
180
    /**
181
     * @param $data
182
     * @param $plural
183
     * @param $variable
184
     * @return string
185
     */
186
    protected function getPlural($data, $plural, $variable)
187
    {
188
189
        if ($variable === "editortranslator") {
190
            $var = $data->editor;
191
        } else {
192
            $var = $data->{$variable};
193
        }
194
        if (((!isset($this->plural) || empty($plural))) && !empty($var)) {
195
            if (is_array($var)) {
196
                $count = count($var);
197
                if ($count == 1) {
198
                    $plural = 'single';
199
                    return $plural;
200
                } elseif ($count > 1) {
201
                    $plural = 'multiple';
202
                    return $plural;
203
                }
204
                return $plural;
205
            } else {
206
                return $this->evaluateStringPluralism($data, $variable);
207
            }
208
209
        } else {
210
            if ($this->plural != "always") {
211
                $plural = $this->evaluateStringPluralism($data, $variable);
212
                return $plural;
213
            }
214
            return $plural;
215
        }
216
    }
217
218
    /**
219
     * @return string
220
     */
221
    public function getForm()
222
    {
223
        return $this->form;
224
    }
225
226
    /**
227
     * @param string $form
228
     */
229
    public function setForm($form)
230
    {
231
        $this->form = $form;
232
    }
233
234
235
}