fix financial year issue, fix test, improve DateTimeFactory (#4985)

This commit is contained in:
Kevin Papst
2024-07-23 07:20:11 +02:00
committed by GitHub
parent 8788311faf
commit 7e1025d61d
3 changed files with 29 additions and 68 deletions

View File

@@ -4798,41 +4798,6 @@ parameters:
count: 1 count: 1
path: src/Security/SessionHandler.php path: src/Security/SessionHandler.php
-
message: "#^Method App\\\\Timesheet\\\\DateTimeFactory\\:\\:createWeekDateTime\\(\\) has no return type specified\\.$#"
count: 1
path: src/Timesheet/DateTimeFactory.php
-
message: "#^Method App\\\\Timesheet\\\\DateTimeFactory\\:\\:createWeekDateTime\\(\\) has parameter \\$day with no type specified\\.$#"
count: 1
path: src/Timesheet/DateTimeFactory.php
-
message: "#^Method App\\\\Timesheet\\\\DateTimeFactory\\:\\:createWeekDateTime\\(\\) has parameter \\$hour with no type specified\\.$#"
count: 1
path: src/Timesheet/DateTimeFactory.php
-
message: "#^Method App\\\\Timesheet\\\\DateTimeFactory\\:\\:createWeekDateTime\\(\\) has parameter \\$minute with no type specified\\.$#"
count: 1
path: src/Timesheet/DateTimeFactory.php
-
message: "#^Method App\\\\Timesheet\\\\DateTimeFactory\\:\\:createWeekDateTime\\(\\) has parameter \\$second with no type specified\\.$#"
count: 1
path: src/Timesheet/DateTimeFactory.php
-
message: "#^Method App\\\\Timesheet\\\\DateTimeFactory\\:\\:createWeekDateTime\\(\\) has parameter \\$week with no type specified\\.$#"
count: 1
path: src/Timesheet/DateTimeFactory.php
-
message: "#^Method App\\\\Timesheet\\\\DateTimeFactory\\:\\:createWeekDateTime\\(\\) has parameter \\$year with no type specified\\.$#"
count: 1
path: src/Timesheet/DateTimeFactory.php
- -
message: "#^Parameter \\#2 \\$isFavorite of class App\\\\Model\\\\FavoriteTimesheet constructor expects bool, App\\\\Model\\\\FavoriteTimesheet\\|bool given\\.$#" message: "#^Parameter \\#2 \\$isFavorite of class App\\\\Model\\\\FavoriteTimesheet constructor expects bool, App\\\\Model\\\\FavoriteTimesheet\\|bool given\\.$#"
count: 1 count: 1

View File

@@ -42,18 +42,18 @@ final class DateTimeFactory
{ {
$date = $this->getDate($date); $date = $this->getDate($date);
$date->modify('first day of this month'); $date = $date->modify('first day of this month');
$date->setTime(0, 0, 0); $date = $date->setTime(0, 0, 0);
return $date; return $date;
} }
public function getStartOfLastMonth(): DateTimeInterface public function getStartOfLastMonth(): DateTimeInterface
{ {
$newDate = $this->createDateTime('first day of -1 month'); $date = $this->createDateTime('first day of -1 month');
$newDate->setTime(0, 0, 0); $date = $date->setTime(0, 0, 0);
return $newDate; return $date;
} }
private function getDate(DateTimeInterface|string|null $date = null): DateTime private function getDate(DateTimeInterface|string|null $date = null): DateTime
@@ -79,11 +79,11 @@ final class DateTimeFactory
// if today = sunday => increase week by one // if today = sunday => increase week by one
if ($date->format('N') !== '7') { if ($date->format('N') !== '7') {
$date->modify('-1 week'); $date = $date->modify('-1 week');
} }
} }
return $this->createWeekDateTime($date->format('o'), $date->format('W'), $firstDay, 0, 0, 0); return $this->createWeekDateTime((int) $date->format('o'), (int) $date->format('W'), $firstDay, 0, 0, 0);
} }
public function getEndOfWeek(DateTimeInterface|string|null $date = null): DateTime public function getEndOfWeek(DateTimeInterface|string|null $date = null): DateTime
@@ -96,36 +96,35 @@ final class DateTimeFactory
// only change when today is not sunday // only change when today is not sunday
if ($date->format('N') === '7') { if ($date->format('N') === '7') {
$date->modify('+1 week'); $date = $date->modify('+1 week');
} }
} }
return $this->createWeekDateTime($date->format('o'), $date->format('W'), $lastDay, 23, 59, 59); return $this->createWeekDateTime((int) $date->format('o'), (int) $date->format('W'), $lastDay, 23, 59, 59);
} }
public function getEndOfMonth(DateTimeInterface|string|null $date = null): DateTime public function getEndOfMonth(DateTimeInterface|string|null $date = null): DateTime
{ {
$date = $this->getDate($date); $date = $this->getDate($date);
$date = $date->modify('last day of this month'); $date = $date->modify('last day of this month');
$date->setTime(23, 59, 59); $date = $date->setTime(23, 59, 59);
return $date; return $date;
} }
public function getEndOfLastMonth(): DateTimeInterface public function getEndOfLastMonth(): DateTimeInterface
{ {
$newDate = $this->createDateTime('last day of -1 month'); $date = $this->createDateTime('last day of -1 month');
$newDate->setTime(23, 59, 59); $date = $date->setTime(23, 59, 59);
return $newDate; return $date;
} }
private function createWeekDateTime($year, $week, $day, $hour, $minute, $second) private function createWeekDateTime(int $year, int $week, int $day, int $hour, int $minute, int $second): DateTime
{ {
$date = new DateTime('now', $this->getTimezone()); $date = new DateTime('now', $this->getTimezone());
$date->setISODate($year, $week, $day); $date = $date->setISODate($year, $week, $day);
$date->setTime($hour, $minute, $second); $date = $date->setTime($hour, $minute, $second);
return $date; return $date;
} }
@@ -154,21 +153,17 @@ final class DateTimeFactory
{ {
$date = $this->getDate($date); $date = $this->getDate($date);
$date->modify('first day of january 00:00:00'); return $date->modify('first day of january 00:00:00');
return $date;
} }
public function createEndOfYear(DateTimeInterface|string|null $date = null): DateTime public function createEndOfYear(DateTimeInterface|string|null $date = null): DateTime
{ {
$date = $this->getDate($date); $date = $this->getDate($date);
$date->modify('last day of december 23:59:59'); return $date->modify('last day of december 23:59:59');
return $date;
} }
public function createStartOfFinancialYear(?string $financialYear = null): DateTime public function createStartOfFinancialYear(?string $financialYear = null): DateTimeInterface
{ {
$defaultDate = $this->createDateTime('01 january this year 00:00:00'); $defaultDate = $this->createDateTime('01 january this year 00:00:00');
@@ -177,21 +172,22 @@ final class DateTimeFactory
} }
$financialYear = $this->createDateTime($financialYear); $financialYear = $this->createDateTime($financialYear);
$financialYear->setDate((int) $defaultDate->format('Y'), (int) $financialYear->format('m'), (int) $financialYear->format('d')); $financialYear = $financialYear->setDate((int) $defaultDate->format('Y'), (int) $financialYear->format('m'), (int) $financialYear->format('d'));
$financialYear = $financialYear->setTime(0, 0, 0);
$now = $this->createDateTime('00:00:00'); $today = $this->createDateTime('00:00:00');
if ($financialYear >= $now) { if ($financialYear > $today) {
$financialYear->modify('-1 year'); $financialYear = $financialYear->modify('-1 year');
} }
return $financialYear; return $financialYear;
} }
public function createEndOfFinancialYear(DateTimeInterface $financialYear): DateTime public function createEndOfFinancialYear(DateTimeInterface $financialYear): DateTimeInterface
{ {
$yearEnd = DateTime::createFromInterface($financialYear); $yearEnd = DateTime::createFromInterface($financialYear);
$yearEnd->modify('+1 year')->modify('-1 day')->setTime(23, 59, 59); $yearEnd = $yearEnd->modify('+1 year')->modify('-1 day')->setTime(23, 59, 59);
return $yearEnd; return $yearEnd;
} }

View File

@@ -205,14 +205,14 @@ class DateTimeFactoryTest extends TestCase
$now = $sut->createDateTime(); $now = $sut->createDateTime();
$expected = $sut->createDateTime(); $expected = $sut->createDateTime();
$expected->setDate((int) $expected->format('Y'), 7, 22); $expected = $expected->setDate((int) $expected->format('Y'), 7, 22);
$expected->setTime(23, 59, 59); $expected = $expected->setTime(23, 59, 59);
if ($now > $expected) { if ($now > $expected) {
$expected->modify('+1 year'); $expected->modify('+1 year');
} }
$financial = $sut->createStartOfFinancialYear('2018-07-23 15:30:00'); $financial = $sut->createStartOfFinancialYear('2018-07-23 00:00:00');
$end = $sut->createEndOfFinancialYear($financial); $end = $sut->createEndOfFinancialYear($financial);
self::assertEquals($expected, $end); self::assertEquals($expected, $end);