Skip to content

Commit

Permalink
Simplify type check
Browse files Browse the repository at this point in the history
  • Loading branch information
alies-dev committed Jan 28, 2024
1 parent 181f6be commit 44fb0c0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 40 deletions.
7 changes: 4 additions & 3 deletions src/Generators/Ics.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/**
* @see https://icalendar.org/RFC-Specifications/iCalendar-RFC-5545/
* @psalm-type IcsOptions = array{UID?: string, URL?: string, PRODID?: string, REMINDER?: array{DESCRIPTION?: string, TIME?: \DateTimeInterface}}
* @psalm-type IcsPresentationOptions = array{format?: self::FORMAT_*}
*/
class Ics implements Generator
{
Expand All @@ -19,15 +20,15 @@ class Ics implements Generator

protected string $dateTimeFormat = 'Ymd\THis\Z';

/** @var IcsOptions */
/** @psalm-var IcsOptions */
protected array $options = [];

/** @var array{format?: self::FORMAT_*} */
/** @psalm-var IcsPresentationOptions */
protected $presentationOptions = [];

/**
* @param IcsOptions $options Optional ICS properties and components
* @param array{format?: self::FORMAT_*} $presentationOptions
* @param IcsPresentationOptions $presentationOptions
*/
public function __construct(array $options = [], array $presentationOptions = [])
{
Expand Down
14 changes: 5 additions & 9 deletions src/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

/**
* @psalm-import-type IcsOptions from \Spatie\CalendarLinks\Generators\Ics
* @psalm-import-type IcsPresentationOptions from \Spatie\CalendarLinks\Generators\Ics
*/
class Link
{
Expand Down Expand Up @@ -56,17 +57,13 @@ public static function createAllDay(string $title, \DateTimeInterface $fromDate,
// In cases where the from date is not UTC, make sure it's UTC, size all day events are floating and non UTC dates cause bugs in the generators
if ($fromDate->getTimezone() !== new \DateTimeZone('UTC')) {
$fromDate = \DateTimeImmutable::createFromFormat('Y-m-d', $fromDate->format('Y-m-d'));
assert($fromDate instanceof \DateTimeImmutable);
}

$from = \DateTimeImmutable::createFromInterface($fromDate)->modify('midnight');
if (! $from instanceof \DateTimeImmutable) {
throw new InvalidLink('Could not modify $fromDate while building all day link.');
}

$to = $from->modify("+$numberOfDays days");
if (! $to instanceof \DateTimeImmutable) {
throw new InvalidLink('Could not modify $fromDate while calculating $to date.');
}
assert($to instanceof \DateTimeImmutable);

return new static($title, $from, $to, true);
}
Expand All @@ -79,7 +76,7 @@ public function description(string $description): static
return $this;
}

/** Set address of the Event. */
/** Set the address of the Event. */
public function address(string $address): static
{
$this->address = $address;
Expand All @@ -99,8 +96,7 @@ public function google(): string

/**
* @psalm-param IcsOptions $options ICS specific properties and components
* @param array<non-empty-string, non-empty-string> $options ICS specific properties and components
* @param array{format?: \Spatie\CalendarLinks\Generators\Ics::FORMAT_*} $presentationOptions
* @psalm-param IcsPresentationOptions $presentationOptions
* @return string
*/
public function ics(array $options = [], array $presentationOptions = []): string
Expand Down
25 changes: 0 additions & 25 deletions tests/Generators/GeneratorTestContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ public function it_can_generate_a_short_event_link(): void
$this->assertMatchesSnapshot(
$this->generator()->generate($this->createShortEventLink())
);

$this->assertSame(
$this->generator()->generate($this->createShortEventLink(false)),
$this->generator()->generate($this->createShortEventLink(true))
);
}

/** @test */
Expand All @@ -30,11 +25,6 @@ public function it_can_generate_a_single_day_allday_event_link(): void
$this->assertMatchesSnapshot(
$this->generator()->generate($this->createSingleDayAllDayEventLink())
);

$this->assertSame(
$this->generator()->generate($this->createSingleDayAllDayEventLink(false)),
$this->generator()->generate($this->createSingleDayAllDayEventLink(true))
);
}

/** @test */
Expand All @@ -43,11 +33,6 @@ public function it_can_generate_a_multiple_days_event_link(): void
$this->assertMatchesSnapshot(
$this->generator()->generate($this->createMultipleDaysEventLink())
);

$this->assertSame(
$this->generator()->generate($this->createMultipleDaysEventLink(false)),
$this->generator()->generate($this->createMultipleDaysEventLink(true))
);
}

/** @test */
Expand All @@ -56,11 +41,6 @@ public function it_can_generate_a_multiple_days_event_link_with_allday_flag(): v
$this->assertMatchesSnapshot(
$this->generator()->generate($this->createMultipleDaysAllDayEventLink())
);

$this->assertSame(
$this->generator()->generate($this->createMultipleDaysAllDayEventLink(false)),
$this->generator()->generate($this->createMultipleDaysAllDayEventLink(true))
);
}

/** @test */
Expand All @@ -69,10 +49,5 @@ public function it_can_generate_a_description_is_html_code_event_link_with_allda
$this->assertMatchesSnapshot(
$this->generator()->generate($this->createDescriptionIsHtmlCodeEventLink())
);

$this->assertSame(
$this->generator()->generate($this->createDescriptionIsHtmlCodeEventLink(false)),
$this->generator()->generate($this->createDescriptionIsHtmlCodeEventLink(true))
);
}
}
9 changes: 6 additions & 3 deletions tests/Generators/IcsGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
use Spatie\CalendarLinks\Generators\Ics;
use Spatie\CalendarLinks\Tests\TestCase;

/** @psalm-import-type IcsOptions from \Spatie\CalendarLinks\Generators\Ics */
/**
* @psalm-import-type IcsOptions from \Spatie\CalendarLinks\Generators\Ics
* @psalm-import-type IcsPresentationOptions from \Spatie\CalendarLinks\Generators\Ics
*/
final class IcsGeneratorTest extends TestCase
{
use GeneratorTestContract;

/**
* @psalm-param IcsOptions $options ICS specific properties and components
* @param array<non-empty-string, non-empty-string> $options ICS specific properties and components
* @param array{format?: \Spatie\CalendarLinks\Generators\Ics::FORMAT_*} $presentationOptions
* @param IcsOptions $options ICS specific properties and components
* @param IcsPresentationOptions $presentationOptions
* @return \Spatie\CalendarLinks\Generator
*/
protected function generator(array $options = [], array $presentationOptions = []): Generator
Expand Down

0 comments on commit 44fb0c0

Please sign in to comment.