Skip to content

Commit

Permalink
Fix insufficient type detection for FREQ and WKST
Browse files Browse the repository at this point in the history
In some cases non-string types would end up passed to strtoupper
which causes a deprecation warning from PHP 8.3.
There might be other places where stricter type check is needed
Ref #149
  • Loading branch information
rlanvin committed Jun 23, 2024
1 parent 4b19d8e commit a2dd785
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
php: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
name: PHP ${{ matrix.php }}
steps:
- name: Checkout
Expand Down
36 changes: 20 additions & 16 deletions src/RRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,34 +219,38 @@ public function __construct($parts, $dtstart = null)
$this->rule = $parts; // save original rule

// WKST
$parts['WKST'] = strtoupper($parts['WKST']);
if (! array_key_exists($parts['WKST'], self::WEEKDAYS)) {
if (is_string($parts['WKST'])) {
$parts['WKST'] = strtoupper($parts['WKST']);
if (array_key_exists($parts['WKST'], self::WEEKDAYS)) {
$this->wkst = self::WEEKDAYS[$parts['WKST']];
}
}

if (!$this->wkst) {
throw new \InvalidArgumentException(
'The WKST rule part must be one of the following: '
.implode(', ',array_keys(self::WEEKDAYS))
);
}
$this->wkst = self::WEEKDAYS[$parts['WKST']];

// FREQ
if (is_integer($parts['FREQ'])) {
if ($parts['FREQ'] > self::SECONDLY || $parts['FREQ'] < self::YEARLY) {
throw new \InvalidArgumentException(
'The FREQ rule part must be one of the following: '
.implode(', ',array_keys(self::FREQUENCIES))
);
if ($parts['FREQ'] <= self::SECONDLY && $parts['FREQ'] >= self::YEARLY) {
$this->freq = $parts['FREQ'];
}
$this->freq = $parts['FREQ'];
}
else { // string
elseif (is_string($parts['FREQ'])) {
$parts['FREQ'] = strtoupper($parts['FREQ']);
if (! array_key_exists($parts['FREQ'], self::FREQUENCIES)) {
throw new \InvalidArgumentException(
'The FREQ rule part must be one of the following: '
.implode(', ',array_keys(self::FREQUENCIES))
);
if (array_key_exists($parts['FREQ'], self::FREQUENCIES)) {
$this->freq = self::FREQUENCIES[$parts['FREQ']];
}
$this->freq = self::FREQUENCIES[$parts['FREQ']];
}

if (!$this->freq) {
throw new \InvalidArgumentException(
'The FREQ rule part must be one of the following: '
.implode(', ',array_keys(self::FREQUENCIES))
);
}

// INTERVAL
Expand Down
9 changes: 8 additions & 1 deletion tests/RRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ public function invalidRules()
array(array()),
array(array('FOOBAR' => 'DAILY')),

array(array('FREQ' => 'foobar')),
'invalid string freq' => [['FREQ' => 'foobar']],
'Invalid integer frequency' => [['FREQ' => 42]],
'Array freq' => [['FREQ' => array()]],
'null freq' => [['FREQ' => null]],
'object freq' => [['FREQ' => new Stdclass()]],

array(array('FREQ' => 'DAILY', 'INTERVAL' => -1)),
array(array('FREQ' => 'DAILY', 'INTERVAL' => 1.5)),
array(array('FREQ' => 'DAILY', 'UNTIL' => 'foobar')),
Expand Down Expand Up @@ -87,6 +91,9 @@ public function invalidRules()
array(array('FREQ' => 'MONTHLY', 'BYSECOND' => 61)),

'Invalid WKST' => [['FREQ' => 'DAILY', 'WKST' => 'XX']],
'Null WKST' => [['FREQ' => 'DAILY', 'WKST' => null]],
'Array WKST' => [['FREQ' => 'DAILY', 'WKST' => array()]],
'Object WKST' => [['FREQ' => 'DAILY', 'WKST' => new stdClass()]],

'Invalid DTSTART (invalid date)' => [['FREQ' => 'DAILY', 'DTSTART' => new stdClass()]]
);
Expand Down

0 comments on commit a2dd785

Please sign in to comment.