Completed
Push — master ( 0a0843...e0ea85 )
by Chad
12s queued 10s
created

DateTimeZoneUtil   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 33.82 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 23
loc 68
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 12 12 3
A fromOffset() 0 4 1
A getLongName() 11 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace SubjectivePHP\DateTime\Utilities;
3
4
use DateTimeZone;
5
use SubjectivePHP\DateTime\Constants\TimezonesInterface;
6
7
/**
8
 * Static utility class for working with DateTimeZone objects.
9
 */
10
abstract class DateTimeZoneUtil implements TimezonesInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    const OUTLIERS = [
16
        'WIB' => self::TIMEZONE_ASIA_JAKARTA,  //Western Indonesian Time
17
        'FET' => self::TIMEZONE_EUROPE_HELSINKI, //Further-eastern European Time
18
        'AEST' => self::TIMEZONE_AUSTRALIA_TASMANIA, //Australia Eastern Standard Time
19
        'AWST' => self::TIMEZONE_AUSTRALIA_WEST, //Australia Western Standard Time
20
        'WITA' => self::TIMEZONE_ASIA_MAKASSAR,
21
        'AEDT' => self::TIMEZONE_AUSTRALIA_SYDNEY, //Australia Eastern Daylight Time
22
        'ACDT' => self::TIMEZONE_AUSTRALIA_ADELAIDE, //Australia Central Daylight Time
23
    ];
24
25
    /**
26
     * Returns a DateTimeZone instance for the give nameOrAbbreviation.
27
     *
28
     * @param string       $nameOrAbbreviation The timezone nameOrAbbreviation.
29
     * @param DateTimeZone $default            The default timezone to return if none can be created.
30
     *
31
     * @return DateTimeZone|null
32
     */
33 View Code Duplication
    final public static function fromString(string $nameOrAbbreviation, DateTimeZone $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        try {
36
            return new DateTimeZone($nameOrAbbreviation);
37
        } catch (\Exception $e) {
38
            if (array_key_exists($nameOrAbbreviation, self::OUTLIERS)) {
39
                return new DateTimeZone(self::OUTLIERS[$nameOrAbbreviation]);
40
            }
41
42
            return $default;
43
        }
44
    }
45
46
    /**
47
     * Returns a DateTimeZone object based on gmt offset.
48
     *
49
     * @param int  $gmtOffset         Offset from GMT in seconds.
50
     * @param bool $isDaylightSavings Daylight saving time indicator.
51
     *
52
     * @return DateTimeZone
53
     */
54
    final public static function fromOffset(int $gmtOffset, bool $isDaylightSavings) : DateTimeZone
55
    {
56
        return self::fromString(timezone_name_from_abbr('', $gmtOffset, (int)$isDaylightSavings));
57
    }
58
59
    /**
60
     * Returns the long name of the given DateTimeZone.
61
     *
62
     * @param DateTimeZone $timezone The timezone object from which the long name should be obtained.
63
     *
64
     * @return string
65
     */
66 View Code Duplication
    final public static function getLongName(DateTimeZone $timezone) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        $nameFromTimeZone = $timezone->getName();
69
        if (strlen($nameFromTimeZone) > 6) {
70
            return $nameFromTimeZone;
71
        }
72
73
        $nameFromAbbr = timezone_name_from_abbr($nameFromTimeZone);
74
75
        return $nameFromAbbr === false ? $nameFromTimeZone : $nameFromAbbr;
76
    }
77
}
78