Fix "Argument 1 of Meritoo\\Common\\Utilities\\Date::getdayofweek expects int, string|false provided" bug pointed by Psalm

This commit is contained in:
Meritoo
2019-06-20 21:11:00 +02:00
parent 4ddc299e5b
commit c5a68b54af
2 changed files with 13 additions and 29 deletions

View File

@@ -235,20 +235,20 @@ class Date
*
* @return int
*/
public static function getCurrentDayOfWeek()
public static function getCurrentDayOfWeek(): int
{
$now = new DateTime();
$year = $now->format('Y');
$month = $now->format('m');
$day = $now->format('d');
$year = (int)$now->format('Y');
$month = (int)$now->format('m');
$day = (int)$now->format('d');
return self::getDayOfWeek($year, $month, $day);
}
/**
* Returns day of week (number 0 to 6, 0 - sunday, 6 - saturday).
* Based on the Zeller's algorithm (http://pl.wikipedia.org/wiki/Kalendarz_wieczny).
* Based on the Zeller's algorithm (https://en.wikipedia.org/wiki/Perpetual_calendar).
*
* @param int $year The year value
* @param int $month The month value
@@ -257,12 +257,8 @@ class Date
* @throws UnknownDatePartTypeException
* @return int
*/
public static function getDayOfWeek($year, $month, $day)
public static function getDayOfWeek(int $year, int $month, int $day): int
{
$year = (int)$year;
$month = (int)$month;
$day = (int)$day;
// Oops, given year is incorrect
if ($year <= 0) {
throw UnknownDatePartTypeException::createException(DatePartType::YEAR, $year);
@@ -299,13 +295,13 @@ class Date
*
* @return string
*/
public static function getCurrentDayOfWeekName()
public static function getCurrentDayOfWeekName(): string
{
$now = new DateTime();
$year = $now->format('Y');
$month = $now->format('m');
$day = $now->format('d');
$year = (int)$now->format('Y');
$month = (int)$now->format('m');
$day = (int)$now->format('d');
return self::getDayOfWeekName($year, $month, $day);
}

View File

@@ -228,7 +228,7 @@ class DateTest extends BaseTestCase
*
* @dataProvider provideIncorrectYearMonthDay
*/
public function testGetDayOfWeekIncorrectValues($year, $month, $day): void
public function testGetDayOfWeekIncorrectValues(int $year, int $month, int $day): void
{
$this->expectException(UnknownDatePartTypeException::class);
self::assertEmpty(Date::getDayOfWeek($year, $month, $day));
@@ -241,7 +241,7 @@ class DateTest extends BaseTestCase
*
* @dataProvider provideYearMonthDay
*/
public function testGetDayOfWeek($year, $month, $day): void
public function testGetDayOfWeek(int $year, int $month, int $day): void
{
self::assertRegExp('/^[0-6]{1}$/', (string)Date::getDayOfWeek($year, $month, $day));
}
@@ -766,20 +766,8 @@ class DateTest extends BaseTestCase
*
* @return Generator
*/
public function provideIncorrectYearMonthDay()
public function provideIncorrectYearMonthDay(): Generator
{
yield[
null,
null,
null,
];
yield[
'',
'',
'',
];
yield[
0,
0,