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 841e157
Show file tree
Hide file tree
Showing 24 changed files with 4,502 additions and 250 deletions.
6 changes: 3 additions & 3 deletions lib/Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ public function main(array $argv): int
// jcard/jcal documents
case 'jcard':
case 'jcal':
// specific document versions
// specific document versions
case 'vcard21':
case 'vcard30':
case 'vcard40':
case 'icalendar20':
// specific formats
// specific formats
case 'json':
case 'mimedir':
// icalendar/vcard
// icalendar/vcard
case 'icalendar':
case 'vcard':
$this->format = $value;
Expand Down
4 changes: 2 additions & 2 deletions lib/Parser/MimeDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Sabre\VObject\InvalidDataException;
use Sabre\VObject\Node;
use Sabre\VObject\ParseException;
use Sabre\VObject\Reader;
use Sabre\VObject\Property;
use Sabre\VObject\Reader;

/**
* MimeDir parser.
Expand Down Expand Up @@ -223,7 +223,7 @@ protected function parseLine(string $line)
) {
// Fix unfolding
$component->remove($prevNode);
$value = $prevNode->getValue() . ' ' . $line . PHP_EOL;
$value = $prevNode->getValue().' '.$line.PHP_EOL;
$prevNode->offsetSet('VALUE', $value);
$prevNode->setValue($value);
$component->add($prevNode);
Expand Down
1 change: 0 additions & 1 deletion lib/Parser/XML.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ protected function parseComponent(Component $parentComponent): void
/**
* Create a property.
*
* @param mixed $value
* @throws InvalidDataException
*/
protected function createProperty(Component $parentComponent, string $name, array $parameters, string $type, $value): void
Expand Down
2 changes: 1 addition & 1 deletion lib/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Reader
/**
* If this option is turned on, it will fix unfolding parse error by adding empty space.
*/
const OPTION_FIX_UNFOLDING = 4;
public const OPTION_FIX_UNFOLDING = 4;

/**
* Parses a vCard or iCalendar object, and returns the top component.
Expand Down
40 changes: 14 additions & 26 deletions lib/Recur/RRuleIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Sabre\VObject\Recur;

use DateTimeInterface;
use DateTimeImmutable;
use Iterator;
use Sabre\VObject\DateTimeParser;
use Sabre\VObject\InvalidDataException;
use Sabre\VObject\Property;
Expand All @@ -29,7 +26,7 @@ class RRuleIterator implements \Iterator
* we want to iterate. The value is a unix timestamp and currently
* corresponds to the datetime 9999-12-31 11:59:59 UTC.
*/
public const dateUpperLimit = 253402300799;
public const int dateUpperLimit = 253402300799;

/**
* Creates the Iterator.
Expand Down Expand Up @@ -63,7 +60,7 @@ public function current(): ?\DateTimeInterface
#[\ReturnTypeWillChange]
public function key(): int
{
return $this->counter;
return (int) $this->counter;
}

/**
Expand All @@ -86,8 +83,6 @@ public function valid(): bool

/**
* Resets the iterator.
*
* @return void
*/
#[\ReturnTypeWillChange]
public function rewind(): void
Expand All @@ -98,8 +93,6 @@ public function rewind(): void

/**
* Goes on to the next iteration.
*
* @param int $amount
*/
#[\ReturnTypeWillChange]
public function next(int $amount = 1): void
Expand Down Expand Up @@ -155,7 +148,7 @@ public function fastForward(\DateTimeInterface $dt): void
/**
* This method allows you to quickly go to the next occurrence before the specified date.
*/
public function fastForwardBefore(DateTimeInterface $dt)
public function fastForwardBefore(\DateTimeInterface $dt): void
{
$hasCount = isset($this->count);

Expand All @@ -179,7 +172,7 @@ public function fastForwardBefore(DateTimeInterface $dt)
/**
* This method allows you to quickly go to the last occurrence.
*/
public function fastForwardToEnd()
public function fastForwardToEnd(): void
{
if ($this->isInfinite()) {
throw new \LogicException('Cannot fast forward to the end an infinite event.');
Expand All @@ -202,22 +195,22 @@ public function fastForwardToEnd()
$this->currentDate = $previous;
}

public function getCount()
public function getCount(): ?int
{
return $this->count;
}

public function getInterval()
public function getInterval(): int
{
return $this->interval;
}

public function getUntil()
public function getUntil(): ?\DateTimeInterface
{
return $this->until;
}

public function getFrequency()
public function getFrequency(): string
{
return $this->frequency;
}
Expand Down Expand Up @@ -257,7 +250,7 @@ private function getFrequencyCoeff()
* recurrence rule. Will set the position of the iterator to the last occurrence before the requested date. If the
* fast forwarding failed, the position will be reset.
*/
private function jumpForward(DateTimeInterface $dt)
private function jumpForward(\DateTimeInterface $dt): void
{
$frequencyCoeff = $this->getFrequencyCoeff();

Expand Down Expand Up @@ -364,7 +357,7 @@ private function jumpForward(DateTimeInterface $dt)
*
* You can get this number with the key() method.
*/
protected int $counter = 0;
protected float $counter = 0;

/**
* Which weekdays to recur.
Expand Down Expand Up @@ -618,11 +611,6 @@ protected function nextMonthly($amount = 1): void
(int) $this->currentDate->format('n'),
1
);
//
// This line does not currently work in hhvm. Temporary workaround
// follows:
// $this->currentDate->modify('first day of this month');
// $this->currentDate = new \DateTimeImmutable($this->currentDate->format('Y-m-1 H:i:s'), $this->currentDate->getTimezone());
// end of workaround
$this->currentDate = $this->currentDate->modify('+ '.($amount * $this->interval).' months');
$amount = 1;
Expand Down Expand Up @@ -1014,9 +1002,9 @@ protected function parseRRule($rrule): void
}

if (
(isset($this->byWeekNo) && $this->frequency !== 'yearly') ||
(isset($this->byYearDay) && in_array($this->frequency, ['daily', 'weekly', 'monthly'], true)) ||
(isset($this->byMonthDay) && $this->frequency === 'weekly')
(isset($this->byWeekNo) && 'yearly' !== $this->frequency)
|| (isset($this->byYearDay) && in_array($this->frequency, ['daily', 'weekly', 'monthly'], true))
|| (isset($this->byMonthDay) && 'weekly' === $this->frequency)
) {
throw new InvalidDataException('Invalid combination of FREQ with BY rules');
}
Expand Down Expand Up @@ -1172,7 +1160,7 @@ protected function getMonthlyOccurrences(): array
*
* @return array an array of arrays with the day of the month, hours, minute and seconds of the occurence
*/
protected function addDailyOccurences(array $result)
protected function addDailyOccurences(array $result): array
{
$output = [];
$hour = (int) $this->currentDate->format('G');
Expand Down
3 changes: 2 additions & 1 deletion lib/TimeZoneUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Sabre\VObject\TimezoneGuesser\FindFromOutlookCities;
use Sabre\VObject\TimezoneGuesser\FindFromTimezoneIdentifier;
use Sabre\VObject\TimezoneGuesser\FindFromTimezoneMap;
use Sabre\VObject\TimezoneGuesser\GuessFromCustomizedTimeZone;
use Sabre\VObject\TimezoneGuesser\GuessFromLicEntry;
use Sabre\VObject\TimezoneGuesser\GuessFromMsTzId;
use Sabre\VObject\TimezoneGuesser\GuessFromCustomizedTimeZone;
use Sabre\VObject\TimezoneGuesser\LowercaseTimezoneIdentifier;
use Sabre\VObject\TimezoneGuesser\TimezoneFinder;
use Sabre\VObject\TimezoneGuesser\TimezoneGuesser;
Expand Down Expand Up @@ -47,6 +47,7 @@ private function __construct()
$this->addFinder('version', new FindFromMzVersionTimezone());
$this->addFinder('offsetName', new FindFromOffsetName());
}

private static function getInstance(): self
{
if (null === self::$instance) {
Expand Down
9 changes: 4 additions & 5 deletions lib/TimezoneGuesser/FindFromMzVersionTimezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Sabre\VObject\TimezoneGuesser;

use DateTimeZone;
use Sabre\VObject\TimeZoneUtil;

/**
Expand All @@ -14,21 +13,21 @@
*/
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;
}

$trailingChar = (int) $tzid[strlen($tzid)-1];
$trailingChar = (int) $tzid[strlen($tzid) - 1];
if ($trailingChar <= 9 && $trailingChar >= 1) {
$emptySpace = strrpos($tzid, ' ');
if ($emptySpace === false) {
if (false === $emptySpace) {
return null;
}

$tz = TimeZoneUtil::getTimeZone(substr($tzid, 0, $emptySpace));
if ($tz->getName() === 'UTC') {
if ('UTC' === $tz->getName()) {
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
14 changes: 6 additions & 8 deletions lib/TimezoneGuesser/FindFromOutlookCities.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@

namespace Sabre\VObject\TimezoneGuesser;

use DateTimeZone;

class FindFromOutlookCities implements TimezoneFinder
{
/**
* Example: TZID:(UTC+01:00) Bruxelles\, København\, Madrid\, Paris
* 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) {
if (0 === $count) {
return null;
}

Expand All @@ -25,16 +23,16 @@ public function find(string $tzid, bool $failIfUncertain = false): ?DateTimeZone

$cities = explode(', ', $tzid);

if (count($cities) === 1) {
if (1 === count($cities)) {
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
4 changes: 2 additions & 2 deletions lib/TimezoneGuesser/FindFromTimezoneMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FindFromTimezoneMap implements TimezoneFinder

public function find(string $tzid, ?bool $failIfUncertain = false): ?\DateTimeZone
{
$tzid = str_replace(".", "", $tzid);
$tzid = str_replace('.', '', $tzid);

// Next, we check if the tzid is somewhere in our tzid map.
if ($this->hasTzInMap($tzid)) {
Expand Down Expand Up @@ -61,7 +61,7 @@ private function getTzMaps(): array
include __DIR__.'/../timezonedata/extrazones.php'
);
$this->map = array_combine(
array_map(static fn (string $key) => str_replace(".", "", mb_strtolower($key, 'UTF-8')), array_keys($map)),
array_map(static fn (string $key) => str_replace('.', '', mb_strtolower($key, 'UTF-8')), array_keys($map)),
array_values($map),
);
}
Expand Down
18 changes: 8 additions & 10 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') {
if (null === $vtimezone->TZID || 'Customized Time Zone' !== $vtimezone->TZID->getValue()) {
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 Expand Up @@ -88,8 +86,8 @@ private function parseOffsetToInteger(string $offset): int

$time = $time * 60;

if ($offset[0] === "-") {
$time = $time *-1;
if ('-' === $offset[0]) {
$time = $time * -1;
}

return $time;
Expand Down
Loading

0 comments on commit 841e157

Please sign in to comment.