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
8 changes: 4 additions & 4 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.2
php-version: 8.4
coverage: none

- name: Get composer cache directory
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
coverage: none
php-version: 8.2
php-version: 8.4

- name: Get composer cache directory
id: composer-cache
Expand All @@ -77,7 +77,7 @@ jobs:

strategy:
matrix:
php-versions: ['8.0', '8.1', '8.2']
php-versions: ['8.3', '8.4']

steps:
- name: Checkout code
Expand All @@ -104,7 +104,7 @@ jobs:
run: composer install --no-interaction --no-progress --prefer-dist --optimize-autoloader

- name: Run unit tests
run: ./vendor/bin/phpunit --verbose --coverage-clover build/logs/clover.xml --coverage-text
run: ./vendor/bin/phpunit

- name: Publish coverage report to Codecov
uses: codecov/codecov-action@v1
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "pauci/datetime",
"description": "Enhanced DateTime, DateTimeImmutable and DateInterval objects",
"version": "0.7.0",
"type": "library",
"keywords": [
"datetime",
Expand All @@ -22,7 +23,7 @@
"issues": "https://github.com/pauci/datetime/issues"
},
"require": {
"php": "^8.0 || ^8.1 || ^8.2",
"php": "^8.3 || ^8.4",
"ext-json": "*",
"psr/clock": "^1.0"
},
Expand All @@ -31,7 +32,8 @@
"phpstan/extension-installer": "^1.2",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-phpunit": "^1.3",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^11.0.0",
"rector/rector": "^1.2",
"squizlabs/php_codesniffer": "^3.7"
},
"suggest": {
Expand All @@ -57,12 +59,16 @@
"check": [
"@lint",
"@cs-check",
"@rector",
"@stan",
"@test"
],
"lint": "parallel-lint src tests",
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"rector": "rector -n -vv",
"rector:clear": "rector -n --clear-cache -vv",
"rector:fix": "rector -vv",
"stan": "phpstan analyse --no-progress",
"test": "phpunit",
"test-coverage": "phpunit --coverage-clover clover.xml"
Expand Down
7 changes: 6 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="./tests/bootstrap.php" colors="true">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
bootstrap="./tests/bootstrap.php"
colors="true"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
Expand Down
14 changes: 14 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\CodingStyle;
use Rector\Config\RectorConfig;
use Rector\Privatization;

return RectorConfig::configure()
->withPhpSets(php83: true)
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);
25 changes: 21 additions & 4 deletions src/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function now(): DateTimeInterface
/**
* @throws Exception
*/
public static function fromString(string $time, DateTimeZone $timezone = null): static
public static function fromString(string $time, ?DateTimeZone $timezone = null): static
{
$dateTime = parent::createFromInterface(new \DateTime($time, $timezone));
\assert($dateTime instanceof static);
Expand All @@ -53,7 +53,7 @@ public static function fromString(string $time, DateTimeZone $timezone = null):
/**
* @throws Exception
*/
public static function fromTimestamp(int $timestamp, DateTimeZone $timezone = null): static
public static function fromTimestamp(int $timestamp, ?DateTimeZone $timezone = null): static
{
$dateTime = static::createFromFormat('U', (string) $timestamp);
\assert(false !== $dateTime);
Expand All @@ -63,7 +63,7 @@ public static function fromTimestamp(int $timestamp, DateTimeZone $timezone = nu
: $dateTime->inDefaultTimezone();
}

public static function fromFloatTimestamp(float $timestamp, DateTimeZone $timezone = null): static
public static function fromFloatTimestamp(float $timestamp, ?DateTimeZone $timezone = null): static
{
$integer = floor($timestamp);
$fract = fmod($timestamp, 1);
Expand All @@ -80,21 +80,24 @@ public static function fromFloatTimestamp(float $timestamp, DateTimeZone $timezo
}

#[\ReturnTypeWillChange]
#[\Override]
public static function createFromFormat(
string $format,
string $datetime,
DateTimeZone $timezone = null
?DateTimeZone $timezone = null
): static|false {
return parent::createFromFormat($format, $datetime, $timezone);
}

#[\ReturnTypeWillChange]
#[\Override]
public static function createFromMutable(\DateTime $object): static
{
return parent::createFromMutable($object);
}

#[\ReturnTypeWillChange]
#[\Override]
public static function createFromInterface(\DateTimeInterface $object): static
{
$dateTime = parent::createFromInterface($object);
Expand All @@ -106,63 +109,75 @@ public static function createFromInterface(\DateTimeInterface $object): static
/**
* @throws Exception
*/
#[\Override]
public function diff(\DateTimeInterface $targetObject, bool $absolute = false): DateInterval
{
return DateInterval::fromDateInterval(
parent::diff($targetObject, $absolute)
);
}

#[\Override]
public function add(\DateInterval $interval): static
{
return parent::add($interval);
}

#[\Override]
public function sub(\DateInterval $interval): static
{
return parent::sub($interval);
}

#[\Override]
public function modify(string $modifier): static|false
{
return parent::modify($modifier);
}

#[\Override]
public function setDate(int $year, int $month, int $day): static
{
return parent::setDate($year, $month, $day);
}

#[\Override]
public function setISODate(int $year, int $week, int $dayOfWeek = 1): static
{
return parent::setISODate($year, $week, $dayOfWeek);
}

#[\Override]
public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): static
{
return parent::setTime($hour, $minute, $second, $microsecond);
}

#[\Override]
public function setTimestamp(int $timestamp): static
{
return parent::setTimestamp($timestamp);
}

#[\Override]
public function setTimezone(\DateTimeZone $timezone): static
{
return parent::setTimezone($timezone);
}

#[\Override]
public function equals(DateTimeInterface $dateTime): bool
{
return $this == $dateTime;
}

#[\Override]
public function inDefaultTimezone(): static
{
return $this->setTimezone(new DateTimeZone(date_default_timezone_get()));
}

#[\Override]
public function toString(): string
{
$format = static::$format
Expand All @@ -175,11 +190,13 @@ public function toString(): string
return $this->format($format);
}

#[\Override]
public function __toString(): string
{
return $this->toString();
}

#[\Override]
public function jsonSerialize(): string
{
return $this->toString();
Expand Down
6 changes: 2 additions & 4 deletions src/FrozenClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@

class FrozenClock implements ClockInterface
{
private DateTime $now;

public function __construct(DateTime $now = null)
public function __construct(private DateTime $now = new DateTime())
{
$this->now = $now ?? new DateTime();
}

public function set(DateTime $now): void
Expand All @@ -26,6 +23,7 @@ public function modify(string $modifier): void
$this->now = $now;
}

#[\Override]
public function now(): DateTime
{
return $this->now;
Expand Down
4 changes: 2 additions & 2 deletions tests/src/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ public function testModify(): void

public function testModifyFailed(): void
{
$this->expectError();
$this->expectErrorMessage('DateTimeImmutable::modify(): Failed to parse time string (foo) at position 0 (f): The timezone could not be found in the database');
self::expectExceptionMessage('Failed to parse time string (foo) at position 0 (f): The timezone could not be found in the database');

$dateTime = DateTime::fromString('2016-05-12 22:37:46+02:00');

$dateTime->modify('foo');
}

Expand Down
3 changes: 1 addition & 2 deletions tests/src/FrozenClockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ public function testItFailsToModify(): void
{
$clock = new FrozenClock();

$this->expectError();
$this->expectErrorMessage('DateTimeImmutable::modify(): Failed to parse time string (foo) at position 0 (f): The timezone could not be found in the database');
self::expectExceptionMessage('DateTimeImmutable::modify(): Failed to parse time string (foo) at position 0 (f): The timezone could not be found in the database');

$clock->modify('foo');
}
Expand Down
Loading