From 66778d51c194012ee4f4ccf6dc19443e72667ad4 Mon Sep 17 00:00:00 2001 From: rennokki Date: Mon, 30 Jul 2018 22:11:09 +0300 Subject: [PATCH] Consistency fixes with hints. (#5) * Consistency fixes. * Apply fixes from StyleCI (#6) --- src/TimeRange.php | 36 +++++++++++++++++----------------- src/Traits/HasSchedule.php | 40 +++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/TimeRange.php b/src/TimeRange.php index f47c7a0..768fdcb 100644 --- a/src/TimeRange.php +++ b/src/TimeRange.php @@ -30,9 +30,9 @@ 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); } /** @@ -40,9 +40,9 @@ public function isValidTimeRange() * * @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); } /** @@ -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; @@ -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()); @@ -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()); @@ -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]; @@ -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]; @@ -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]; @@ -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]; @@ -177,7 +177,7 @@ public function getEndMinute() * * @return array */ - public function toArray() + public function toArray(): array { if (! $this->isValidTimeRange()) { return []; diff --git a/src/Traits/HasSchedule.php b/src/Traits/HasSchedule.php index fceb511..564b888 100644 --- a/src/Traits/HasSchedule.php +++ b/src/Traits/HasSchedule.php @@ -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; } @@ -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) ?: [] : []; } @@ -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()); } /** @@ -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(); } @@ -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); @@ -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); } @@ -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; @@ -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); } @@ -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; @@ -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; @@ -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); @@ -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); @@ -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)) { @@ -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 = []; @@ -445,7 +445,7 @@ protected function normalizeScheduleArray(array $scheduleArray) /** * Normalize the exclusions array. */ - protected function normalizeExclusionsArray($exclusionsArray) + protected function normalizeExclusionsArray($exclusionsArray): array { $finalExclusionsArray = []; @@ -477,7 +477,7 @@ protected function normalizeExclusionsArray($exclusionsArray) return (array) $finalExclusionsArray; } - protected function isValidMonthDay($dateString) + protected function isValidMonthDay($dateString): bool { try { $day = $this->carbonInstance::createFromFormat('m-d', $dateString); @@ -485,10 +485,10 @@ protected function isValidMonthDay($dateString) 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); @@ -496,7 +496,7 @@ protected function isValidYearMonthDay($dateString) return false; } - return $day && $day->format('Y-m-d') === $dateString; + return (bool) ($day && $day->format('Y-m-d') === $dateString); } protected function getCarbonDateFromString($dateString)