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

DatePart   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Test Coverage

Coverage 86.05%

Importance

Changes 0
Metric Value
eloc 88
dl 0
loc 202
ccs 74
cts 86
cp 0.8605
rs 9.2
c 0
b 0
f 0
wmc 40

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getRangeDelimiter() 0 3 1
A getName() 0 3 1
A __construct() 0 21 6
A render() 0 5 2
A monthFromLocale() 0 8 2
A getForm() 0 3 1
A renderYear() 0 12 4
A renderWithoutAffixes() 0 18 6
B renderMonth() 0 24 8
B renderDay() 0 21 9

How to fix   Complexity   

Complex Class

Complex classes like DatePart often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DatePart, and based on these observations, apply Extract Interface, too.

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 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 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 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\DelimiterTrait;
16
use Seboettg\CiteProc\Styles\FormattingTrait;
17
use Seboettg\CiteProc\Styles\RangeDelimiterTrait;
18
use Seboettg\CiteProc\Styles\TextCaseTrait;
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 55
    public function __construct(\SimpleXMLElement $node)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
58
    {
59 55
        foreach ($node->attributes() as $attribute) {
60 55
            if ("name" === $attribute->getName()) {
61 55
                $this->name = (string) $attribute;
62
            }
63 55
            if ("form" === $attribute->getName()) {
64 23
                $this->form = (string) $attribute;
65
            }
66 55
            if ("range-delimiter" === $attribute->getName()) {
67 1
                $this->rangeDelimiter = (string) $attribute;
68
            }
69
        }
70
71 55
        if (empty($this->rangeDelimiter)) {
72 55
            $this->rangeDelimiter = self::DEFAULT_RANGE_DELIMITER;
73
        }
74
75 55
        $this->initFormattingAttributes($node);
76 55
        $this->initAffixesAttributes($node);
77 55
        $this->initTextCaseAttributes($node);
78 55
    }
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 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 indented incorrectly; expected 2 spaces but found 1
Loading history...
84
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
85
     */
86 47
    public function render(DateTime $date, Date $parent)
87
    {
88 47
        $this->parent = $parent; //set parent
89 47
        $text = $this->renderWithoutAffixes($date);
90 47
        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...
Coding Style introduced by
Parameter $date should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $parent should have a doc-comment as per coding-style.
Loading history...
94
     * @param $date
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...
95
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
96
     */
97 47
    public function renderWithoutAffixes(DateTime $date, Date $parent = null)
98
    {
99 47
        if (!is_null($parent)) {
100 1
            $this->parent = $parent;
101
        }
102 47
        $text = "";
103 47
        switch ($this->name) {
104 47
            case 'year':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
105 47
                $text = $this->renderYear($date);
106 47
                break;
107 15
            case 'month':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
108 15
                $text = $this->renderMonth($date);
109 15
                break;
110 14
            case 'day':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
111 14
                $text = $this->renderDay($date);
112
        }
113
114 47
        return !empty($text) ? $this->format($this->applyTextCase($text)) : "";
115
    }
116
117
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
118
     * @return string
119
     */
120
    public function getForm()
121
    {
122
        return $this->form;
123
    }
124
125
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
126
     * @return string
127
     */
128 36
    public function getName()
129
    {
130 36
        return $this->name;
131
    }
132
133
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
134
     * @return string
135
     */
136 1
    public function getRangeDelimiter()
137
    {
138 1
        return $this->rangeDelimiter;
139
    }
140
141
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
142
     * @param DateTime $date
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...
143
     * @return string|int
1 ignored issue
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
144
     */
145 47
    protected function renderYear(DateTime $date)
146
    {
147 47
        $text = $date->getYear();
148 47
        if ($text > 0 && $text < 1000) {
149
            $text = $text . CiteProc::getContext()->getLocale()->filter("terms", "ad")->single;
150
            return $text;
151 47
        } elseif ($text < 0) {
152
            $text = $text * -1;
153
            $text = $text . CiteProc::getContext()->getLocale()->filter("terms", "bc")->single;
154
            return $text;
155
        }
156 47
        return $text;
157
    }
158
159
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
160
     * @param DateTime $date
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...
161
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
162
     */
163 15
    protected function renderMonth(DateTime $date)
164
    {
165 15
        if ($date->getMonth() < 1 || $date->getMonth() > 12) {
166
            return "";
167
        }
168
169 15
        $text = $date->getMonth();
170
171 15
        $form = !empty($this->form) ? $this->form : "long";
172
        switch ($form) {
173 15
            case 'numeric':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
174 1
                break;
175 15
            case 'numeric-leading-zeros':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
176 3
                $text = sprintf("%02d", $text);
177 3
                break;
178 13
            case 'short':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
179 3
                $text = $this->monthFromLocale($text, $form);
180 3
                break;
181 11
            case 'long':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
182
            default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
183 11
                $text = $this->monthFromLocale($text, $form);
184 11
                break;
185
        }
186 15
        return $text;
187
    }
188
189
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
190
     * @param DateTime $date
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...
191
     * @return int|string
1 ignored issue
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
192
     */
193 14
    protected function renderDay(DateTime $date)
194
    {
195 14
        if ($date->getDay() < 1 || $date->getDay() > 31) {
196 3
            return "";
197
        }
198
199 13
        $text = $date->getDay();
200 13
        $form = !empty($this->form) ? $this->form : $this->parent->getForm();
201
        switch ($form) {
202 13
            case 'numeric':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
203 1
                break;
204 13
            case 'numeric-leading-zeros':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
205 3
                $text = sprintf("%02d", $text);
206 3
                break;
207 11
            case 'ordinal':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
208
                $limitDayOrdinals = CiteProc::getContext()->getLocale()->filter("options", "limit-day-ordinals-to-day-1");
209
                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...
210
                    $text = Number::ordinal($text);
211
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
212
        }
213 13
        return $text;
214
    }
215
216
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $text should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $form should have a doc-comment as per coding-style.
Loading history...
217
     * @param $text
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...
218
     * @param $form
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...
219
     * @return mixed
1 ignored issue
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
220
     */
221 13
    protected function monthFromLocale($text, $form)
222
    {
223 13
        if (empty($form)) {
224
            $form = "long";
225
        }
226 13
        $month = 'month-' . sprintf('%02d', $text);
227 13
        $text = CiteProc::getContext()->getLocale()->filter('terms', $month, $form)->single;
228 13
        return $text;
229
    }
230
231
}