Skip to content

Commit

Permalink
Update error messages and tests for newer versions of PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
fisharebest committed May 31, 2022
1 parent 50010ea commit 77801e9
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 96 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/phpunit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-version: ['5.3', '5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
php-version: ['5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1']

steps:
- uses: shivammathur/setup-php@master
with:
php-version: ${{ matrix.php-version }}
coverage: pcov
coverage: xdebug

- uses: actions/checkout@v2

Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ CHANGE LOG
==========

## 2.6.0 (????-??-??)
- Update jdtounix() for PHP 7.3
- Update error handling to match latest versions of PHP.
- Add PHP 8 to the test matrix, and remove PHP 5.3 from it.

## 2.5.0 (2018-09-17)
- Calculate number of months in a given year
Expand Down
2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
<directory>test</directory>
</testsuite>
</testsuites>
<!--
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
-->
</phpunit>
63 changes: 46 additions & 17 deletions src/Shim.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@
namespace Fisharebest\ExtCalendar;

use InvalidArgumentException;

use ValueError;

use const PHP_INT_SIZE;
use const PHP_VERSION_ID;

class Shim
{
/** @var FrenchCalendar */
Expand Down Expand Up @@ -192,7 +188,11 @@ public static function calDaysInMonth($calendar_id, $month, $year)
return self::calDaysInMonthCalendar(self::$julian_calendar, $year, $month);

default:
return trigger_error('invalid calendar ID ' . $calendar_id, E_USER_WARNING);
if (PHP_VERSION_ID < 80000) {
return trigger_error('invalid calendar ID ' . $calendar_id, E_USER_WARNING);
}

throw new ValueError('cal_days_in_month(): Argument #1 ($calendar) must be a valid calendar ID');
}
}

Expand All @@ -210,9 +210,14 @@ private static function calDaysInMonthCalendar(CalendarInterface $calendar, $yea
try {
return $calendar->daysInMonth($year, $month);
} catch (InvalidArgumentException $ex) {
$error_msg = PHP_VERSION_ID < 70200 ? 'invalid date.' : 'invalid date';
if (PHP_VERSION_ID < 70200) {
return trigger_error('invalid date.', E_USER_WARNING);
}
if (PHP_VERSION_ID < 80000) {
return trigger_error('invalid date', E_USER_WARNING);
}

return trigger_error($error_msg, E_USER_WARNING);
throw new ValueError('Invalid date');
}
}

Expand All @@ -233,9 +238,14 @@ private static function calDaysInMonthFrench($year, $month)
}

if ($year > 14) {
$error_msg = PHP_VERSION_ID < 70200 ? 'invalid date.' : 'invalid date';
if (PHP_VERSION_ID < 70200) {
return trigger_error('invalid date.', E_USER_WARNING);
}
if (PHP_VERSION_ID < 80000) {
return trigger_error('invalid date', E_USER_WARNING);
}

return trigger_error($error_msg, E_USER_WARNING);
throw new ValueError('Invalid date');
}

return self::calDaysInMonthCalendar(self::$french_calendar, $year, $month);
Expand Down Expand Up @@ -279,7 +289,11 @@ public static function calFromJd($julian_day, $calendar_id)
return self::calFromJdCalendar($julian_day, self::jdToJulian($julian_day), self::$MONTH_NAMES, self::$MONTH_NAMES_SHORT);

default:
return trigger_error('invalid calendar ID ' . $calendar_id, E_USER_WARNING);
if (PHP_VERSION_ID < 80000) {
return trigger_error('invalid calendar ID ' . $calendar_id, E_USER_WARNING);
}

throw new ValueError('cal_from_jd(): Argument #2 ($calendar) must be a valid calendar ID');
}
}

Expand Down Expand Up @@ -347,7 +361,11 @@ public static function calInfo($calendar_id)
);

default:
return trigger_error('invalid calendar ID ' . $calendar_id, E_USER_WARNING);
if (PHP_VERSION_ID < 80000) {
return trigger_error('invalid calendar ID ' . $calendar_id, E_USER_WARNING);
}

throw new ValueError('cal_info(): Argument #1 ($calendar) must be a valid calendar ID');
}
}

Expand Down Expand Up @@ -403,7 +421,11 @@ public static function calToJd($calendar_id, $month, $day, $year)
return self::julianToJd($month, $day, $year);

default:
return trigger_error('invalid calendar ID ' . $calendar_id . '.', E_USER_WARNING);
if (PHP_VERSION_ID < 80000) {
return trigger_error('invalid calendar ID ' . $calendar_id, E_USER_WARNING);
}

throw new ValueError('cal_to_jd(): Argument #1 ($calendar) must be a valid calendar ID');
}
}

Expand All @@ -421,6 +443,9 @@ public static function calToJd($calendar_id, $month, $day, $year)
public static function easterDate($year)
{
if ($year < 1970 || $year > 2037) {
if (PHP_VERSION_ID >= 80000) {
throw new ValueError('easter_date(): Argument #1 ($year) must be between 1970 and 2037 (inclusive)');
}
return trigger_error('This function is only valid for years between 1970 and 2037 inclusive', E_USER_WARNING);
}

Expand Down Expand Up @@ -742,11 +767,11 @@ public static function jdToUnix($julian_day)
return ($julian_day - 2440588) * 86400;
}

if (PHP_VERSION_ID >= 80000) {
throw new ValueError('jday must be between 2440588 and ' . $upper_limit);
if (PHP_VERSION_ID < 80000) {
return false;
}

return false;
throw new ValueError('jday must be between 2440588 and ' . $upper_limit);
}

/**
Expand All @@ -759,7 +784,7 @@ public static function jdToUnix($julian_day)
public static function jdToUnixUpperLimit()
{
if (PHP_INT_SIZE === 2 || PHP_VERSION_ID < 70324 || PHP_VERSION_ID >= 70400 && PHP_VERSION_ID < 70412) {
return 2465443;
return 2465343;
}

return 106751993607888;
Expand Down Expand Up @@ -823,7 +848,11 @@ public static function julianToJd($month, $day, $year)
public static function unixToJd($timestamp)
{
if ($timestamp < 0) {
return false;
if (PHP_VERSION_ID < 80000) {
return false;
}

throw new ValueError('unixtojd(): Argument #1 ($timestamp) must be greater than or equal to 0');
}

// Convert timestamp based on local timezone
Expand Down

0 comments on commit 77801e9

Please sign in to comment.