Skip to content
This repository has been archived by the owner on Jan 17, 2020. It is now read-only.

Commit

Permalink
Consistency fixes with hints. (#5)
Browse files Browse the repository at this point in the history
* Consistency fixes.

* Apply fixes from StyleCI (#6)
  • Loading branch information
rennokki authored Jul 30, 2018
1 parent a3a7767 commit 66778d5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
36 changes: 18 additions & 18 deletions src/TimeRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ public function __construct($timeRangeString, $carbonInstance = null)
*
* @return bool
*/
public function isValidTimeRange()
public function isValidTimeRange(): bool
{
return preg_match_all('/(([0-1])([0-9])|(2)([0-3])):([0-5])([0-9])-(([0-1])([0-9])|(2)([0-3])):([0-5])([0-9])/', $this->timeRangeString);
return (bool) preg_match_all('/(([0-1])([0-9])|(2)([0-3])):([0-5])([0-9])-(([0-1])([0-9])|(2)([0-3])):([0-5])([0-9])/', $this->timeRangeString);
}

/**
* Check if the string provided is a valid hour:minute formatted string.
*
* @return bool
*/
public function isValidHourMinute($hourMinute)
public function isValidHourMinute($hourMinute): bool
{
return preg_match_all('/(([0-1])([0-9])|(2)([0-3])):([0-5])([0-9])/', $hourMinute);
return (bool) preg_match_all('/(([0-1])([0-9])|(2)([0-3])):([0-5])([0-9])/', $hourMinute);
}

/**
Expand All @@ -51,7 +51,7 @@ public function isValidHourMinute($hourMinute)
* @param string $hourMinute The time in format hour:minute
* @return bool
*/
public function isInTimeRange($hourMinute)
public function isInTimeRange($hourMinute): bool
{
if (! $this->isValidHourMinute($hourMinute)) {
return false;
Expand Down Expand Up @@ -91,10 +91,10 @@ public function getEndCarbonInstance()
*
* @return int The difference, in hours.
*/
public function diffInHours()
public function diffInHours(): int
{
if (! $this->isValidTimeRange()) {
return 0;
return (int) 0;
}

return (int) $this->getStartCarbonInstance()->diffInHours($this->getEndCarbonInstance());
Expand All @@ -105,10 +105,10 @@ public function diffInHours()
*
* @return int The difference, in minutes.
*/
public function diffInMinutes()
public function diffInMinutes(): int
{
if (! $this->isValidTimeRange()) {
return 0;
return (int) 0;
}

return (int) $this->getStartCarbonInstance()->diffInMinutes($this->getEndCarbonInstance());
Expand All @@ -119,10 +119,10 @@ public function diffInMinutes()
*
* @return int
*/
public function getStartHour()
public function getStartHour(): ?int
{
if (! $this->isValidTimeRange()) {
return;
return null;
}

return (int) explode(':', $this->toArray()[0])[0];
Expand All @@ -133,10 +133,10 @@ public function getStartHour()
*
* @return int
*/
public function getStartMinute()
public function getStartMinute(): ?int
{
if (! $this->isValidTimeRange()) {
return;
return null;
}

return (int) explode(':', $this->toArray()[0])[1];
Expand All @@ -147,10 +147,10 @@ public function getStartMinute()
*
* @return int
*/
public function getEndHour()
public function getEndHour(): ?int
{
if (! $this->isValidTimeRange()) {
return;
return null;
}

return (int) explode(':', $this->toArray()[1])[0];
Expand All @@ -161,10 +161,10 @@ public function getEndHour()
*
* @return int
*/
public function getEndMinute()
public function getEndMinute(): ?int
{
if (! $this->isValidTimeRange()) {
return;
return null;
}

return (int) explode(':', $this->toArray()[1])[1];
Expand All @@ -177,7 +177,7 @@ public function getEndMinute()
*
* @return array
*/
public function toArray()
public function toArray(): array
{
if (! $this->isValidTimeRange()) {
return [];
Expand Down
40 changes: 20 additions & 20 deletions src/Traits/HasSchedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function schedule()
*
* @return array|null The array with schedules or null.
*/
public function getSchedule()
public function getSchedule(): ?array
{
return ($this->hasSchedule()) ? $this->schedule()->first()->schedule : null;
}
Expand All @@ -39,7 +39,7 @@ public function getSchedule()
*
* @return array The array with exclusions.
*/
public function getExclusions()
public function getExclusions(): array
{
return ($this->hasSchedule()) ? ($this->schedule()->first()->exclusions) ?: [] : [];
}
Expand All @@ -49,9 +49,9 @@ public function getExclusions()
*
* @return bool If the binded model has a schedule already set.
*/
public function hasSchedule()
public function hasSchedule(): bool
{
return $this->schedule()->first() != null;
return (bool) ! is_null($this->schedule()->first());
}

/**
Expand Down Expand Up @@ -125,7 +125,7 @@ public function updateExclusions(array $exclusionsArray)
*
* @return bool Wether the schedule was deleted or not.
*/
public function deleteSchedule()
public function deleteSchedule(): bool
{
return (bool) $this->schedule()->delete();
}
Expand Down Expand Up @@ -154,7 +154,7 @@ public function deleteExclusions()
* @param string|Carbon|DateTime $dateOrDay The datetime, date or the day.
* @return bool Wether it is available on that day.
*/
public function isAvailableOn($dateOrDay)
public function isAvailableOn($dateOrDay): bool
{
if (in_array($dateOrDay, Self::$availableDays)) {
return (bool) (count($this->getSchedule()[$dateOrDay]) > 0);
Expand Down Expand Up @@ -193,7 +193,7 @@ public function isAvailableOn($dateOrDay)
* @param string|Carbon|DateTime $dateOrDay The datetime, date or the day.
* @return bool Wether it is unavailable on that day.
*/
public function isUnavailableOn($dateOrDay)
public function isUnavailableOn($dateOrDay): bool
{
return (bool) ! $this->isAvailableOn($dateOrDay);
}
Expand All @@ -205,7 +205,7 @@ public function isUnavailableOn($dateOrDay)
* @param string The time.
* @return bool Wether it is available on that day, at a certain time.
*/
public function isAvailableOnAt($dateOrDay, $time)
public function isAvailableOnAt($dateOrDay, $time): bool
{
$timeRanges = null;

Expand Down Expand Up @@ -251,7 +251,7 @@ public function isAvailableOnAt($dateOrDay, $time)
* @param string The time.
* @return bool Wether it is unavailable on that day, at a certain time.
*/
public function isUnavailableOnAt($dateOrDay, $time)
public function isUnavailableOnAt($dateOrDay, $time): bool
{
return (bool) ! $this->isAvailableOnAt($dateOrDay, $time);
}
Expand All @@ -262,7 +262,7 @@ public function isUnavailableOnAt($dateOrDay, $time)
* @param string|Carbon|DateTime $dateOrDay The datetime, date or the day.
* @return int The amount of hours on that day.
*/
public function getHoursOn($dateOrDay)
public function getHoursOn($dateOrDay): int
{
$totalHours = 0;
$timeRanges = null;
Expand Down Expand Up @@ -306,7 +306,7 @@ public function getHoursOn($dateOrDay)
* @param string|Carbon|DateTime $dateOrDay The datetime, date or the day.
* @return int The amount of minutes on that day.
*/
public function getMinutesOn($dateOrDay)
public function getMinutesOn($dateOrDay): int
{
$totalMinutes = 0;
$timeRanges = null;
Expand Down Expand Up @@ -347,7 +347,7 @@ public function getMinutesOn($dateOrDay)
/**
* Get the time ranges for a particular excluded date.
*/
protected function getExcludedTimeRangesOn($date)
protected function getExcludedTimeRangesOn($date): array
{
foreach ($this->getExclusions() as $day => $timeRanges) {
$carbonDay = $this->getCarbonDateFromString($day);
Expand All @@ -364,7 +364,7 @@ protected function getExcludedTimeRangesOn($date)
/**
* Check if the model is excluded on a date.
*/
protected function isExcludedOn($date)
protected function isExcludedOn($date): bool
{
foreach ($this->getExclusions() as $day => $timeRanges) {
$carbonDay = $this->getCarbonDateFromString($day);
Expand All @@ -381,7 +381,7 @@ protected function isExcludedOn($date)
/**
* Check if the model is excluded on a date and time.
*/
protected function isExcludedOnAt($date, $time)
protected function isExcludedOnAt($date, $time): bool
{
foreach ($this->getExclusions() as $day => $timeRanges) {
if ($this->isValidMonthDay($day) && $this->isValidMonthDay($date)) {
Expand Down Expand Up @@ -411,7 +411,7 @@ protected function isExcludedOnAt($date, $time)
/**
* Normalize the schedule array.
*/
protected function normalizeScheduleArray(array $scheduleArray)
protected function normalizeScheduleArray(array $scheduleArray): array
{
$finalScheduleArray = [];

Expand Down Expand Up @@ -445,7 +445,7 @@ protected function normalizeScheduleArray(array $scheduleArray)
/**
* Normalize the exclusions array.
*/
protected function normalizeExclusionsArray($exclusionsArray)
protected function normalizeExclusionsArray($exclusionsArray): array
{
$finalExclusionsArray = [];

Expand Down Expand Up @@ -477,26 +477,26 @@ protected function normalizeExclusionsArray($exclusionsArray)
return (array) $finalExclusionsArray;
}

protected function isValidMonthDay($dateString)
protected function isValidMonthDay($dateString): bool
{
try {
$day = $this->carbonInstance::createFromFormat('m-d', $dateString);
} catch (\Exception $e) {
return false;
}

return $day && $day->format('m-d') === $dateString;
return (bool) ($day && $day->format('m-d') === $dateString);
}

protected function isValidYearMonthDay($dateString)
protected function isValidYearMonthDay($dateString): bool
{
try {
$day = $this->carbonInstance::createFromFormat('Y-m-d', $dateString);
} catch (\Exception $e) {
return false;
}

return $day && $day->format('Y-m-d') === $dateString;
return (bool) ($day && $day->format('Y-m-d') === $dateString);
}

protected function getCarbonDateFromString($dateString)
Expand Down

0 comments on commit 66778d5

Please sign in to comment.