ArabicCalendar   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 120
Duplicated Lines 1.67 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 2
loc 120
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A daysInMonth() 0 10 5
A daysInWeek() 0 4 1
A gedcomCalendarEscape() 0 4 1
A isLeapYear() 0 4 1
A jdEnd() 0 4 1
A jdStart() 0 4 1
A jdToYmd() 0 8 1
A monthsInYear() 0 4 1
A ymdToJd() 2 8 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 Fisharebest\ExtCalendar;
3
4
use InvalidArgumentException;
5
6
/**
7
 * Class ArabicCalendar - calculations for the Arabic (Hijri) calendar.
8
 *
9
 * @author    Greg Roach <[email protected]>
10
 * @copyright (c) 2014-2020 Greg Roach
11
 * @license   This program is free software: you can redistribute it and/or modify
12
 *            it under the terms of the GNU General Public License as published by
13
 *            the Free Software Foundation, either version 3 of the License, or
14
 *            (at your option) any later version.
15
 *
16
 *            This program is distributed in the hope that it will be useful,
17
 *            but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *            GNU General Public License for more details.
20
 *
21
 *            You should have received a copy of the GNU General Public License
22
 *            along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
class ArabicCalendar implements CalendarInterface
25
{
26
    /**
27
     * Determine the number of days in a specified month, allowing for leap years, etc.
28
     *
29
     * @param int $year
30
     * @param int $month
31
     *
32
     * @return int
33
     */
34
    public function daysInMonth($year, $month)
35
    {
36
        if ($month === 2) {
37
            return 28;
38
        } elseif ($month % 2 === 1 || $month === 12 && $this->isLeapYear($year)) {
39
            return 30;
40
        } else {
41
            return 29;
42
        }
43
    }
44
45
    /**
46
     * Determine the number of days in a week.
47
     *
48
     * @return int
49
     */
50
    public function daysInWeek()
51
    {
52
        return 7;
53
    }
54
55
    /**
56
     * The escape sequence used to indicate this calendar in GEDCOM files.
57
     *
58
     * @return string
59
     */
60
    public function gedcomCalendarEscape()
61
    {
62
        return '@#DHIJRI@';
63
    }
64
65
    /**
66
     * Determine whether or not a given year is a leap-year.
67
     *
68
     * @param int $year
69
     *
70
     * @return bool
71
     */
72
    public function isLeapYear($year)
73
    {
74
        return ((11 * $year + 14) % 30) < 11;
75
    }
76
77
    /**
78
     * What is the highest Julian day number that can be converted into this calendar.
79
     *
80
     * @return int
81
     */
82
    public function jdEnd()
83
    {
84
        return PHP_INT_MAX;
85
    }
86
87
    /**
88
     * What is the lowest Julian day number that can be converted into this calendar.
89
     *
90
     * @return int
91
     */
92
    public function jdStart()
93
    {
94
        return 1948440; // 1 Muharram 1 AH, 16 July 622 AD
95
    }
96
97
    /**
98
     * Convert a Julian day number into a year/month/day.
99
     *
100
     * @param int $julian_day
101
     *
102
     * @return int[]
103
     */
104
    public function jdToYmd($julian_day)
105
    {
106
        $year  = (int) ((30 * ($julian_day - 1948440) + 10646) / 10631);
107
        $month = (int) ((11 * ($julian_day - $year * 354 - (int) ((3 + 11 * $year) / 30) - 1948086) + 330) / 325);
108
        $day   = $julian_day - 29 * ($month - 1) - (int) ((6 * $month - 1) / 11) - $year * 354 - (int) ((3 + 11 * $year) / 30) - 1948085;
109
110
        return array($year, $month, $day);
111
    }
112
113
    /**
114
     * Determine the number of months in a year (if given),
115
     * or the maximum number of months in any year.
116
     *
117
     * @param int|null $year
118
     *
119
     * @return int
120
     */
121
    public function monthsInYear($year = null)
122
    {
123
        return 12;
124
    }
125
126
    /**
127
     * Convert a year/month/day to a Julian day number.
128
     *
129
     * @param int $year
130
     * @param int $month
131
     * @param int $day
132
     *
133
     * @return int
134
     */
135
    public function ymdToJd($year, $month, $day)
136
    {
137 View Code Duplication
        if ($month < 1 || $month > $this->monthsInYear()) {
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...
138
            throw new InvalidArgumentException('Month ' . $month . ' is invalid for this calendar');
139
        }
140
141
        return $day + 29 * ($month - 1) + (int) ((6 * $month - 1) / 11) + $year * 354 + (int) ((3 + 11 * $year) / 30) + 1948085;
142
    }
143
}
144