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

DatePart::renderYear()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 6

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 1
dl 0
loc 12
ccs 5
cts 10
cp 0.5
crap 6
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * citeproc-php
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
1 ignored issue
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
Coding Style introduced by
Tag value for @link tag indented incorrectly; expected 6 spaces but found 8
Loading history...
6
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
1 ignored issue
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
Coding Style introduced by
Tag value for @copyright tag indented incorrectly; expected 1 spaces but found 3
Loading history...
7
 * @license     https://opensource.org/licenses/MIT
1 ignored issue
show
Coding Style introduced by
@license tag must contain a URL and a license name
Loading history...
Coding Style introduced by
Tag value for @license tag indented incorrectly; expected 3 spaces but found 5
Loading history...
8
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
9
10
namespace Seboettg\CiteProc\Rendering\Date;
11
use Seboettg\CiteProc\CiteProc;
12
use Seboettg\CiteProc\Rendering\Layout;
13
use Seboettg\CiteProc\Rendering\Number;
14
use Seboettg\CiteProc\Styles\AffixesTrait;
15
use Seboettg\CiteProc\Styles\FormattingTrait;
16
use Seboettg\CiteProc\Styles\RangeDelimiterTrait;
17
use Seboettg\CiteProc\Styles\TextCaseTrait;
18
use SimpleXMLElement;
19
20
21
/**
22
 * Class DatePart
23
 * @package Seboettg\CiteProc\Rendering\Date
1 ignored issue
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
24
 *
25
 * @author Sebastian Böttger <[email protected]>
26
 */
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...
27
class DatePart
28
{
29
30
    const DEFAULT_RANGE_DELIMITER = "–";
31
32
    use FormattingTrait,
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\Date\DatePart.
Loading history...
33
        AffixesTrait,
34
        TextCaseTrait,
35
        RangeDelimiterTrait;
36
37
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
38
     * @var string
39
     */
40
    private $name;
0 ignored issues
show
Coding Style introduced by
Private member variable "name" must be prefixed with an underscore
Loading history...
41
42
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
     * @var string
44
     */
45
    private $form;
0 ignored issues
show
Coding Style introduced by
Private member variable "form" must be prefixed with an underscore
Loading history...
46
47
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
48
     * @var string
49
     */
50
    private $rangeDelimiter;
0 ignored issues
show
Coding Style introduced by
Private member variable "rangeDelimiter" must be prefixed with an underscore
Loading history...
51
52
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
53
     * @var Date
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 58
    public function __construct(SimpleXMLElement $node)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
58
    {
59 58
        foreach ($node->attributes() as $attribute) {
60 58
            if ("name" === $attribute->getName()) {
61 58
                $this->name = (string) $attribute;
62
            }
63 58
            if ("form" === $attribute->getName()) {
64 23
                $this->form = (string) $attribute;
65
            }
66 58
            if ("range-delimiter" === $attribute->getName()) {
67 1
                $this->rangeDelimiter = (string) $attribute;
68
            }
69
        }
70
71 58
        if (empty($this->rangeDelimiter)) {
72 58
            $this->rangeDelimiter = self::DEFAULT_RANGE_DELIMITER;
73
        }
74
75 58
        $this->initFormattingAttributes($node);
76 58
        $this->initAffixesAttributes($node);
77 58
        $this->initTextCaseAttributes($node);
78 58
    }
79
80
81
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
82
     * @param DateTime $date
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...
83
     * @param Date $parent
3 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
84
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
85
     */
86 50
    public function render(DateTime $date, Date $parent)
87
    {
88 50
        $this->parent = $parent; //set parent
89 50
        $text = $this->renderWithoutAffixes($date);
90 50
        return !empty($text) ? $this->addAffixes($text) : "";
91
    }
92
93
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
94
     * @param DateTime $date
3 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
95
     * @param Date|null $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...
96
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
97
     */
98 50
    public function renderWithoutAffixes(DateTime $date, Date $parent = null)
99
    {
100 50
        if (!is_null($parent)) {
101 1
            $this->parent = $parent;
102
        }
103 50
        $text = "";
104 50
        switch ($this->name) {
105 50
            case 'year':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
106 49
                $text = $this->renderYear($date);
107 49
                break;
108 16
            case 'month':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
109 16
                $text = $this->renderMonth($date);
110 16
                break;
111 14
            case 'day':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
112 14
                $text = $this->renderDay($date);
113
        }
114
115 50
        return !empty($text) ? $this->format($this->applyTextCase($text)) : "";
116
    }
117
118
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
119
     * @return string
120
     */
121
    public function getForm()
122
    {
123
        return $this->form;
124
    }
125
126
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
127
     * @return string
128
     */
129 39
    public function getName()
130
    {
131 39
        return $this->name;
132
    }
133
134
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
135
     * @return string
136
     */
137 1
    public function getRangeDelimiter()
138
    {
139 1
        return $this->rangeDelimiter;
140
    }
141
142
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
143
     * @param DateTime $date
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...
144
     * @return string|int
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
145
     */
146 49
    protected function renderYear(DateTime $date)
147
    {
148 49
        $text = $date->getYear();
149 49
        if ($text > 0 && $text < 1000) {
150
            $text = $text . CiteProc::getContext()->getLocale()->filter("terms", "ad")->single;
151
            return $text;
152 49
        } elseif ($text < 0) {
153
            $text = $text * -1;
154
            $text = $text . CiteProc::getContext()->getLocale()->filter("terms", "bc")->single;
155
            return $text;
156
        }
157 49
        return $text;
158
    }
159
160
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
161
     * @param DateTime $date
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...
162
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
163
     */
164 16
    protected function renderMonth(DateTime $date)
165
    {
166 16
        if ($date->getMonth() < 1 || $date->getMonth() > 12) {
167 1
            return "";
168
        }
169
170 15
        $text = $date->getMonth();
171
172 15
        $form = !empty($this->form) ? $this->form : "long";
173
        switch ($form) {
174 15
            case 'numeric':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
175 1
                break;
176 15
            case 'numeric-leading-zeros':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
177 3
                $text = sprintf("%02d", $text);
178 3
                break;
179 13
            case 'short':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
180 11
            case 'long':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
181
            default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
182 13
                $text = $this->monthFromLocale($text, $form);
183 13
                break;
184
        }
185 15
        return $text;
186
    }
187
188
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
189
     * @param DateTime $date
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...
190
     * @return int|string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
191
     */
192 14
    protected function renderDay(DateTime $date)
193
    {
194 14
        if ($date->getDay() < 1 || $date->getDay() > 31) {
195 2
            return "";
196
        }
197
198 14
        $text = $date->getDay();
199 14
        $form = !empty($this->form) ? $this->form : $this->parent->getForm();
200
        switch ($form) {
201 14
            case 'numeric':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
202 1
                break;
203 14
            case 'numeric-leading-zeros':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
204 3
                $text = sprintf("%02d", $text);
205 3
                break;
206 12
            case 'ordinal':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
207
                $limitDayOrdinals = CiteProc::getContext()->getLocale()->filter("options", "limit-day-ordinals-to-day-1");
208
                if (!$limitDayOrdinals || Layout::getNumberOfCitedItems() <= 1) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
introduced by
$limitDayOrdinals is of type stdClass, thus it always evaluated to true.
Loading history...
209
                    $text = Number::ordinal($text);
210
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
211
        }
212 14
        return $text;
213
    }
214
215
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
216
     * @param $text
2 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
217
     * @param $form
2 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
218
     * @return mixed
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
219
     */
220 13
    protected function monthFromLocale($text, $form)
221
    {
222 13
        if (empty($form)) {
223
            $form = "long";
224
        }
225 13
        $month = 'month-' . sprintf('%02d', $text);
226 13
        $text = CiteProc::getContext()->getLocale()->filter('terms', $month, $form)->single;
227 13
        return $text;
228
    }
229
230
}