Skip to content

Commit

Permalink
Simplify dropping of time part for date objects on PHP 8
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Aug 28, 2020
1 parent fb302be commit b9d7768
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
18 changes: 4 additions & 14 deletions phpstan-baseline.neon
Expand Up @@ -12,7 +12,7 @@ parameters:

-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 10
count: 11
path: src/Chronos.php

-
Expand All @@ -25,19 +25,14 @@ parameters:
count: 1
path: src/Chronos.php

-
message: "#^Result of && is always false\\.$#"
count: 1
path: src/Chronos.php

-
message: "#^Result of \\|\\| is always false\\.$#"
count: 1
path: src/Chronos.php

-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 10
count: 11
path: src/Date.php

-
Expand All @@ -52,7 +47,7 @@ parameters:

-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 10
count: 11
path: src/MutableDate.php

-
Expand All @@ -67,7 +62,7 @@ parameters:

-
message: "#^Unsafe usage of new static\\(\\)\\.$#"
count: 10
count: 11
path: src/MutableDateTime.php

-
Expand All @@ -80,11 +75,6 @@ parameters:
count: 1
path: src/MutableDateTime.php

-
message: "#^Result of && is always false\\.$#"
count: 1
path: src/MutableDateTime.php

-
message: "#^Result of \\|\\| is always false\\.$#"
count: 1
Expand Down
7 changes: 4 additions & 3 deletions src/Traits/FactoryTrait.php
Expand Up @@ -245,11 +245,12 @@ public static function createFromFormat($format, $time, $tz = null): ChronosInte
throw new InvalidArgumentException(implode(PHP_EOL, $errors['errors']));
}

$dt = new static($dt->format('Y-m-d H:i:s.u'), $dt->getTimezone());
if (PHP_MAJOR_VERSION >= 8 && ($dt instanceof Date || $dt instanceof MutableDate)) {
$dt = $dt->setTime(0, 0, 0);
$format = 'Y-m-d H:i:s.u';
if ($dt instanceof Date || $dt instanceof MutableDate) {
$format = 'Y-m-d';
}

$dt = new static($dt->format($format), $dt->getTimezone());
static::$_lastErrors = $errors;

return $dt;
Expand Down

0 comments on commit b9d7768

Please sign in to comment.