diff --git a/rector.php b/rector.php index dd4380e3..2456d3ec 100644 --- a/rector.php +++ b/rector.php @@ -19,7 +19,6 @@ use Rector\Config\RectorConfig; use Rector\Php70\Rector\FunctionLike\ExceptionHandlerTypehintRector; -use Rector\Php71\Rector\FuncCall\CountOnNullRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\AddSeeTestAnnotationRector; use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector; use Rector\PHPUnit\Set\PHPUnitLevelSetList; @@ -41,7 +40,6 @@ $rectorConfig->importNames(); $rectorConfig->importShortClasses(false); $rectorConfig->skip([ - CountOnNullRector::class, ExceptionHandlerTypehintRector::class, AddSeeTestAnnotationRector::class, PreferPHPUnitThisCallRector::class, diff --git a/src/DependencyInjection/SonataIntlExtension.php b/src/DependencyInjection/SonataIntlExtension.php index 9e63200f..3eba5294 100644 --- a/src/DependencyInjection/SonataIntlExtension.php +++ b/src/DependencyInjection/SonataIntlExtension.php @@ -105,6 +105,8 @@ private function configureLocale(ContainerBuilder $container, array $config): vo * @param array $timezones * * @throws \RuntimeException If one of the locales is invalid + * + * @phpstan-param array $timezones */ private function validateTimezones(array $timezones): void { diff --git a/src/Helper/DateTimeFormatter.php b/src/Helper/DateTimeFormatter.php index 0c65d913..fecf93f1 100644 --- a/src/Helper/DateTimeFormatter.php +++ b/src/Helper/DateTimeFormatter.php @@ -135,9 +135,15 @@ public function getDatetime( } } + $timezone ??= $this->timezoneDetector->getTimezone(); + + if (null === $timezone || '' === $timezone) { + throw new \InvalidArgumentException('Invalid timezone provided'); + } + $date = new \DateTime(); $date->setTimestamp($data); - $date->setTimezone(new \DateTimeZone($timezone ?? $this->timezoneDetector->getTimezone() ?? '')); + $date->setTimezone(new \DateTimeZone($timezone)); return $date; } diff --git a/src/Helper/DateTimeFormatterInterface.php b/src/Helper/DateTimeFormatterInterface.php index 84c5fbda..682ceb67 100644 --- a/src/Helper/DateTimeFormatterInterface.php +++ b/src/Helper/DateTimeFormatterInterface.php @@ -24,6 +24,8 @@ interface DateTimeFormatterInterface { /** * @param int|null $dateType See \IntlDateFormatter::getDateType + * + * @phpstan-param non-empty-string|null $timezone */ public function formatDate( \DateTimeInterface|string|int $date, @@ -35,6 +37,8 @@ public function formatDate( /** * @param int|null $dateType See \IntlDateFormatter::getDateType * @param int|null $timeType See \IntlDateFormatter::getTimeType + * + * @phpstan-param non-empty-string|null $timezone */ public function formatDateTime( \DateTimeInterface|string|int $datetime, @@ -46,6 +50,8 @@ public function formatDateTime( /** * @param int|null $timeType See \IntlDateFormatter::getTimeType + * + * @phpstan-param non-empty-string|null $timezone */ public function formatTime( \DateTimeInterface|string|int $time, @@ -54,6 +60,9 @@ public function formatTime( ?int $timeType = null ): string; + /** + * @phpstan-param non-empty-string|null $timezone + */ public function format( \DateTimeInterface|string|int $datetime, string $pattern, @@ -63,6 +72,8 @@ public function format( /** * Gets a date time instance by a given data and timezone. + * + * @phpstan-param non-empty-string|null $timezone */ public function getDatetime( \DateTimeInterface|string|int $data, diff --git a/src/Twig/DateTimeRuntime.php b/src/Twig/DateTimeRuntime.php index 2cd3ee76..159e8e29 100644 --- a/src/Twig/DateTimeRuntime.php +++ b/src/Twig/DateTimeRuntime.php @@ -22,6 +22,9 @@ public function __construct(private DateTimeFormatterInterface $helper) { } + /** + * @phpstan-param non-empty-string|null $timezone + */ public function formatDate( \DateTimeInterface|string|int $date, ?string $pattern = null, @@ -36,6 +39,9 @@ public function formatDate( return $this->helper->formatDate($date, $locale, $timezone, $dateType); } + /** + * @phpstan-param non-empty-string|null $timezone + */ public function formatTime( \DateTimeInterface|string|int $time, ?string $pattern = null, @@ -50,6 +56,9 @@ public function formatTime( return $this->helper->formatTime($time, $locale, $timezone, $timeType); } + /** + * @phpstan-param non-empty-string|null $timezone + */ public function formatDatetime( \DateTimeInterface|string|int $time, ?string $pattern = null,