From d81a5674502403b316fdae19e2aefccb1781c5eb Mon Sep 17 00:00:00 2001 From: Chris Tankersley Date: Fri, 14 Jan 2022 10:59:30 -0500 Subject: [PATCH] Fixed PHPStan errors --- composer.json | 8 ++++---- phpstan.neon | 13 +++++++++++++ src/Cron/AbstractField.php | 3 ++- src/Cron/CronExpression.php | 3 +++ src/Cron/DayOfMonthField.php | 11 +++++++---- src/Cron/DayOfWeekField.php | 1 - src/Cron/HoursField.php | 2 +- 7 files changed, 30 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 54f296e7..e512446d 100644 --- a/composer.json +++ b/composer.json @@ -13,12 +13,12 @@ ], "require": { "php": "^7.2|^8.0", - "webmozart/assert": "^1.7.0" + "webmozart/assert": "^1.0" }, "require-dev": { - "phpstan/phpstan": "^0.12", + "phpstan/phpstan": "^1.0", "phpunit/phpunit": "^7.0|^8.0|^9.0", - "phpstan/phpstan-webmozart-assert": "^0.12.7", + "phpstan/phpstan-webmozart-assert": "^1.0", "phpstan/extension-installer": "^1.0" }, "autoload": { @@ -35,7 +35,7 @@ "mtdowling/cron-expression": "^1.0" }, "scripts": { - "phpstan": "./vendor/bin/phpstan analyse -l max src tests", + "phpstan": "./vendor/bin/phpstan analyze", "test": "phpunit" } } diff --git a/phpstan.neon b/phpstan.neon index 9d52fd98..bea9cb0d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,2 +1,15 @@ parameters: checkMissingIterableValueType: false + + ignoreErrors: + - '#Call to an undefined method DateTimeInterface::add\(\)#' + - '#Call to an undefined method DateTimeInterface::modify\(\)#' + - '#Call to an undefined method DateTimeInterface::setDate\(\)#' + - '#Call to an undefined method DateTimeInterface::setTime\(\)#' + - '#Call to an undefined method DateTimeInterface::setTimezone\(\)#' + - '#Call to an undefined method DateTimeInterface::sub\(\)#' + + level: max + + paths: + - src/ diff --git a/src/Cron/AbstractField.php b/src/Cron/AbstractField.php index b0b28405..df2848df 100644 --- a/src/Cron/AbstractField.php +++ b/src/Cron/AbstractField.php @@ -146,8 +146,9 @@ public function isInIncrementsOfRanges(int $dateValue, string $value): bool // Generate the requested small range $rangeChunks = explode('-', $range, 2); - $rangeStart = $rangeChunks[0]; + $rangeStart = (int) $rangeChunks[0]; $rangeEnd = $rangeChunks[1] ?? $rangeStart; + $rangeEnd = (int) $rangeEnd; if ($rangeStart < $this->rangeStart || $rangeStart > $this->rangeEnd || $rangeStart > $rangeEnd) { throw new \OutOfRangeException('Invalid range start requested'); diff --git a/src/Cron/CronExpression.php b/src/Cron/CronExpression.php index b13f3d48..d5337cc5 100644 --- a/src/Cron/CronExpression.php +++ b/src/Cron/CronExpression.php @@ -456,6 +456,9 @@ protected function getRunDate($currentTime = null, int $nth = 0, bool $invert = $currentDate->setTimezone(new DateTimeZone($timeZone)); // Workaround for setTime causing an offset change: https://bugs.php.net/bug.php?id=81074 $currentDate = DateTime::createFromFormat("!Y-m-d H:iO", $currentDate->format("Y-m-d H:iP"), $currentDate->getTimezone()); + if ($currentDate === false) { + throw new \RuntimeException('Unable to create date from format'); + } $currentDate->setTimezone(new DateTimeZone($timeZone)); $nextRun = clone $currentDate; diff --git a/src/Cron/DayOfMonthField.php b/src/Cron/DayOfMonthField.php index a66a5ab4..e08f62ea 100644 --- a/src/Cron/DayOfMonthField.php +++ b/src/Cron/DayOfMonthField.php @@ -96,15 +96,18 @@ public function isSatisfiedBy(DateTimeInterface $date, $value, bool $invert): bo // Check to see if this is the nearest weekday to a particular value if (strpos($value, 'W')) { // Parse the target day - /** @phpstan-ignore-next-line */ $targetDay = (int) substr($value, 0, strpos($value, 'W')); // Find out if the current day is the nearest day of the week - /** @phpstan-ignore-next-line */ - return $date->format('j') === self::getNearestWeekday( + $nearest = self::getNearestWeekday( (int) $date->format('Y'), (int) $date->format('m'), $targetDay - )->format('j'); + ); + if ($nearest) { + return $date->format('j') === $nearest->format('j'); + } + + throw new \RuntimeException('Unable to return nearest weekday'); } return $this->isSatisfied((int) $date->format('d'), $value); diff --git a/src/Cron/DayOfWeekField.php b/src/Cron/DayOfWeekField.php index ef2a609d..5ac003da 100644 --- a/src/Cron/DayOfWeekField.php +++ b/src/Cron/DayOfWeekField.php @@ -69,7 +69,6 @@ public function isSatisfiedBy(DateTimeInterface $date, $value, bool $invert): bo // Find out if this is the last specific weekday of the month if (strpos($value, 'L')) { - /** @phpstan-ignore-next-line */ $weekday = $this->convertLiterals(substr($value, 0, strpos($value, 'L'))); $weekday %= 7; diff --git a/src/Cron/HoursField.php b/src/Cron/HoursField.php index ef930d45..0b809101 100644 --- a/src/Cron/HoursField.php +++ b/src/Cron/HoursField.php @@ -50,7 +50,7 @@ public function isSatisfiedBy(DateTimeInterface $date, $value, bool $invert): bo // Are we on the edge of a transition $lastTransition = $this->getPastTransition($date); - if (($lastTransition !== null) && ($lastTransition["ts"] > ($date->format('U') - 3600))) { + if (($lastTransition !== null) && ($lastTransition["ts"] > ((int) $date->format('U') - 3600))) { $dtLastOffset = clone $date; $this->timezoneSafeModify($dtLastOffset, "-1 hour"); $lastOffset = $dtLastOffset->getOffset();