From abd6b82d159c00f14470eb0382bdad6bfb830779 Mon Sep 17 00:00:00 2001 From: overtrue Date: Tue, 28 Aug 2018 03:09:59 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/Calendar.php | 44 +++++++++------ tests/CalendarTest.php | 121 ++++++++++++++++++----------------------- 2 files changed, 79 insertions(+), 86 deletions(-) diff --git a/src/Calendar.php b/src/Calendar.php index 6ffa10d..f03ded1 100644 --- a/src/Calendar.php +++ b/src/Calendar.php @@ -809,9 +809,11 @@ public function dateDiff($date1, $date2) /** * 获取两个日期之间以年为单位的距离. + * * @param array $lunar1 * @param array $lunar2 - * @param bool $absolute + * @param bool $absolute + * * @return int */ public function diffInYears($lunar1, $lunar2, $absolute = false) @@ -836,20 +838,23 @@ public function diffInYears($lunar1, $lunar2, $absolute = false) if ($greaterLunar['lunar_month'] == $lessLunar['lunar_month']) { if ($greaterLunar['is_leap'] && !$lessLunar['is_leap']) { $monthAdjustFactor = 0; - } else if (!$greaterLunar['is_leap'] && $lessLunar['is_leap']) { + } elseif (!$greaterLunar['is_leap'] && $lessLunar['is_leap']) { $monthAdjustFactor = 1; } } $yearAdjustFactor = $greaterLunar['lunar_month'] - $monthAdjustFactor >= $lessLunar['lunar_month'] ? 0 : 1; $diff = $greaterLunar['lunar_year'] - $yearAdjustFactor - $lessLunar['lunar_year']; + return $absolute ? $diff : ($changed ? -1 * $diff : $diff); } /** * 获取两个日期之间以月为单位的距离. + * * @param array $lunar1 * @param array $lunar2 - * @param bool $absolute + * @param bool $absolute + * * @return int */ public function diffInMonths($lunar1, $lunar2, $absolute = false) @@ -877,14 +882,13 @@ public function diffInMonths($lunar1, $lunar2, $absolute = false) $lessLunarAdjustFactor = $lessLunar['is_leap'] || (0 < $leapMonth && $leapMonth < $lessLunar['lunar_month']) ? 1 : 0; $greaterLunarAdjustFactor = $greaterLunar['is_leap'] || (0 < $leapMonth && $leapMonth < $greaterLunar['lunar_month']) ? 1 : 0; $diff = $greaterLunar['lunar_month'] + $greaterLunarAdjustFactor - $lessLunar['lunar_month'] - $lessLunarAdjustFactor; - } else { $lessLunarLeapMonth = $this->leapMonth($lessLunar['lunar_year']); $greaterLunarLeapMonth = $this->leapMonth($greaterLunar['lunar_year']); $lessLunarAdjustFactor = (!$lessLunar['is_leap'] && $lessLunarLeapMonth == $lessLunar['lunar_month']) || $lessLunarLeapMonth > $lessLunar['lunar_month'] ? 1 : 0; $diff += 12 + $lessLunarAdjustFactor - $lessLunar['lunar_month']; - for ($i = $lessLunar['lunar_year'] + 1; $i < $greaterLunar['lunar_year']; $i++) { + for ($i = $lessLunar['lunar_year'] + 1; $i < $greaterLunar['lunar_year']; ++$i) { $diff += $this->monthsOfYear($i); } $greaterLunarAdjustFactor = $greaterLunar['is_leap'] || (0 < $greaterLunarLeapMonth && $greaterLunarLeapMonth < $greaterLunar['lunar_month']) ? 1 : 0; @@ -898,9 +902,11 @@ public function diffInMonths($lunar1, $lunar2, $absolute = false) /** * 获取两个日期之间以日为单位的距离. + * * @param array $lunar1 * @param array $lunar2 - * @param bool $absolute + * @param bool $absolute + * * @return int */ public function diffInDays($lunar1, $lunar2, $absolute = false) @@ -915,7 +921,7 @@ public function diffInDays($lunar1, $lunar2, $absolute = false) } /** - * 增加年数 + * 增加年数. * * @param array $lunar * @param int $value @@ -952,7 +958,7 @@ public function addYears($lunar, $value = 1, $overFlow = true) } /** - * 减少年数 + * 减少年数. * * @param array $lunar * @param int $value @@ -966,7 +972,7 @@ public function subYears($lunar, $value = 1, $overFlow = true) } /** - * 增加月数 + * 增加月数. * * @param array $lunar * @param int $value @@ -991,7 +997,7 @@ public function addMonths($lunar, $value = 1, $overFlow = true) $isLeap = $newMonth + $value == $leapMonth + ($isLeap ? 0 : 1); if ((!$currentIsLeap && $leapMonth == $newMonth) || ($newMonth < $leapMonth && $newMonth + $value > $leapMonth)) { - $value--; + --$value; } } else { $isLeap = false; @@ -1002,7 +1008,7 @@ public function addMonths($lunar, $value = 1, $overFlow = true) $value = 0; } else { $value = $value + $newMonth - 13; - $newYear++; + ++$newYear; $newMonth = 1; } @@ -1011,7 +1017,7 @@ public function addMonths($lunar, $value = 1, $overFlow = true) if ($newDay > $maxDays) { if ($overFlow) { $newDay = 1; - $value++; + ++$value; } else { $newDay = $maxDays; } @@ -1024,7 +1030,7 @@ public function addMonths($lunar, $value = 1, $overFlow = true) } /** - * 减少月数 + * 减少月数. * * @param array $lunar * @param int $value @@ -1050,7 +1056,7 @@ public function subMonths($lunar, $value = 1, $overFlow = true) $isLeap = $newMonth - $value == $leapMonth; if ($newMonth >= $leapMonth && $newMonth - $value < $leapMonth) { - $value--; + --$value; } } else { $isLeap = false; @@ -1061,7 +1067,7 @@ public function subMonths($lunar, $value = 1, $overFlow = true) $value = 0; } else { $value = $value - $newMonth; - $newYear--; + --$newYear; $newMonth = 12; } @@ -1078,12 +1084,13 @@ public function subMonths($lunar, $value = 1, $overFlow = true) if ($needOverFlow) { $ret = $this->addDays($ret, 1); } + return $ret; } } /** - * 增加天数 + * 增加天数. * * @param array $lunar * @param int $value @@ -1094,12 +1101,13 @@ public function addDays($lunar, $value = 1) { $solar = $this->lunar2solar($lunar['lunar_year'], $lunar['lunar_month'], $lunar['lunar_day'], $lunar['is_leap']); $date = $this->makeDate("{$solar['solar_year']}-{$solar['solar_month']}-{$solar['solar_day']}"); - $date->modify($value . ' day'); + $date->modify($value.' day'); + return $this->solar2lunar($date->format('Y'), $date->format('m'), $date->format('d')); } /** - * 减少天数 + * 减少天数. * * @param array $lunar * @param int $value diff --git a/tests/CalendarTest.php b/tests/CalendarTest.php index abcd08b..70b402f 100644 --- a/tests/CalendarTest.php +++ b/tests/CalendarTest.php @@ -2,7 +2,7 @@ /** * User: hao.li * Date: 2018/8/21 - * Time: 8:54 AM + * Time: 8:54 AM. */ namespace Overtrue\ChineseCalendar\Tests; @@ -12,7 +12,7 @@ class CalendarTest extends TestCase { - #region diffInYears + //region diffInYears public function testSameNormalDateDiffInYears() { @@ -36,7 +36,7 @@ public function testSameLeapDateDiffInYears() $this->assertEquals(0, $diff2); } - #region less month + //region less month public function testLessMonthLessDayNormalDateAndNormalDateDiffInYears() { @@ -51,7 +51,6 @@ public function testLessMonthLessDayNormalDateAndNormalDateDiffInYears() $this->assertEquals(10, $diff2a); } - public function testLessMonthLessDayNormalDateAndLeapDateDiffInYears() { $calendar = new Calendar(); @@ -104,7 +103,6 @@ public function testLessMonthEqualDayNormalDateAndNormalDateDiffInYears() $this->assertEquals(10, $diff2a); } - public function testLessMonthEqualDayNormalDateAndLeapDateDiffInYears() { $calendar = new Calendar(); @@ -157,7 +155,6 @@ public function testLessMonthGreaterDayNormalDateAndNormalDateDiffInYears() $this->assertEquals(10, $diff2a); } - public function testLessMonthGreaterDayNormalDateAndLeapDateDiffInYears() { $calendar = new Calendar(); @@ -197,9 +194,9 @@ public function testLessMonthGreaterDayLeapDateAndLeapDateDiffInYears() $this->assertEquals(35, $diff2a); } - #endregion less month + //endregion less month - #region equal month + //region equal month public function testEqualMonthLessDayNormalDateAndNormalDateDiffInYears() { @@ -214,7 +211,6 @@ public function testEqualMonthLessDayNormalDateAndNormalDateDiffInYears() $this->assertEquals(10, $diff2a); } - public function testEqualMonthLessDayNormalDateAndLeapDateDiffInYears() { $calendar = new Calendar(); @@ -254,7 +250,6 @@ public function testEqualMonthLessDayLeapDateAndLeapDateDiffInYears() $this->assertEquals(8, $diff2a); } - public function testEqualMonthEqualDayNormalDateAndNormalDateDiffInYears() { $calendar = new Calendar(); @@ -268,7 +263,6 @@ public function testEqualMonthEqualDayNormalDateAndNormalDateDiffInYears() $this->assertEquals(10, $diff2a); } - public function testEqualMonthEqualDayNormalDateAndLeapDateDiffInYears() { $calendar = new Calendar(); @@ -308,7 +302,6 @@ public function testEqualMonthEqualDayLeapDateAndLeapDateDiffInYears() $this->assertEquals(8, $diff2a); } - public function testEqualMonthGreaterDayNormalDateAndNormalDateDiffInYears() { $calendar = new Calendar(); @@ -322,7 +315,6 @@ public function testEqualMonthGreaterDayNormalDateAndNormalDateDiffInYears() $this->assertEquals(9, $diff2a); } - public function testEqualMonthGreaterDayNormalDateAndLeapDateDiffInYears() { $calendar = new Calendar(); @@ -362,9 +354,9 @@ public function testEqualMonthGreaterDayLeapDateAndLeapDateDiffInYears() $this->assertEquals(7, $diff2a); } - #endregion equal month + //endregion equal month - #region greater month + //region greater month public function testGreaterMonthLessDayNormalDateAndNormalDateDiffInYears() { @@ -379,7 +371,6 @@ public function testGreaterMonthLessDayNormalDateAndNormalDateDiffInYears() $this->assertEquals(9, $diff2a); } - public function testGreaterMonthLessDayNormalDateAndLeapDateDiffInYears() { $calendar = new Calendar(); @@ -419,7 +410,6 @@ public function testGreaterMonthLessDayLeapDateAndLeapDateDiffInYears() $this->assertEquals(2, $diff2a); } - public function testGreaterMonthEqualDayNormalDateAndNormalDateDiffInYears() { $calendar = new Calendar(); @@ -433,7 +423,6 @@ public function testGreaterMonthEqualDayNormalDateAndNormalDateDiffInYears() $this->assertEquals(9, $diff2a); } - public function testGreaterMonthEqualDayNormalDateAndLeapDateDiffInYears() { $calendar = new Calendar(); @@ -473,7 +462,6 @@ public function testGreaterMonthEqualDayLeapDateAndLeapDateDiffInYears() $this->assertEquals(2, $diff2a); } - public function testGreaterMonthGreaterDayNormalDateAndNormalDateDiffInYears() { $calendar = new Calendar(); @@ -487,7 +475,6 @@ public function testGreaterMonthGreaterDayNormalDateAndNormalDateDiffInYears() $this->assertEquals(9, $diff2a); } - public function testGreaterMonthGreaterDayNormalDateAndLeapDateDiffInYears() { $calendar = new Calendar(); @@ -527,13 +514,13 @@ public function testGreaterMonthGreaterDayLeapDateAndLeapDateDiffInYears() $this->assertEquals(2, $diff2a); } - #endregion greater month + //endregion greater month - #endregion diffInYears + //endregion diffInYears - #region diffInMonths + //region diffInMonths - #region different year less month less day + //region different year less month less day public function testDifferentYearLessMonthLessDayNormalYearNormalDateAndNormalYearNormalDateDiffInMonths() { @@ -910,9 +897,9 @@ public function testDifferentYearLessMonthLessDayLeapDateAndLeapDateDiffInMonths $this->assertEquals(64, $diff2a); } - #endregion different year less month less day + //endregion different year less month less day - #region different year less month equal day + //region different year less month equal day public function testDifferentYearLessMonthEqualDayNormalYearNormalDateAndNormalYearNormalDateDiffInMonths() { @@ -1289,9 +1276,9 @@ public function testDifferentYearLessMonthEqualDayLeapDateAndLeapDateDiffInMonth $this->assertEquals(64, $diff2a); } - #endregion different year less month equal day + //endregion different year less month equal day - #region different year less month greater day + //region different year less month greater day public function testDifferentYearLessMonthGreaterDayNormalYearNormalDateAndNormalYearNormalDateDiffInMonths() { @@ -1668,9 +1655,9 @@ public function testDifferentYearLessMonthGreaterDayLeapDateAndLeapDateDiffInMon $this->assertEquals(63, $diff2a); } - #endregion different year less month greater day + //endregion different year less month greater day - #region different year equal month less day + //region different year equal month less day public function testDifferentYearEqualMonthLessDayNormalYearNormalDateAndNormalYearNormalDateDiffInMonths() { @@ -2047,9 +2034,9 @@ public function testDifferentYearEqualMonthLessDayLeapDateAndLeapDateDiffInMonth $this->assertEquals(99, $diff2a); } - #endregion different year equal month less day + //endregion different year equal month less day - #region different year equal month equal day + //region different year equal month equal day public function testDifferentYearEqualMonthEqualDayNormalYearNormalDateAndNormalYearNormalDateDiffInMonths() { @@ -2426,9 +2413,9 @@ public function testDifferentYearEqualMonthEqualDayLeapDateAndLeapDateDiffInMont $this->assertEquals(99, $diff2a); } - #endregion different year equal month equal day + //endregion different year equal month equal day - #region different year equal month greater day + //region different year equal month greater day public function testDifferentYearEqualMonthGreaterDayNormalYearNormalDateAndNormalYearNormalDateDiffInMonths() { @@ -2805,9 +2792,9 @@ public function testDifferentYearEqualMonthGreaterDayLeapDateAndLeapDateDiffInMo $this->assertEquals(98, $diff2a); } - #endregion different year equal month greater day + //endregion different year equal month greater day - #region different year greater month less day + //region different year greater month less day public function testDifferentYearGreaterMonthLessDayNormalYearNormalDateAndNormalYearNormalDateDiffInMonths() { @@ -3184,9 +3171,9 @@ public function testDifferentYearGreaterMonthLessDayLeapYearLeapDateAndLeapDateD $this->assertEquals(70, $diff2a); } - #endregion different year greater month less day + //endregion different year greater month less day - #region different year greater month equal day + //region different year greater month equal day public function testDifferentYearGreaterMonthEqualDayNormalYearNormalDateAndNormalYearNormalDateDiffInMonths() { @@ -3563,9 +3550,9 @@ public function testDifferentYearGreaterMonthEqualDayLeapYearLeapDateAndLeapDate $this->assertEquals(70, $diff2a); } - #endregion different year greater month equal day + //endregion different year greater month equal day - #region different year greater month greater day + //region different year greater month greater day public function testDifferentYearGreaterMonthGreaterDayNormalYearNormalDateAndNormalYearNormalDateDiffInMonths() { @@ -3942,9 +3929,9 @@ public function testDifferentYearGreaterMonthGreaterDayLeapYearLeapDateAndLeapDa $this->assertEquals(69, $diff2a); } - #endregion different year greater month greater day + //endregion different year greater month greater day - #region same year less month less day + //region same year less month less day public function testSameYearLessMonthLessDayNormalYearNormalDateAndNormalDateDiffInMonths() { @@ -4066,10 +4053,9 @@ public function testSameYearLessMonthLessDayLeapYearLeapDateAndNormalDateGreater $this->assertEquals(3, $diff2a); } - #endregion same year less month less day - - #region same year less month equal day + //endregion same year less month less day + //region same year less month equal day public function testSameYearLessMonthEqualDayNormalYearNormalDateAndNormalDateDiffInMonths() { @@ -4191,9 +4177,9 @@ public function testSameYearLessMonthEqualDayLeapYearLeapDateAndNormalDateGreate $this->assertEquals(3, $diff2a); } - #endregion same year less month equal day + //endregion same year less month equal day - #region same year less month greater day + //region same year less month greater day public function testSameYearLessMonthGreaterDayNormalYearNormalDateAndNormalDateDiffInMonths() { @@ -4315,9 +4301,9 @@ public function testSameYearLessMonthGreaterDayLeapYearLeapDateAndNormalDateGrea $this->assertEquals(2, $diff2a); } - #endregion same year less month greater day + //endregion same year less month greater day - #region same year equal month less day + //region same year equal month less day public function testSameYearEqualMonthLessDayNormalYearNormalDateAndNormalDateDiffInMonths() { @@ -4409,9 +4395,9 @@ public function testSameYearEqualMonthLessDayLeapDateAndLeapDateDiffInMonths() $this->assertEquals(0, $diff2a); } - #endregion same year equal month less day + //endregion same year equal month less day - #region same year equal month equal day + //region same year equal month equal day public function testSameYearEqualMonthEqualDayNormalYearNormalDateAndNormalDateDiffInMonths() { @@ -4503,11 +4489,11 @@ public function testSameYearEqualMonthEqualDayLeapDateAndLeapDateDiffInMonths() $this->assertEquals(0, $diff2a); } - #endregion same year equal month equal day + //endregion same year equal month equal day - #endregion diffInMonths + //endregion diffInMonths - #region diffInDays + //region diffInDays public function testDiffInDays() { @@ -4524,9 +4510,9 @@ public function testDiffInDays() $this->assertEquals(9509, $diff2a); } - #endregion diffInDays + //endregion diffInDays - #region addYears + //region addYears public function testLastDayOfLeapMonthOverFlowAddYears() { @@ -4594,9 +4580,9 @@ public function testNormalMonthAddYearsToNormalMonth() $this->assertEquals(false, $newLunar['is_leap']); } - #endregion addYears + //endregion addYears - #region subYears + //region subYears public function testLastDayOfLeapMonthOverFlowSubYears() { @@ -4642,7 +4628,6 @@ public function testLastDayOfYearNotOverFlowSubYears() $this->assertEquals(false, $newLunar['is_leap']); } - public function testLeapMonthSubYearsToLeapMonth() { $calendar = new Calendar(); @@ -4654,9 +4639,9 @@ public function testLeapMonthSubYearsToLeapMonth() $this->assertEquals(true, $newLunar['is_leap']); } - #endregion subYears + //endregion subYears - #region addMonths + //region addMonths public function testAddMonthsLesserThanLeapMonth() { @@ -4768,9 +4753,9 @@ public function testLastDayOfNormalMonthNotOverFlowAddMonthsOverLeapMonthToNorma $this->assertEquals(false, $newLunar['is_leap']); } - #endregion addMonths + //endregion addMonths - #region subMonths + //region subMonths public function testSubMonthsGreaterThanLeapMonth() { @@ -4860,9 +4845,9 @@ public function testLastDayOfLeapMonthNotOverFlowSubMonthsOverLeapMonthToNormalM $this->assertEquals(false, $newLunar['is_leap']); } - #endregion subMonths + //endregion subMonths - #region addDays + //region addDays public function testAddDaysOverYears() { @@ -4875,9 +4860,9 @@ public function testAddDaysOverYears() $this->assertEquals(true, $newLunar['is_leap']); } - #endregion addDays + //endregion addDays - #region subDays + //region subDays public function testSubDaysOverYears() { @@ -4890,5 +4875,5 @@ public function testSubDaysOverYears() $this->assertEquals(false, $newLunar['is_leap']); } - #endregion subDays + //endregion subDays }