GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 864bdc...94a255 )
by Francisco
03:51
created

Calculator   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 97.78%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 114
ccs 44
cts 45
cp 0.9778
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A adjustLatitude() 0 11 4
A adjustLongitude() 0 11 4
A getTimeZoneName() 0 12 2
A getLocalDate() 0 10 2
A getCorrectTimestamp() 0 14 4
1
<?php
2
3
namespace GeoTimeZone;
4
5
use DateTime;
6
use DateTimeZone;
7
use ErrorException;
8
use GeoTimeZone\Quadrant\Tree;
9
10
class Calculator
11
{
12
    protected $quadrantTree;
13
    
14
    /**
15
     * TimeZone constructor.
16
     * @param $dataDirectory
17
     * @throws ErrorException
18
     */
19 18
    public function __construct($dataDirectory = null)
20
    {
21 18
        if (isset($dataDirectory) && is_dir($dataDirectory)) {
22 18
            $this->quadrantTree = new Tree($dataDirectory);
23 18
            $this->quadrantTree->initializeDataTree();
24
        }else{
25
            throw new ErrorException('Invalid data tree directory: ' . $dataDirectory);
26
        }
27 18
    }
28
    
29
    /**
30
     * Adjust the latitude value
31
     * @param $latitude
32
     * @return float|int
33
     * @throws ErrorException
34
     */
35 16
    protected function adjustLatitude($latitude)
36
    {
37 16
        $newLatitude = $latitude;
38 16
        if (null == $latitude || abs($latitude) > Tree::MAX_ABS_LATITUDE) {
39 2
            throw new ErrorException('Invalid latitude: ' . $latitude);
40
        }
41 14
        if (abs($latitude) == Tree::MAX_ABS_LATITUDE) {
42 4
            $newLatitude = ($latitude <=> 0) * Tree::ABS_LATITUDE_LIMIT;
43
        }
44 14
        return $newLatitude;
45
    }
46
    
47
    /**
48
     * Adjust longitude value
49
     * @param $longitude
50
     * @return float|int
51
     * @throws ErrorException
52
     */
53 14
    protected function adjustLongitude($longitude)
54
    {
55 14
        $newLongitude = $longitude;
56 14
        if (null == $longitude || abs($longitude) > Tree::MAX_ABS_LONGITUDE) {
57 2
            throw new ErrorException('Invalid longitude: ' . $longitude);
58
        }
59 12
        if (abs($longitude) == Tree::MAX_ABS_LONGITUDE) {
60 4
            $newLongitude = ($longitude <=> 0) * Tree::ABS_LONGITUDE_LIMIT;
61
        }
62 12
        return $newLongitude;
63
    }
64
    
65
    /**
66
     * Get timezone name from a particular location (latitude, longitude)
67
     * @param $latitude
68
     * @param $longitude
69
     * @return string
70
     */
71 14
    public function getTimeZoneName($latitude, $longitude)
72
    {
73 14
        $timeZone = Tree::NONE_TIMEZONE;
74
        try {
75 14
            $latitude = $this->adjustLatitude($latitude);
76 12
            $longitude = $this->adjustLongitude($longitude);
77 10
            $timeZone = $this->quadrantTree->lookForTimezone($latitude, $longitude);
78 8
        }catch (ErrorException $error){
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
79 8
            echo $error->getMessage() . "\n";
80
        }
81 14
        return $timeZone;
82
    }
83
    
84
    /**
85
     * Get the local date belonging to a particular latitude, longitude and timestamp
86
     * @param $latitude
87
     * @param $longitude
88
     * @param $timestamp
89
     * @return DateTime
90
     */
91 2
    public function getLocalDate($latitude, $longitude, $timestamp)
92
    {
93 2
        $timeZone = $this->getTimeZoneName($latitude, $longitude);
94 2
        $date = new DateTime();
95 2
        $date->setTimestamp($timestamp);
96 2
        if ($timeZone != Tree::NONE_TIMEZONE) {
97 2
            $date->setTimezone(new DateTimeZone($timeZone));
98
        }
99 2
        return $date;
100
    }
101
    
102
    /**
103
     * Get timestamp from latitude, longitude and localTimestamp
104
     * @param $latitude
105
     * @param $longitude
106
     * @param $localTimestamp
107
     * @return mixed
108
     */
109 2
    public function getCorrectTimestamp($latitude, $longitude, $localTimestamp)
110
    {
111 2
        $timestamp = $localTimestamp;
112 2
        $timeZoneName = $this->getTimeZoneName($latitude, $longitude);
113 2
        if ($timeZoneName != Tree::NONE_TIMEZONE) {
114 2
            $date = new DateTime();
115 2
            $date->setTimestamp($localTimestamp);
116 2
            if ($timeZoneName != null) {
117 2
                $date->setTimezone(new DateTimeZone($timeZoneName));
118
            }
119 2
            $timestamp = $date->getOffset() != false ? $localTimestamp - $date->getOffset() : $localTimestamp;
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $date->getOffset() of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
120
        }
121 2
        return $timestamp;
122
    }
123
}
124
125