Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Szu committed Dec 28, 2023
1 parent 7fc1528 commit 247586d
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 102 deletions.
4 changes: 1 addition & 3 deletions lib/Recur/RRuleIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace Sabre\VObject\Recur;

use DateTimeInterface;
use DateTimeImmutable;
use Iterator;
use Sabre\VObject\DateTimeParser;
use Sabre\VObject\InvalidDataException;
use Sabre\VObject\Property;
Expand Down Expand Up @@ -292,7 +290,7 @@ private function jumpForward(DateTimeInterface $dt)
isset($previousDate) && $this->currentDate = clone $previousDate;

// We don't know the counter at this point anymore
$this->counter = NAN;
$this->counter = (int) NAN;

// It's possible that we miss the previous occurrence by jumping too much, in this case we reset the rrule and
// do the normal forward.
Expand Down
2 changes: 1 addition & 1 deletion lib/TimezoneGuesser/FindFromMzVersionTimezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class FindFromMzVersionTimezone implements TimezoneFinder
{
public function find(string $tzid, bool $failIfUncertain = false): ?DateTimeZone
public function find(string $tzid, ?bool $failIfUncertain = false): ?DateTimeZone
{
if (strlen($tzid) < 1) {
return null;
Expand Down
10 changes: 4 additions & 6 deletions lib/TimezoneGuesser/FindFromOffsetName.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Sabre\VObject\TimezoneGuesser;

use DateTimeZone;

class FindFromOffsetName implements TimezoneFinder
{
public static $offsetTimezones = [
public static array $offsetTimezones = [
'+01:00' => 'Africa/Lagos',
'+02:00' => 'Africa/Cairo',
'+03:00' => 'Europe/Moscow',
Expand All @@ -35,17 +33,17 @@ class FindFromOffsetName implements TimezoneFinder
'-11:00' => 'Pacific/Niue',
];

public function find(string $tzid, bool $failIfUncertain = false): ?DateTimeZone
public function find(string $tzid, ?bool $failIfUncertain = false): ?\DateTimeZone
{
// only handle number timezone
if (strlen($tzid) > 6) {
return null;
}

try {
$tzid = new DateTimeZone($tzid);
$tzid = new \DateTimeZone($tzid);

return new DateTimeZone(self::$offsetTimezones[$tzid->getName()]) ?? null;
return new \DateTimeZone(self::$offsetTimezones[$tzid->getName()]) ?? null;
} catch (\Exception $e) {
return null;
}
Expand Down
8 changes: 3 additions & 5 deletions lib/TimezoneGuesser/FindFromOutlookCities.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

namespace Sabre\VObject\TimezoneGuesser;

use DateTimeZone;

class FindFromOutlookCities implements TimezoneFinder
{
/**
* Example: TZID:(UTC+01:00) Bruxelles\, København\, Madrid\, Paris
*/
public function find(string $tzid, bool $failIfUncertain = false): ?DateTimeZone
public function find(string $tzid, ?bool $failIfUncertain = false): ?\DateTimeZone
{
$tzid = preg_replace('/TZID:\(UTC(\+|\-)\d{2}:\d{2}\)/', '', $tzid, -1, $count);
if ($count === 0) {
Expand All @@ -29,12 +27,12 @@ public function find(string $tzid, bool $failIfUncertain = false): ?DateTimeZone
return null;
}

$tzIdentifiers = DateTimeZone::listIdentifiers();
$tzIdentifiers = \DateTimeZone::listIdentifiers();

foreach ($cities as $city) {
foreach ($tzIdentifiers as $tzIdentifier) {
if (str_contains(strtolower($tzIdentifier), strtolower($city))) {
return new DateTimeZone($tzIdentifier);
return new \DateTimeZone($tzIdentifier);
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions lib/TimezoneGuesser/GuessFromCustomizedTimeZone.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@

namespace Sabre\VObject\TimezoneGuesser;

use DateTimeZone;
use DateTimeImmutable;
use Sabre\VObject\Component\VTimeZone;
use Sabre\VObject\Recur\RRuleIterator;
use Sabre\VObject\TimeZoneUtil;

class GuessFromCustomizedTimeZone implements TimezoneGuesser
{
public function guess(VTimeZone $vtimezone, bool $failIfUncertain = false): ?DateTimeZone
public function guess(VTimeZone $vtimezone, ?bool $failIfUncertain = false): ?\DateTimeZone
{
if (null === $vtimezone->TZID || $vtimezone->TZID->getValue() !== 'Customized Time Zone') {
return null;
}

$timezones = DateTimeZone::listIdentifiers();
$timezones = \DateTimeZone::listIdentifiers();
$standard = $vtimezone->STANDARD;
$daylight = $vtimezone->DAYLIGHT;
if (!$standard) {
Expand All @@ -34,8 +32,8 @@ public function guess(VTimeZone $vtimezone, bool $failIfUncertain = false): ?Dat
$standardRRule = $standard->RRULE ? $standard->RRULE->getValue() : 'FREQ=DAILY';
// The guess will not be perfectly matched since we use the timezone data of the current year
// It might be wrong if the timezone data changed in the past
$year = (new DateTimeImmutable('now'))->format('Y');
$start = new DateTimeImmutable($year . '-01-01');
$year = (new \DateTimeImmutable('now'))->format('Y');
$start = new \DateTimeImmutable($year . '-01-01');
$standardIterator = new RRuleIterator($standardRRule, $start);
$standardIterator->next();

Expand All @@ -48,7 +46,7 @@ public function guess(VTimeZone $vtimezone, bool $failIfUncertain = false): ?Dat
$daylightIterator && $daylightIterator->next();

foreach ($timezones as $timezone) {
$tz = new DateTimeZone($timezone);
$tz = new \DateTimeZone($timezone);
// check standard
$timestamp = $standardIterator->current()->getTimestamp();
$transitions = $tz->getTransitions($timestamp, $timestamp + 1);
Expand Down
8 changes: 3 additions & 5 deletions lib/TimezoneGuesser/LowercaseTimezoneIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

namespace Sabre\VObject\TimezoneGuesser;

use DateTimeZone;

class LowercaseTimezoneIdentifier implements TimezoneFinder
{
public function find(string $tzid, bool $failIfUncertain = false): ?DateTimeZone
public function find(string $tzid, ?bool $failIfUncertain = false): ?\DateTimeZone
{
foreach (DateTimeZone::listIdentifiers() as $timezone) {
foreach (\DateTimeZone::listIdentifiers() as $timezone) {
if (strtolower($tzid) === strtolower($timezone)) {
return new DateTimeZone($timezone);
return new \DateTimeZone($timezone);
}
}

Expand Down
5 changes: 0 additions & 5 deletions tests/VObject/Property/ICalendar/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,6 @@ public function testGetDateTimeBadTimeZone(): void
$this->vcal->add($event);
$this->vcal->add($timezone);

$dt = $elem->getDateTime();

self::assertInstanceOf('DateTimeImmutable', $dt);
self::assertEquals('1985-07-04 01:30:00', $dt->format('Y-m-d H:i:s'));
self::assertEquals('Canada/Eastern', $dt->getTimeZone()->getName());
date_default_timezone_set($default);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('We were unable to determine the correct PHP timezone for tzid: Moon');
Expand Down
2 changes: 1 addition & 1 deletion tests/VObject/Recur/EventIterator/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testValues(): void
/** @var VEvent $ev */
$ev = $vcal->createComponent('VEVENT');
$ev->UID = 'bla';
$ev->RRULE = 'FREQ=DAILY;BYHOUR=10;BYMINUTE=5;BYSECOND=16;BYWEEKNO=32;BYYEARDAY=100,200';
$ev->RRULE = 'FREQ=DAILY;BYHOUR=10;BYMINUTE=5;BYSECOND=16';
/** @var DateTime $dtStart */
$dtStart = $vcal->createProperty('DTSTART');
$dtStart->setDateTime(new \DateTimeImmutable('2011-10-07'));
Expand Down
Loading

0 comments on commit 247586d

Please sign in to comment.