Skip to content
Merged
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
7 changes: 4 additions & 3 deletions server/lib/PhpStan/IsoDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ public static function fromWire(mixed $data): IsoDate {
if (!is_string($data)) {
throw new \InvalidArgumentException("IsoDate must be string");
}
if (!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $data)) {
throw new \InvalidArgumentException("IsoDate must be Y-m-d");
$date_time = \DateTime::createFromFormat('Y-m-d', $data);
if ($date_time === false || $date_time->format('Y-m-d') !== $data) {
throw new \InvalidArgumentException("IsoDate must be valid Y-m-d format, got: {$data}");
}
return new IsoDate($data);
return new IsoDate($date_time->format('Y-m-d'));
}

public static function fromDateTime(?\DateTimeInterface $date_time): ?IsoDate {
Expand Down
7 changes: 4 additions & 3 deletions server/lib/PhpStan/IsoDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ public static function fromWire(mixed $data): IsoDateTime {
if (!is_string($data)) {
throw new \InvalidArgumentException("IsoDateTime must be string");
}
if (!preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/', $data)) {
throw new \InvalidArgumentException("IsoDateTime must be Y-m-d H:i:s");
$date_time = \DateTime::createFromFormat('Y-m-d H:i:s', $data);
if ($date_time === false || $date_time->format('Y-m-d H:i:s') !== $data) {
throw new \InvalidArgumentException("IsoDateTime must be valid Y-m-d H:i:s format, got: {$data}");
}
return new IsoDateTime($data);
return new IsoDateTime($date_time->format('Y-m-d H:i:s'));
}

public static function fromDateTime(?\DateTimeInterface $date_time): ?IsoDateTime {
Expand Down
7 changes: 4 additions & 3 deletions server/lib/PhpStan/IsoTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ public static function fromWire(mixed $data): IsoTime {
if (!is_string($data)) {
throw new \InvalidArgumentException("IsoTime must be string");
}
if (!preg_match('/^[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $data)) {
throw new \InvalidArgumentException("IsoTime must be H:i:s");
$date_time = \DateTime::createFromFormat('H:i:s', $data);
if ($date_time === false || $date_time->format('H:i:s') !== $data) {
throw new \InvalidArgumentException("IsoTime must be valid H:i:s format, got: {$data}");
}
return new IsoTime($data);
return new IsoTime($date_time->format('H:i:s'));
}

public static function fromDateTime(?\DateTimeInterface $date_time): ?IsoTime {
Expand Down
8 changes: 2 additions & 6 deletions server/tests/UnitTests/PhpStan/IsoDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testDeserializeMalformedIsoDate(): void {
IsoDate::fromWire('malformed');
$this->fail('Error expected');
} catch (\Throwable $th) {
$this->assertSame('IsoDate must be Y-m-d', $th->getMessage());
$this->assertSame('IsoDate must be valid Y-m-d format, got: malformed', $th->getMessage());
}
}

Expand All @@ -48,11 +48,7 @@ public function testDeserializeInvalidIsoDate(): void {
IsoDate::fromWire('2024-99-99');
$this->fail('Error expected');
} catch (\Throwable $th) {
if (\PHP_VERSION_ID < 80300) {
$this->assertSame(\Exception::class, get_class($th));
} else {
$this->assertSame(\DateMalformedStringException::class, get_class($th));
}
$this->assertSame(\InvalidArgumentException::class, get_class($th));
}
}

Expand Down
8 changes: 2 additions & 6 deletions server/tests/UnitTests/PhpStan/IsoDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testDeserializeMalformedIsoDateTime(): void {
IsoDateTime::fromWire('malformed');
$this->fail('Error expected');
} catch (\Throwable $th) {
$this->assertSame('IsoDateTime must be Y-m-d H:i:s', $th->getMessage());
$this->assertSame('IsoDateTime must be valid Y-m-d H:i:s format, got: malformed', $th->getMessage());
}
}

Expand All @@ -48,11 +48,7 @@ public function testDeserializeInvalidIsoDateTime(): void {
IsoDateTime::fromWire('2024-99-99 99:99:99');
$this->fail('Error expected');
} catch (\Throwable $th) {
if (\PHP_VERSION_ID < 80300) {
$this->assertSame(\Exception::class, get_class($th));
} else {
$this->assertSame(\DateMalformedStringException::class, get_class($th));
}
$this->assertSame(\InvalidArgumentException::class, get_class($th));
}
}

Expand Down
8 changes: 2 additions & 6 deletions server/tests/UnitTests/PhpStan/IsoTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testDeserializeMalformedIsoTime(): void {
IsoTime::fromWire('malformed');
$this->fail('Error expected');
} catch (\Throwable $th) {
$this->assertSame('IsoTime must be H:i:s', $th->getMessage());
$this->assertSame('IsoTime must be valid H:i:s format, got: malformed', $th->getMessage());
}
}

Expand All @@ -48,11 +48,7 @@ public function testDeserializeInvalidIsoTime(): void {
IsoTime::fromWire('99:99:99');
$this->fail('Error expected');
} catch (\Throwable $th) {
if (\PHP_VERSION_ID < 80300) {
$this->assertSame(\Exception::class, get_class($th));
} else {
$this->assertSame(\DateMalformedStringException::class, get_class($th));
}
$this->assertSame(\InvalidArgumentException::class, get_class($th));
}
}

Expand Down
Loading