From 0b6900eb61518368e3b551399dd1c53dbf586a9d Mon Sep 17 00:00:00 2001 From: Dorian Villet Date: Fri, 22 Mar 2024 10:56:51 +0100 Subject: [PATCH 1/2] Add @psalm-return non-empty-string on toString methods (and alike). --- src/DayOfWeek.php | 4 ++++ src/Duration.php | 6 ++++++ src/Instant.php | 6 ++++++ src/Interval.php | 6 ++++++ src/LocalDate.php | 6 ++++++ src/LocalDateRange.php | 6 ++++++ src/LocalDateTime.php | 6 ++++++ src/LocalTime.php | 6 ++++++ src/Month.php | 4 ++++ src/MonthDay.php | 6 ++++++ src/Period.php | 6 ++++++ src/TimeZone.php | 3 +++ src/Year.php | 6 ++++++ src/YearMonth.php | 6 ++++++ src/YearMonthRange.php | 6 ++++++ src/YearWeek.php | 6 ++++++ src/ZonedDateTime.php | 6 ++++++ 17 files changed, 95 insertions(+) diff --git a/src/DayOfWeek.php b/src/DayOfWeek.php index 59af2dd8..532df96b 100644 --- a/src/DayOfWeek.php +++ b/src/DayOfWeek.php @@ -226,6 +226,8 @@ public function minus(int $days): DayOfWeek /** * Serializes as a string using {@see DayOfWeek::toString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -234,6 +236,8 @@ public function jsonSerialize(): string /** * Returns the capitalized English name of this day-of-week. + * + * @psalm-return non-empty-string */ public function toString(): string { diff --git a/src/Duration.php b/src/Duration.php index 77d35834..fe6b1271 100644 --- a/src/Duration.php +++ b/src/Duration.php @@ -756,6 +756,8 @@ public function toNanosPart(): int /** * Serializes as a string using {@see Duration::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -772,6 +774,8 @@ public function jsonSerialize(): string * The hours, minutes and seconds will all have the same sign. * * Note that multiples of 24 hours are not output as days to avoid confusion with Period. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -817,6 +821,8 @@ public function toISOString(): string /** * {@see Duration::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { diff --git a/src/Instant.php b/src/Instant.php index 14c17b3d..cf2d9a15 100644 --- a/src/Instant.php +++ b/src/Instant.php @@ -368,6 +368,8 @@ public function toDecimal(): string /** * Serializes as a string using {@see Instant::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -376,6 +378,8 @@ public function jsonSerialize(): string /** * Returns the ISO 8601 representation of this instant. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -384,6 +388,8 @@ public function toISOString(): string /** * {@see Instant::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { diff --git a/src/Interval.php b/src/Interval.php index d8476964..aab657c5 100644 --- a/src/Interval.php +++ b/src/Interval.php @@ -140,6 +140,8 @@ public function isEqualTo(Interval $that): bool /** * Serializes as a string using {@see Interval::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -148,6 +150,8 @@ public function jsonSerialize(): string /** * Returns the ISO 8601 representation of this interval. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -156,6 +160,8 @@ public function toISOString(): string /** * {@see Interval::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { diff --git a/src/LocalDate.php b/src/LocalDate.php index 59e4859d..806ca4da 100644 --- a/src/LocalDate.php +++ b/src/LocalDate.php @@ -771,6 +771,8 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable /** * Serializes as a string using {@see LocalDate::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -779,6 +781,8 @@ public function jsonSerialize(): string /** * Returns the ISO 8601 representation of this date. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -799,6 +803,8 @@ public function toISOString(): string /** * {@see LocalDate::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { diff --git a/src/LocalDateRange.php b/src/LocalDateRange.php index 5c919b82..73409cb7 100644 --- a/src/LocalDateRange.php +++ b/src/LocalDateRange.php @@ -239,6 +239,8 @@ public function count(): int /** * Serializes as a string using {@see LocalDateRange::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -262,6 +264,8 @@ public function toNativeDatePeriod(): DatePeriod /** * Returns the ISO 8601 representation of this date range. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -270,6 +274,8 @@ public function toISOString(): string /** * {@see LocalDateRange::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { diff --git a/src/LocalDateTime.php b/src/LocalDateTime.php index 6da9d614..086f642c 100644 --- a/src/LocalDateTime.php +++ b/src/LocalDateTime.php @@ -738,6 +738,8 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable /** * Serializes as a string using {@see LocalDateTime::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -746,6 +748,8 @@ public function jsonSerialize(): string /** * Returns the ISO 8601 representation of this date time. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -754,6 +758,8 @@ public function toISOString(): string /** * {@see LocalDateTime::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { diff --git a/src/LocalTime.php b/src/LocalTime.php index 9ca1e99b..10921e7f 100644 --- a/src/LocalTime.php +++ b/src/LocalTime.php @@ -643,6 +643,8 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable /** * Serializes as a string using {@see LocalTime::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -661,6 +663,8 @@ public function jsonSerialize(): string * The format used will be the shortest that outputs the full value of * the time where the omitted parts are implied to be zero. * The nanoseconds value, if present, can be 0 to 9 digits. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -674,6 +678,8 @@ public function toISOString(): string /** * {@see LocalTime::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { diff --git a/src/Month.php b/src/Month.php index f896d5fc..b6dd5cc5 100644 --- a/src/Month.php +++ b/src/Month.php @@ -201,6 +201,8 @@ public function minus(int $months): Month /** * Serializes as a string using {@see Month::toString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -209,6 +211,8 @@ public function jsonSerialize(): string /** * Returns the capitalized English name of this Month. + * + * @psalm-return non-empty-string */ public function toString(): string { diff --git a/src/MonthDay.php b/src/MonthDay.php index 15912f92..9a77768f 100644 --- a/src/MonthDay.php +++ b/src/MonthDay.php @@ -243,6 +243,8 @@ public function atYear(int $year): LocalDate /** * Serializes as a string using {@see MonthDay::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -251,6 +253,8 @@ public function jsonSerialize(): string /** * Returns the ISO 8601 representation of this month-day. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -263,6 +267,8 @@ public function toISOString(): string /** * {@see MonthDay::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { diff --git a/src/Period.php b/src/Period.php index 0f1844c9..94679be7 100644 --- a/src/Period.php +++ b/src/Period.php @@ -379,6 +379,8 @@ public function toNativeDateInterval(): DateInterval /** * Serializes as a string using {@see Period::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -387,6 +389,8 @@ public function jsonSerialize(): string /** * Returns the ISO 8601 representation of this period. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -411,6 +415,8 @@ public function toISOString(): string /** * {@see Period::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { diff --git a/src/TimeZone.php b/src/TimeZone.php index 467f8d0b..915e8d26 100644 --- a/src/TimeZone.php +++ b/src/TimeZone.php @@ -93,6 +93,9 @@ public static function fromNativeDateTimeZone(DateTimeZone $dateTimeZone): TimeZ */ abstract public function toNativeDateTimeZone(): DateTimeZone; + /** + * @psalm-return non-empty-string + */ public function __toString(): string { return $this->getId(); diff --git a/src/Year.php b/src/Year.php index 105278de..7098d280 100644 --- a/src/Year.php +++ b/src/Year.php @@ -284,6 +284,8 @@ public function toLocalDateRange(): LocalDateRange /** * Serializes as a string using {@see Year::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -292,6 +294,8 @@ public function jsonSerialize(): string /** * Returns the ISO 8601 representation of this year. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -307,6 +311,8 @@ public function toISOString(): string /** * {@see Year::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { diff --git a/src/YearMonth.php b/src/YearMonth.php index b30ff4a4..4d784748 100644 --- a/src/YearMonth.php +++ b/src/YearMonth.php @@ -304,6 +304,8 @@ public function toLocalDateRange(): LocalDateRange /** * Serializes as a string using {@see YearMonth::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -312,6 +314,8 @@ public function jsonSerialize(): string /** * Returns the ISO 8601 representation of this year-month. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -330,6 +334,8 @@ public function toISOString(): string /** * {@see YearMonth::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { diff --git a/src/YearMonthRange.php b/src/YearMonthRange.php index 7606c982..55ac86ce 100644 --- a/src/YearMonthRange.php +++ b/src/YearMonthRange.php @@ -182,6 +182,8 @@ public function toLocalDateRange(): LocalDateRange /** * Serializes as a string using {@see YearMonthRange::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -193,6 +195,8 @@ public function jsonSerialize(): string * * ISO 8601 does not seem to provide a standard notation for year-month ranges, but we're using the same format as * date ranges. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -201,6 +205,8 @@ public function toISOString(): string /** * {@see YearMonthRange::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { diff --git a/src/YearWeek.php b/src/YearWeek.php index f430775b..db60440d 100644 --- a/src/YearWeek.php +++ b/src/YearWeek.php @@ -311,6 +311,8 @@ public function toLocalDateRange(): LocalDateRange /** * Serializes as a string using {@see YearWeek::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -319,6 +321,8 @@ public function jsonSerialize(): string /** * Returns the ISO 8601 representation of this year-week. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -337,6 +341,8 @@ public function toISOString(): string /** * {@see YearWeek::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { diff --git a/src/ZonedDateTime.php b/src/ZonedDateTime.php index 86b9d646..ce0780ae 100644 --- a/src/ZonedDateTime.php +++ b/src/ZonedDateTime.php @@ -734,6 +734,8 @@ public function toNativeDateTimeImmutable(): DateTimeImmutable /** * Serializes as a string using {@see ZonedDateTime::toISOString()}. + * + * @psalm-return non-empty-string */ public function jsonSerialize(): string { @@ -742,6 +744,8 @@ public function jsonSerialize(): string /** * Returns the ISO 8601 representation of this zoned date time. + * + * @psalm-return non-empty-string */ public function toISOString(): string { @@ -756,6 +760,8 @@ public function toISOString(): string /** * {@see ZonedDateTime::toISOString()}. + * + * @psalm-return non-empty-string */ public function __toString(): string { From fe52b5615e23012eb5c0a76c92514550af8c499f Mon Sep 17 00:00:00 2001 From: Dorian Villet Date: Fri, 22 Mar 2024 11:00:19 +0100 Subject: [PATCH 2/2] Add @psalm-return -1|0|1 on compareTo methods. --- src/Duration.php | 2 ++ src/Instant.php | 2 ++ src/LocalDate.php | 2 ++ src/LocalDateTime.php | 2 ++ src/LocalTime.php | 2 ++ src/MonthDay.php | 2 ++ src/Year.php | 2 ++ src/YearMonth.php | 2 ++ src/YearWeek.php | 2 ++ src/ZonedDateTime.php | 2 ++ 10 files changed, 20 insertions(+) diff --git a/src/Duration.php b/src/Duration.php index fe6b1271..c4843ec3 100644 --- a/src/Duration.php +++ b/src/Duration.php @@ -466,6 +466,8 @@ public function abs(): Duration * @param Duration $that The other duration to compare to. * * @return int [-1,0,1] If this duration is less than, equal to, or greater than the given duration. + * + * @psalm-return -1|0|1 */ public function compareTo(Duration $that): int { diff --git a/src/Instant.php b/src/Instant.php index cf2d9a15..ff97f477 100644 --- a/src/Instant.php +++ b/src/Instant.php @@ -238,6 +238,8 @@ public function getNano(): int * Compares this instant with another. * * @return int [-1,0,1] If this instant is before, on, or after the given instant. + * + * @psalm-return -1|0|1 */ public function compareTo(Instant $that): int { diff --git a/src/LocalDate.php b/src/LocalDate.php index 806ca4da..9bd3296e 100644 --- a/src/LocalDate.php +++ b/src/LocalDate.php @@ -591,6 +591,8 @@ public function minusDays(int $days): LocalDate * Returns -1 if this date is before the given date, 1 if after, 0 if the dates are equal. * * @return int [-1,0,1] If this date is before, on, or after the given date. + * + * @psalm-return -1|0|1 */ public function compareTo(LocalDate $that): int { diff --git a/src/LocalDateTime.php b/src/LocalDateTime.php index 086f642c..11c93958 100644 --- a/src/LocalDateTime.php +++ b/src/LocalDateTime.php @@ -659,6 +659,8 @@ public function minusNanos(int $nanos): LocalDateTime * @param LocalDateTime $that The date-time to compare to. * * @return int [-1,0,1] If this date-time is before, on, or after the given date-time. + * + * @psalm-return -1|0|1 */ public function compareTo(LocalDateTime $that): int { diff --git a/src/LocalTime.php b/src/LocalTime.php index 10921e7f..8144a80e 100644 --- a/src/LocalTime.php +++ b/src/LocalTime.php @@ -527,6 +527,8 @@ public function minusNanos(int $nanos): LocalTime * @param LocalTime $that The time to compare to. * * @return int [-1,0,1] If this time is before, on, or after the given time. + * + * @psalm-return -1|0|1 */ public function compareTo(LocalTime $that): int { diff --git a/src/MonthDay.php b/src/MonthDay.php index 9a77768f..797f83e2 100644 --- a/src/MonthDay.php +++ b/src/MonthDay.php @@ -133,6 +133,8 @@ public function getDayOfMonth(): int * Returns -1 if this date is before the given date, 1 if after, 0 if the dates are equal. * * @return int [-1,0,1] If this date is before, on, or after the given date. + * + * @psalm-return -1|0|1 */ public function compareTo(MonthDay $that): int { diff --git a/src/Year.php b/src/Year.php index 7098d280..5dc600c2 100644 --- a/src/Year.php +++ b/src/Year.php @@ -178,6 +178,8 @@ public function minus(int $years): Year * @param Year $that The year to compare to. * * @return int [-1, 0, 1] If this year is before, equal to, or after the given year. + * + * @psalm-return -1|0|1 */ public function compareTo(Year $that): int { diff --git a/src/YearMonth.php b/src/YearMonth.php index 4d784748..43cd211b 100644 --- a/src/YearMonth.php +++ b/src/YearMonth.php @@ -147,6 +147,8 @@ public function getLengthOfYear(): int /** * @return int [-1,0,1] If this year-month is before, on, or after the given year-month. + * + * @psalm-return -1|0|1 */ public function compareTo(YearMonth $that): int { diff --git a/src/YearWeek.php b/src/YearWeek.php index db60440d..590a54b1 100644 --- a/src/YearWeek.php +++ b/src/YearWeek.php @@ -106,6 +106,8 @@ public function getWeek(): int /** * @return int [-1,0,1] If this year-week is before, on, or after the given year-week. + * + * @psalm-return -1|0|1 */ public function compareTo(YearWeek $that): int { diff --git a/src/ZonedDateTime.php b/src/ZonedDateTime.php index ce0780ae..fb625d1c 100644 --- a/src/ZonedDateTime.php +++ b/src/ZonedDateTime.php @@ -611,6 +611,8 @@ public function minusSeconds(int $seconds): ZonedDateTime * The comparison is performed on the instant. * * @return int [-1,0,1] If this zoned date-time is before, on, or after the given one. + * + * @psalm-return -1|0|1 */ public function compareTo(ZonedDateTime $that): int {