TimezoneDropdown   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 24
dl 0
loc 70
rs 10
c 3
b 0
f 1
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizedAliases() 0 15 4
A dataValue() 0 3 1
A __construct() 0 20 5
1
<?php
2
3
namespace LeKoala\GeoTools\Fields;
4
5
use ArrayAccess;
6
use SilverStripe\ORM\ArrayLib;
7
use SilverStripe\Forms\DropdownField;
8
9
/**
10
 * A timezone dropdown
11
 * @link https://www.php.net/manual/en/timezones.others.php
12
 * @link https://www.timeanddate.com/time/zones/
13
 */
14
class TimezoneDropdown extends DropdownField
15
{
16
    /**
17
     * in yml, it needs to be name/value like so
18
     * - name: "Gulf Standard Time (GST)"
19
     *   value: "Asia/Dubai"
20
     *
21
     * @var array<string,string>
22
     */
23
    private static $aliases = [
24
        // "Gulf Standard Time (GST)" => "Asia/Dubai",
25
        // "Central European Time (CET)" => "Europe/Brussels",
26
        // "Atlantic Standard Time (AST)" => "America/Blanc-Sablon",
27
        // "Eastern Standard Time (EST)" => "America/Panama",
28
        // "Central Standard Time (CST)" => "America/Regina",
29
        // "Mountain Standard Time (MST)" => "America/Phoenix",
30
        // "Pacific Standard Time (PST)" => "America/Los_Angeles",
31
    ];
32
33
    /**
34
     * @param string $name The field name
35
     * @param string $title The field title
36
     * @param array<string,string>|ArrayAccess<string,string> $source A map of the dropdown items
37
     * @param mixed $value The current value
38
     */
39
    public function __construct($name, $title = null, $source = [], $value = null)
40
    {
41
        if (empty($source)) {
42
            $source = ArrayLib::valuekey(timezone_identifiers_list());
0 ignored issues
show
Bug introduced by
Are you sure the usage of timezone_identifiers_list() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
43
            $aliases = $this->config()->aliases ?? [];
44
            if (!empty($aliases)) {
45
                $normalizedAliases = [];
46
                foreach ($aliases as $k => $v) {
47
                    // Add a prefix to avoid clashes with regular value
48
                    if (is_int($k)) {
49
                        $normalizedAliases[$v['name']] = '_' . $v['value'];
50
                    } else {
51
                        $normalizedAliases[$k] = '_' . $v;
52
                    }
53
                }
54
55
                $source = array_flip($normalizedAliases) + $source;
56
            }
57
        }
58
        parent::__construct($name, $title, $source, $value);
59
    }
60
61
    /**
62
     * @return array<string,string>
63
     */
64
    public function normalizedAliases(): array
65
    {
66
        $aliases = $this->config()->aliases ?? [];
67
        $normalizedAliases = [];
68
        if (!empty($aliases)) {
69
            foreach ($aliases as $k => $v) {
70
                if (is_int($k)) {
71
                    $normalizedAliases[$v['name']] = $v['value'];
72
                } else {
73
                    $normalizedAliases[$k] = $v;
74
                }
75
            }
76
        }
77
        //@phpstan-ignore-next-line
78
        return $normalizedAliases;
79
    }
80
81
    public function dataValue()
82
    {
83
        return ltrim(parent::dataValue(), '_');
84
    }
85
}
86