Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Psalm immutability annotations #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ interface Clock
{
/**
* Returns the current time.
*
* @psalm-mutation-free
*/
public function getTime() : Instant;
}
3 changes: 3 additions & 0 deletions src/Clock/OffsetClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public function __construct(Clock $referenceClock, Duration $offset)
$this->offset = $offset;
}

/**
* @psalm-mutation-free
*/
public function getTime() : Instant
{
return $this->referenceClock->getTime()->plus($this->offset);
Expand Down
3 changes: 3 additions & 0 deletions src/Clock/ScaleClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public function __construct(Clock $referenceClock, int $timeScale)
$this->timeScale = $timeScale;
}

/**
* @psalm-mutation-free
*/
public function getTime() : Instant
{
$duration = Duration::between($this->startTime, $this->referenceClock->getTime());
Expand Down
6 changes: 6 additions & 0 deletions src/Clock/SystemClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
*/
final class SystemClock implements Clock
{
/**
* @psalm-mutation-free
*/
public function getTime() : Instant
{
/**
* @psalm-suppress ImpureFunctionCall This is how time works
*/
[$fraction, $epochSecond] = \explode(' ', microtime());

$epochSecond = (int) $epochSecond;
Expand Down
5 changes: 5 additions & 0 deletions src/DateTimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ class DateTimeException extends \RuntimeException
* @param int $value The actual value.
* @param int $min The minimum allowed value.
* @param int $max The maximum allowed value.
*
* @psalm-pure
*/
public static function fieldNotInRange(string $field, int $value, int $min, int $max) : self
{
return new DateTimeException("Invalid $field: $value is not in the range $min to $max.");
}

/**
* @psalm-pure
*/
public static function unknownTimeZoneRegion(string $region) : self
{
return new self(\sprintf('Unknown time zone region "%s".', $region));
Expand Down
11 changes: 10 additions & 1 deletion src/DayOfWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* A day-of-week, such as Tuesday.
*
* This class is immutable.
*
* @psalm-immutable
*/
final class DayOfWeek implements \JsonSerializable
{
Expand Down Expand Up @@ -40,10 +42,15 @@ private function __construct(int $value)
* Returns a cached DayOfWeek instance.
*
* @param int $value The day-of-week value, validated from 1 to 7.
*
* @psalm-pure
*/
private static function get(int $value) : DayOfWeek
{
/** @var array<int, DayOfWeek> $values */
/**
* @var array<int, DayOfWeek> $values
* @psalm-suppress ImpureStaticVariable only used to cache results
*/
static $values = [];

if (! isset($values[$value])) {
Expand All @@ -61,6 +68,8 @@ private static function get(int $value) : DayOfWeek
* @return DayOfWeek The DayOfWeek instance.
*
* @throws DateTimeException If the day-of-week is not valid.
*
* @psalm-pure
*/
public static function of(int $dayOfWeek) : DayOfWeek
{
Expand Down
3 changes: 3 additions & 0 deletions src/DefaultClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ private function __construct()

/**
* Gets the default clock.
*
* @psalm-mutation-free
* @psalm-suppress ImpureStaticProperty
*/
public static function get() : Clock
{
Expand Down
6 changes: 6 additions & 0 deletions src/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* Represents a duration of time measured in seconds.
*
* This class is immutable.
*
* @psalm-immutable
*/
final class Duration implements \JsonSerializable
{
Expand Down Expand Up @@ -145,6 +147,8 @@ public static function parse(string $text) : Duration
*
* @param int $seconds The number of seconds of the duration.
* @param int $nanoAdjustment The adjustment to the duration in nanoseconds.
*
* @psalm-pure
*/
public static function ofSeconds(int $seconds, int $nanoAdjustment = 0) : Duration
{
Expand Down Expand Up @@ -211,6 +215,8 @@ public static function ofDays(int $days) : Duration
*
* @param Instant $startInclusive The start instant, inclusive.
* @param Instant $endExclusive The end instant, exclusive.
*
* @psalm-pure
*/
public static function between(Instant $startInclusive, Instant $endExclusive) : Duration
{
Expand Down
2 changes: 2 additions & 0 deletions src/Field/DayOfMonth.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ final class DayOfMonth
* @param int|null $year An optional year to check against, validated.
*
* @throws DateTimeException If the day-of-month is not valid.
*
* @psalm-pure
*/
public static function check(int $dayOfMonth, ?int $monthOfYear = null, ?int $year = null) : void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Field/DayOfWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ final class DayOfWeek
* @param int $dayOfWeek The day-of-week to check.
*
* @throws DateTimeException If the day-of-week is not valid.
*
* @psalm-pure
*/
public static function check(int $dayOfWeek) : void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Field/DayOfYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ final class DayOfYear
* @param int|null $year An optional year to check against, validated.
*
* @throws DateTimeException If the day-of-year is not valid.
*
* @psalm-pure
*/
public static function check(int $dayOfYear, ?int $year = null) : void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Field/HourOfDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class HourOfDay
* @param int $hourOfDay The hour-of-day to check.
*
* @throws DateTimeException If the hour-of-day is not valid.
*
* @psalm-pure
*/
public static function check(int $hourOfDay) : void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Field/MinuteOfHour.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class MinuteOfHour
* @param int $minuteOfHour The minute-of-hour to check.
*
* @throws DateTimeException If the minute-of-hour is not valid.
*
* @psalm-pure
*/
public static function check(int $minuteOfHour) : void
{
Expand Down
6 changes: 6 additions & 0 deletions src/Field/MonthOfYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class MonthOfYear
* @param int $monthOfYear The month-of-year to check.
*
* @throws DateTimeException If the month-of-year is not valid.
*
* @psalm-pure
*/
public static function check(int $monthOfYear) : void
{
Expand All @@ -40,6 +42,8 @@ public static function check(int $monthOfYear) : void
*
* @param int $monthOfYear The month-of-year, validated.
* @param int|null $year An optional year the month-of-year belongs to, validated.
*
* @psalm-pure
*/
public static function getLength(int $monthOfYear, ?int $year = null) : int
{
Expand All @@ -62,6 +66,8 @@ public static function getLength(int $monthOfYear, ?int $year = null) : int
* Returns the camel-cased English name of the given month-of-year.
*
* @param int $monthOfYear The month-of-year, validated.
*
* @psalm-pure
*/
public static function getName(int $monthOfYear) : string
{
Expand Down
2 changes: 2 additions & 0 deletions src/Field/NanoOfSecond.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ final class NanoOfSecond
* @param int $nanoOfSecond The nano-of-second to check.
*
* @throws DateTimeException If the nano-of-second is not valid.
*
* @psalm-pure
*/
public static function check(int $nanoOfSecond) : void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Field/SecondOfDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ final class SecondOfDay
* @param int $secondOfDay The second-of-day to check.
*
* @throws DateTimeException If the second-of-day is not valid.
*
* @psalm-pure
*/
public static function check(int $secondOfDay) : void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Field/SecondOfMinute.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class SecondOfMinute
* @param int $secondOfMinute The second-of-minute to check.
*
* @throws DateTimeException If the second-of-minute is not valid.
*
* @psalm-pure
*/
public static function check(int $secondOfMinute) : void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Field/TimeZoneOffsetHour.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class TimeZoneOffsetHour
* @param int $offsetHour The offset-hour to check.
*
* @throws DateTimeException If the offset-hour is not valid.
*
* @psalm-pure
*/
public static function check(int $offsetHour) : void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Field/TimeZoneOffsetMinute.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class TimeZoneOffsetMinute
* @param int $offsetMinute The offset-minute to check.
*
* @throws DateTimeException If the offset-minute is not valid.
*
* @psalm-pure
*/
public static function check(int $offsetMinute) : void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Field/TimeZoneOffsetSecond.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class TimeZoneOffsetSecond
* @param int $offsetSecond The offset-second to check.
*
* @throws DateTimeException If the offset-second is not valid.
*
* @psalm-pure
*/
public static function check(int $offsetSecond) : void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Field/TimeZoneOffsetTotalSeconds.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ final class TimeZoneOffsetTotalSeconds
* @param int $offsetSeconds The offset-seconds to check.
*
* @throws DateTimeException If the offset-seconds is not valid.
*
* @psalm-pure
*/
public static function check(int $offsetSeconds) : void
{
Expand Down
6 changes: 6 additions & 0 deletions src/Field/WeekOfYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ final class WeekOfYear
* @param int|null $year An optional year to check against, validated.
*
* @throws DateTimeException If the week-of-year is not valid.
*
* @psalm-pure
*/
public static function check(int $weekOfYear, ?int $year = null) : void
{
Expand All @@ -43,6 +45,8 @@ public static function check(int $weekOfYear, ?int $year = null) : void
* @param int $year The year, validated.
*
* @return bool True if 53 weeks, false if 52 weeks.
*
* @psalm-pure
*/
public static function is53WeekYear(int $year) : bool
{
Expand All @@ -59,6 +63,8 @@ public static function is53WeekYear(int $year) : bool
* @param int $year The year, validated.
*
* @return int The number of weeks in the year.
*
* @psalm-pure
*/
public static function getWeeksInYear(int $year) : int
{
Expand Down
4 changes: 4 additions & 0 deletions src/Field/Year.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ final class Year
* @param int $year The year to check.
*
* @throws DateTimeException If the year is not valid.
*
* @psalm-pure
*/
public static function check(int $year) : void
{
Expand All @@ -45,6 +47,8 @@ public static function check(int $year) : void

/**
* @param int $year The year, validated.
*
* @psalm-pure
*/
public static function isLeap(int $year) : bool
{
Expand Down
Loading