Completed
Push — master ( 8b290e...989048 )
by Pierre
05:45
created

StandardDateFragmented::formatSymbol()   D

Complexity

Conditions 16
Paths 16

Size

Total Lines 88
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
eloc 37
nc 16
nop 3
dl 0
loc 88
rs 4.8736
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
namespace Popy\Calendar\Formatter\SymbolFormatter;
4
5
use Popy\Calendar\FormatterInterface;
6
use Popy\Calendar\Parser\FormatToken;
7
use Popy\Calendar\Formatter\LocalisationInterface;
8
use Popy\Calendar\Formatter\SymbolFormatterInterface;
9
use Popy\Calendar\ValueObject\DateRepresentationInterface;
10
use Popy\Calendar\ValueObject\DateFragmentedRepresentationInterface;
11
12
/**
13
 * Standard format, handling DateFragmentedRepresentationInterface.
14
 *
15
 * Weeks and day names assume a gregorian calendar structure.
16
 */
17
class StandardDateFragmented implements SymbolFormatterInterface
18
{
19
    /**
20
     * Locale (used for day & month names)
21
     *
22
     * @var LocalisationInterface
23
     */
24
    protected $locale;
25
26
    /**
27
     * Class constructor.
28
     *
29
     * @param LocalisationInterface $locale
30
     */
31
    public function __construct(LocalisationInterface $locale)
32
    {
33
        $this->locale = $locale;
34
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39
    public function formatSymbol(DateRepresentationInterface $input, FormatToken $token, FormatterInterface $formatter)
40
    {
41
        if (!$input instanceof DateFragmentedRepresentationInterface) {
42
            return;
43
        }
44
45
        if ($token->is('F')) {
46
            // F   A full textual representation of a month
47
            return (string)$this->locale->getMonthName(
48
                $input->getDateParts()->get(0)
49
            );
50
        }
51
52
        if ($token->is('M')) {
53
            // M   A short textual representation of a month, three letters
54
            return (string)$this->locale->getMonthShortName(
55
                $input->getDateParts()->get(0)
56
            );
57
        }
58
59
        if ($token->is('m')) {
60
            // m   Numeric representation of a month, with leading zeros
61
            return sprintf('%02d', $input->getDateParts()->get(0) + 1);
62
        }
63
64
        if ($token->is('n')) {
65
            // n   Numeric representation of a month, without leading zeros
66
            return $input->getDateParts()->get(0) + 1;
67
        }
68
69
        if ($token->is('t')) {
70
            // t    Number of days in the given month
71
            return (int)$input->getDateParts()->getSize(0);
72
        }
73
74
        if ($token->is('d')) {
75
            // d   Day of the month, 2 digits with leading zeros
76
            return sprintf('%02d', $input->getDateParts()->get(1) + 1);
77
        }
78
79
        if ($token->is('j')) {
80
            // j   Day of the month without leading zeros
81
            return $input->getDateParts()->get(1) + 1;
82
        }
83
84
        if ($token->is('S')) {
85
            // S   English ordinal suffix for the day of the month, 2 characters
86
            return $this->locale->getNumberOrdinalSuffix(
87
                (int)$input->getDateParts()->get(1)
88
            );
89
        }
90
91
        if ($token->is('l')) {
92
            // l (lowercase 'L')   A full textual representation of the day of the week
93
            return (string)$this->locale->getDayName(
94
                $input->getDateParts()->getTransversal(2)
95
            );
96
        }
97
98
        if ($token->is('D')) {
99
            // D   A textual representation of a day, three letters
100
            return (string)$this->locale->getDayShortName(
101
                $input->getDateParts()->getTransversal(2)
102
            );
103
        }
104
        
105
        if ($token->is('w')) {
106
            // w   Numeric representation of the day of the week   0 (for Sunday) through 6 (for Saturday)
107
            return (1 + $input->getDateParts()->getTransversal(2)) % 7;
108
        }
109
110
        if ($token->is('N')) {
111
            // N   ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday)
112
            return $input->getDateParts()->getTransversal(2) + 1;
113
        }
114
115
        if ($token->is('W')) {
116
            // W   ISO-8601 week number of year, weeks starting on Monday
117
            return sprintf(
118
                '%02d',
119
                $input->getDateParts()->getTransversal(1) + 1
120
            );
121
        }
122
123
        if ($token->is('o')) {
124
            // Y   A full numeric representation of a year, 4 digits
125
            // o   ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead.
126
            return sprintf('%04d', $input->getDateParts()->getTransversal(0));
127
        }
128
    }
129
}
130