StandardDateTime::formatSymbol()   C
last analyzed

Complexity

Conditions 15
Paths 17

Size

Total Lines 61
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 26
nc 17
nop 3
dl 0
loc 61
rs 6.2274
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\SymbolFormatterInterface;
8
use Popy\Calendar\ValueObject\DateRepresentationInterface;
9
use Popy\Calendar\ValueObject\DateTimeRepresentationInterface;
10
11
/**
12
 * Standard format, handling DateTimeRepresentationInterface.
13
 *
14
 * Not (yet) handling half-time format, so every time is shown as AM.
15
 */
16
class StandardDateTime implements SymbolFormatterInterface
17
{
18
    /**
19
     * @inheritDoc
20
     */
21
    public function formatSymbol(DateRepresentationInterface $input, FormatToken $token, FormatterInterface $formatter)
22
    {
23
        if (!$input instanceof DateTimeRepresentationInterface) {
24
            return;
25
        }
26
27
        if ($token->isOne('a', 'A')) {
28
            // a   Lowercase Ante meridiem and Post meridiem   am or pm
29
            // A   Uppercase Ante meridiem and Post meridiem   AM or PM
30
            $res = $input->getTime()->canBeHalved(0) ? 'pm' : 'am';
31
            return $token->is('a') ? $res : strtoupper($res);
32
        }
33
34
        if ($token->is('B')) {
35
            // B   Swatch Internet time    000 through 999
36
            return sprintf('%03d', $input->getTime()->getTransversal(0));
37
        }
38
39
        if ($token->isOne('g', 'h')) {
40
            // g   12-hour format of an hour without leading zeros 1 through 12
41
            // h   12-hour format of an hour with leading zeros    01 through 12
42
            $res = $input->getTime()->getHalved(0);
43
            if (!$res) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $res of type null|integer is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
44
                $res = (integer)floor($input->getTime()->getSize(0) / 2);
45
            }
46
47
            if ($token->is('h')) {
48
                return sprintf('%02d', $res);
49
            }
50
51
            return (string)$res;
52
        }
53
54
        if ($token->is('G')) {
55
            // G   24-hour format of an hour without leading zeros 0 through 23
56
            return $input->getTime()->get(0);
57
        }
58
59
        if ($token->is('H')) {
60
            // H   24-hour format of an hour with leading zeros    00 through 23
61
            return sprintf('%02d', $input->getTime()->get(0));
62
        }
63
64
        if ($token->is('i')) {
65
            // i   Minutes with leading zeros  00 to 59
66
            return sprintf('%02d', $input->getTime()->get(1));
67
        }
68
69
        if ($token->is('s')) {
70
            // s   Seconds, with leading zeros 00 through 59
71
            return sprintf('%02d', $input->getTime()->get(2));
72
        }
73
74
        if ($token->is('v')) {
75
            // v   Milliseconds
76
            return sprintf('%03d', intval($input->getTime()->get(3)));
77
        }
78
79
        if ($token->is('µ')) {
80
            // Remaining microseconds
81
            return sprintf('%03d', intval($input->getTime()->get(4)));
82
        }
83
    }
84
}
85