DateStyle::fromString()   C
last analyzed

Complexity

Conditions 16
Paths 95

Size

Total Lines 61
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 45
c 1
b 0
f 0
nc 95
nop 1
dl 0
loc 61
rs 5.5666

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
declare(strict_types=1);
3
namespace Ivory\Connection;
4
5
/**
6
 * Manages date styles as configured by the
7
 * {@link https://www.postgresql.org/docs/11/runtime-config-client.html#GUC-DATESTYLE `DateStyle`} configuration
8
 * setting.
9
 */
10
final class DateStyle
11
{
12
    const FORMAT_ISO = 'ISO';
13
    const FORMAT_POSTGRES = 'Postgres';
14
    const FORMAT_SQL = 'SQL';
15
    const FORMAT_GERMAN = 'German';
16
17
    const ORDER_DMY = 'DMY';
18
    const ORDER_MDY = 'MDY';
19
    const ORDER_YMD = 'YMD';
20
21
    private $format;
22
    private $order;
23
24
25
    /**
26
     * @param string $dateStyleStr the date style string, as held in the <tt>DateStyle</tt> configuration setting
27
     * @return DateStyle
28
     */
29
    public static function fromString(string $dateStyleStr): DateStyle
30
    {
31
        $parts = preg_split('~\W+~', $dateStyleStr, 2);
32
        $fmt = $parts[0];
33
        $ord = ($parts[1] ?? null);
34
35
        switch (strtoupper($fmt)) {
36
            case strtoupper(self::FORMAT_ISO):
37
                $format = self::FORMAT_ISO;
38
                $order = self::ORDER_YMD;
39
                break;
40
41
            case strtoupper(self::FORMAT_POSTGRES):
42
                $format = self::FORMAT_POSTGRES;
43
                $orders = [self::ORDER_MDY, self::ORDER_DMY];
44
                break;
45
46
            case strtoupper(self::FORMAT_SQL):
47
                $format = self::FORMAT_SQL;
48
                $orders = [self::ORDER_MDY, self::ORDER_DMY];
49
                break;
50
51
            case strtoupper(self::FORMAT_GERMAN):
52
                $format = self::FORMAT_GERMAN;
53
                $order = self::ORDER_DMY;
54
                break;
55
56
            default:
57
                trigger_error("Unrecognized DateStyle output format specification: $fmt", E_USER_NOTICE);
58
                $format = $fmt;
59
        }
60
61
        if (!isset($order)) {
62
            switch (strtoupper($ord)) {
0 ignored issues
show
Bug introduced by
It seems like $ord can also be of type null; however, parameter $string of strtoupper() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
            switch (strtoupper(/** @scrutinizer ignore-type */ $ord)) {
Loading history...
63
                case strtoupper(self::ORDER_DMY):
64
                case strtoupper('Euro'):
65
                case strtoupper('European'):
66
                    $order = self::ORDER_DMY;
67
                    break;
68
69
                case strtoupper(self::ORDER_MDY):
70
                case strtoupper('US'):
71
                case strtoupper('NonEuro'):
72
                case strtoupper('NonEuropean'):
73
                    $order = self::ORDER_MDY;
74
                    break;
75
76
                case strtoupper(self::ORDER_YMD):
77
                    $order = self::ORDER_YMD;
78
                    break;
79
80
                default:
81
                    trigger_error("Unrecognized DateStyle input/output year/month/day ordering: $ord", E_USER_NOTICE);
82
                    $order = $ord;
83
            }
84
            if (isset($orders) && !in_array($order, $orders)) {
85
                $order = $orders[0]; // irrelevant order, set the default one according to $format
86
            }
87
        }
88
89
        return new DateStyle($format, $order);
90
    }
91
92
    private function __construct(string $format, string $order)
93
    {
94
        $this->format = $format;
95
        $this->order = $order;
96
    }
97
98
    /**
99
     * @return string the output format specification; one of <tt>DateStyle::FORMAT_*</tt> constants
100
     */
101
    public function getFormat(): string
102
    {
103
        return $this->format;
104
    }
105
106
    /**
107
     * @return string the input/output specification for year/month/day ordering;
108
     *                one of <tt>DateStyle::ORDER_*</tt> constants (synonyms are canonicalized to these constants);
109
     *                irrelevant values are fixed to those valid for the output format
110
     */
111
    public function getOrder(): string
112
    {
113
        return $this->order;
114
    }
115
}
116