CalendarTime::setMinutes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
class CalendarTime
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
    // Constants
7
8
    private static $hours_max = 23;
9
    private static $minutes_max = 59;
10
    private static $seconds_max = 59;
11
12
    // Attributes
13
14
    private $hours;
15
    private $minutes;
16
    private $seconds;
17
18
    // Constructor
19
20
    public function __construct($hours = 0, $minutes = 0, $seconds = 0)
21
    {
22
        $this->setHours($hours);
23
        $this->setMinutes($minutes);
24
        $this->setSeconds($seconds);
25
    }
26
    
27
    // Functions
28
29
    public function getHours()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
30
    {
31
        return $this->hours;
32
    }
33
    
34
    public function getMinutes()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
35
    {
36
        return $this->minutes;
37
    }
38
    
39
    public function getSeconds()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
40
    {
41
        return $this->seconds;
42
    }
43
    
44
    public function setHours($hours)
45
    {
46
        $this->setAttribute('hours', $hours, self::$hours_max);
47
    }
48
    
49
    public function setMinutes($minutes)
50
    {
51
        $this->setAttribute('minutes', $minutes, self::$minutes_max);
52
    }
53
    
54
    public function setSeconds($seconds)
55
    {
56
        $this->setAttribute('seconds', $seconds, self::$seconds_max);
57
    }
58
    
59
    public function __less(CalendarTime $time)
60
    {
61
        return $this->hours < $time->hours || ($this->hours == $time->hours && ($this->minutes < $time->minutes || ($this->minutes == $time->minutes && $this->seconds < $time->seconds)));
62
    }
63
    
64
    public function add(CalendarTime $time)
65
    {
66
        $hours = $this->hours + $time->hours;
67
        $minutes = $this->minutes + $time->minutes;
68
        $seconds = $this->seconds + $time->seconds;
69
        
70 View Code Duplication
        if ($seconds > self::$seconds_max) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
71
            $factor = self::$seconds_max + 1;
72
            $minutes += intval($seconds / $factor);
73
            $seconds %= $factor;
74
        }
75 View Code Duplication
        if ($minutes > self::$minutes_max) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
76
            $factor = self::$minutes_max + 1;
77
            $hours += intval($minutes / $factor);
78
            $minutes %= $factor;
79
        }
80
        $factor = self::$hours_max + 1;
81
        $hours %= $factor;
82
        
83
        return new CalendarTime($hours, $minutes, $seconds);
84
    }
85
    
86
    // Private Functions
87
88
    private function setAttribute($name, $value, $max)
89
    {
90
        if (is_numeric($value)) {
91
            if (is_int($value + 0)) {
92
                if ($value >= 0 && $value <= $max) {
93
                    $this->$name = $value;
94
                } elseif ($value < 0) {
95
                    user_error("CalendarTime::setAttribute() : you cannot set the \$$name attribute with a negative value", E_USER_ERROR);
96
                } else {
97
                    user_error("CalendarTime::setAttribute() : you cannot set the \$$name attribute with a value more important than $max", E_USER_ERROR);
98
                }
99
            } else {
100
                user_error("CalendarTime::setAttribute() : you cannot set the \$$name attribute with a non integer value", E_USER_ERROR);
101
            }
102
        } else {
103
            user_error("CalendarTime::setAttribute() : you cannot set the \$$name attribute with a non numeric value", E_USER_ERROR);
104
        }
105
    }
106
}
107