Skip to content

Commit

Permalink
Take advantage of new PHP 8 features
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemand authored Oct 11, 2021
1 parent 87d54cd commit 2f14032
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 80 deletions.
1 change: 0 additions & 1 deletion .php-cs-fixer.cache

This file was deleted.

7 changes: 3 additions & 4 deletions src/AbstractCarbonWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

namespace Calendar;

use Carbon\CarbonImmutable;

/**
* @mixin \Carbon\CarbonImmutable
*/
abstract class AbstractCarbonWrapper
{
/**
* @var \Carbon\CarbonImmutable
*/
protected $carbon;
protected CarbonImmutable $carbon;

/**
* Attempts to query Carbon for the property
Expand Down
40 changes: 7 additions & 33 deletions src/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Carbon\CarbonInterface;
use DateTimeZone;
use InvalidArgumentException;

class Calendar
{
Expand All @@ -14,32 +13,17 @@ class Calendar
*/
public const WEEKS_IN_MONTH = 6;

/**
* @var int
*/
private $month;
private int $month;

/**
* @var int
*/
private $year;
private int $year;

/**
* @var \DateTimeZone
*/
private $timezone;
private DateTimeZone $timezone;

/**
* @var int
*/
private $weekStart = CarbonInterface::MONDAY;
private int $weekStart = CarbonInterface::MONDAY;

/**
* @var bool
*/
private $variableWeeks = false;
private bool $variableWeeks = false;

public function __construct(int $year, int $month, $timezone = 'UTC')
public function __construct(int $year, int $month, DateTimeZone|string $timezone = 'UTC')
{
$this->setYear($year);
$this->setMonth($month);
Expand All @@ -66,17 +50,8 @@ public function getYear(): int
return $this->year;
}

/**
* @param \DateTimeZone|string $timezone
*
* @throws \InvalidArgumentException
*/
public function setTimezone($timezone): void
public function setTimezone(DateTimeZone|string $timezone): void
{
if (! ($timezone instanceof DateTimeZone || is_string($timezone))) {
throw new InvalidArgumentException('setTimezone requires a DateTimeZone instance or a timezone string');
}

$this->timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
}

Expand Down Expand Up @@ -135,7 +110,6 @@ public function getWeeks(): array
$firstDay = $this->getFirstDay();
$lastDay = $this->getLastDay();
$numberOfWeeks = $this->isVariableWeeks() ? $this->getWeekCount($firstDay) : self::WEEKS_IN_MONTH;
/** @var \Calendar\Day $day */
$day = clone $firstDay;

$weeks = [];
Expand Down
20 changes: 5 additions & 15 deletions src/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,13 @@

class Day extends AbstractCarbonWrapper
{
/**
* @var bool
*/
private $blankDay = false;
private bool $blankDay = false;

/**
* @param int $year
* @param int $month
* @param int $day
* @param \DateTimeZone|string $timezone
*/
public function __construct(int $year, int $month, int $day, $timezone = 'UTC')
public function __construct(int $year, int $month, int $day, DateTimeZone|string $timezone = 'UTC')
{
$hour = 5;

if (($timezone instanceof DateTimeZone && $timezone->getName() === 'UTC') || $timezone === 'UTC') {
if ($timezone === 'UTC' || ($timezone instanceof DateTimeZone && $timezone->getName() === 'UTC')) {
$hour = 0;
}

Expand All @@ -35,9 +26,8 @@ public function setBlankDay(bool $value): void
}

/**
* A "blank" day refers to days that appear
* on the current months calendar but are physically part of the
* previous or next months
* A "blank" day refers to days that appear on the current month's calendar
* but are physically part of the previous or next months
*/
public function isBlankDay(): bool
{
Expand Down
35 changes: 8 additions & 27 deletions src/Week.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,15 @@

class Week
{
/**
* @var \Calendar\Day
*/
private $dateStart;
private Day $dateStart;

/**
* @var int
*/
private $currentMonth;
private int $currentMonth;

/**
* @var int
*/
private $weekStart = CarbonInterface::MONDAY;
private int $weekStart = CarbonInterface::MONDAY;

/**
* @var array
*/
private $days = [];
private array $days = [];

public function __construct(Day $dateStart, int $currentMonth = null, int $weekStart = CarbonInterface::MONDAY)
public function __construct(Day $dateStart, ?int $currentMonth = null, int $weekStart = CarbonInterface::MONDAY)
{
$this->setWeekStart($weekStart);
$this->setStartDate($dateStart);
Expand All @@ -47,7 +35,7 @@ public function getStartDate(): Day
return $this->dateStart;
}

private function setCurrentMonth(int $currentMonth = null): void
private function setCurrentMonth(?int $currentMonth = null): void
{
$this->currentMonth = $currentMonth ?? $this->dateStart->month;
}
Expand Down Expand Up @@ -83,8 +71,6 @@ private function generateWeek(): void
/**
* Adds a day to the days property
* Goes through a check first
*
* @param \Calendar\Day $day
*/
private function addDay(Day $day): void
{
Expand All @@ -94,20 +80,15 @@ private function addDay(Day $day): void
}

/**
* @return \Calendar\Day[]
* @return array<\Calendar\Day>
*/
public function getDays(): array
{
return $this->days;
}

/**
* Determines whether a day is part of the currently
* selected month or not
*
* @param \Calendar\Day $day
*
* @return bool
* Determines whether a day is part of the currently selected month or not
*/
public function currentMonthDay(Day $day): bool
{
Expand Down

0 comments on commit 2f14032

Please sign in to comment.