From 3ea955955ccbcae01e22f8dd12bde544a8e29df0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sama=C3=ABl=20Villette?= Date: Sun, 30 Apr 2023 18:11:48 +0200 Subject: [PATCH 1/4] refactor: removed nullable from Duration --- src/Duration.php | 150 +++++++++----------------------------- src/DurationInterface.php | 136 +++++++++------------------------- 2 files changed, 70 insertions(+), 216 deletions(-) diff --git a/src/Duration.php b/src/Duration.php index 9133821..528d519 100644 --- a/src/Duration.php +++ b/src/Duration.php @@ -6,17 +6,13 @@ class Duration implements DurationInterface { - /** @var int|null */ - private ?int $days = 0; + private int $days = 0; - /** @var int|null */ - private ?int $hours = 0; + private int $hours = 0; - /** @var int|null */ - private ?int $minutes = 0; + private int $minutes = 0; - /** @var int|null */ - private ?int $seconds = 0; + private int $seconds = 0; public function __construct(?string $duration = null) { @@ -25,185 +21,117 @@ public function __construct(?string $duration = null) } } - /** - * @return int|null - */ - public function getDays(): ?int + public function getDays(): int { return $this->days; } - /** - * @param int|null $days - * @return $this - */ - public function setDays(?int $days): DurationInterface + public function setDays(int $days): DurationInterface { $this->days = $days; return $this; } - /** - * @param int|null $days - * @return $this - */ - public function addDays(?int $days): DurationInterface + public function addDays(int $days): DurationInterface { $this->days += $days; return $this; } - /** - * @param int|null $days - * @return $this - */ - public function subDays(?int $days): DurationInterface + public function subDays(int $days): DurationInterface { $this->days -= $days; return $this; } - /** - * @return int|null - */ - public function getHours(): ?int + public function getHours(): int { return $this->hours; } - /** - * @param int|null $hours - * @return $this - */ - public function setHours(?int $hours): DurationInterface + public function setHours(int $hours): DurationInterface { $this->hours = $hours; return $this; } - /** - * @param int|null $hours - * @return $this - */ - public function addHours(?int $hours): DurationInterface + public function addHours(int $hours): DurationInterface { $this->hours += $hours; return $this; } - /** - * @param int|null $hours - * @return $this - */ - public function subHours(?int $hours): DurationInterface + public function subHours(int $hours): DurationInterface { $this->hours -= $hours; return $this; } - /** - * @return int|null - */ - public function getMinutes(): ?int + public function getMinutes(): int { return $this->minutes; } - /** - * @param int|null $minutes - * @return $this - */ - public function setMinutes(?int $minutes): DurationInterface + public function setMinutes(int $minutes): DurationInterface { $this->minutes = $minutes; return $this; } - /** - * @param int|null $minutes - * @return $this - */ - public function addMinutes(?int $minutes): DurationInterface + public function addMinutes(int $minutes): DurationInterface { $this->minutes += $minutes; return $this; } - /** - * @param int|null $minutes - * @return $this - */ - public function subMinutes(?int $minutes): DurationInterface + public function subMinutes(int $minutes): DurationInterface { $this->minutes -= $minutes; return $this; } - /** - * @return int|null - */ - public function getSeconds(): ?int + public function getSeconds(): int { return $this->seconds; } - /** - * @param int|null $seconds - * @return $this - */ - public function setSeconds(?int $seconds): DurationInterface + public function setSeconds(int $seconds): DurationInterface { $this->seconds = $seconds; return $this; } - /** - * @param int|null $seconds - * @return $this - */ - public function addSeconds(?int $seconds): DurationInterface + public function addSeconds(int $seconds): DurationInterface { $this->seconds += $seconds; return $this; } - /** - * @param int|null $seconds - * @return $this - */ - public function subSeconds(?int $seconds): DurationInterface + public function subSeconds(int $seconds): DurationInterface { $this->seconds -= $seconds; return $this; } - /** - * @param string|null $duration - * @return static - */ - public function create(?string $duration): DurationInterface + public function create(string $duration): DurationInterface { $this->parse($duration); return $this; } - /** - * @param string $format - * @return string|null - */ public function format(string $format = self::FORMAT_DEFAULT): ?string { if (self::FORMAT_DEFAULT === $format) { @@ -213,16 +141,22 @@ public function format(string $format = self::FORMAT_DEFAULT): ?string $duration = $format; $duration = preg_replace('/%d/', (string) $this->getDays(), $duration); - $duration = preg_replace('/%h/', (string) $this->getHours(), $duration); - $duration = preg_replace('/%m/', (string) $this->getMinutes(), $duration); - $duration = preg_replace('/%s/', (string) $this->getSeconds(), $duration); + + if (null !== $duration) { + $duration = preg_replace('/%h/', (string) $this->getHours(), $duration); + } + + if (null !== $duration) { + $duration = preg_replace('/%m/', (string) $this->getMinutes(), $duration); + } + + if (null !== $duration) { + $duration = preg_replace('/%s/', (string) $this->getSeconds(), $duration); + } return $duration; } - /** - * @return string - */ private function formatDefault(): string { $formattedDuration = ''; @@ -242,10 +176,7 @@ private function formatDefault(): string return trim($formattedDuration); } - /** - * @param string|null $duration - */ - private function parse(?string $duration): void + private function parse(string $duration): void { $daysRegex = '/([0-9]+)\s*(?:d|D)/'; $hoursRegex = '/([0-9]+)\s*(?:h|H)/'; @@ -258,12 +189,7 @@ private function parse(?string $duration): void $this->parseRegex($daysRegex, $duration, 'days'); } - /** - * @param string|null $regex - * @param string|null $duration - * @param string|null $type - */ - private function parseRegex(?string $regex, ?string $duration, ?string $type): void + private function parseRegex(string $regex, string $duration, string $type): void { if (preg_match($regex, $duration, $matches)) { $methodName = sprintf('add%s', ucfirst($type)); @@ -302,11 +228,7 @@ private function calculate(): void } } - /** - * @param string|null $type - * @param int|null $value - */ - private function resetType(?string $type, ?int $value): void + private function resetType(string $type, ?int $value): void { $setMethodName = sprintf('set%s', ucfirst($type)); diff --git a/src/DurationInterface.php b/src/DurationInterface.php index 828d4e6..90e6648 100644 --- a/src/DurationInterface.php +++ b/src/DurationInterface.php @@ -9,107 +9,39 @@ interface DurationInterface public const FORMAT_DEFAULT = 'default'; public const FORMAT_SIMPLE = '%h:%m:%s'; - /** - * @return int|null - */ - public function getDays(): ?int; - - /** - * @param int|null $days - * @return $this - */ - public function setDays(?int $days): self; - - /** - * @param int|null $days - * @return $this - */ - public function addDays(?int $days): self; - - /** - * @param int|null $days - * @return $this - */ - public function subDays(?int $days): self; - - /** - * @return int|null - */ - public function getHours(): ?int; - - /** - * @param int|null $hours - * @return $this - */ - public function setHours(?int $hours): self; - - /** - * @param int|null $hours - * @return $this - */ - public function addHours(?int $hours): self; - - /** - * @param int|null $hours - * @return $this - */ - public function subHours(?int $hours): self; - - /** - * @return int|null - */ - public function getMinutes(): ?int; - - /** - * @param int|null $minutes - * @return $this - */ - public function setMinutes(?int $minutes): self; - - /** - * @param int|null $minutes - * @return $this - */ - public function addMinutes(?int $minutes): self; - - /** - * @param int|null $minutes - * @return $this - */ - public function subMinutes(?int $minutes): self; - - /** - * @return int|null - */ - public function getSeconds(): ?int; - - /** - * @param int|null $seconds - * @return $this - */ - public function setSeconds(?int $seconds): self; - - /** - * @param int|null $seconds - * @return $this - */ - public function addSeconds(?int $seconds): self; - - /** - * @param int|null $seconds - * @return $this - */ - public function subSeconds(?int $seconds): self; - - /** - * @param string|null $duration - * @return static - */ - public function create(?string $duration): self; - - /** - * @param string $format - * @return string|null - */ + public function getDays(): int; + + public function setDays(int $days): self; + + public function addDays(int $days): self; + + public function subDays(int $days): self; + + public function getHours(): int; + + public function setHours(int $hours): self; + + public function addHours(int $hours): self; + + public function subHours(int $hours): self; + + public function getMinutes(): int; + + public function setMinutes(int $minutes): self; + + public function addMinutes(int $minutes): self; + + public function subMinutes(int $minutes): self; + + public function getSeconds(): int; + + public function setSeconds(int $seconds): self; + + public function addSeconds(int $seconds): self; + + public function subSeconds(int $seconds): self; + + public function create(string $duration): self; + public function format(string $format = self::FORMAT_DEFAULT): ?string; } From f431cc9a5cce954133bed98b005ba7fa91cbb2f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sama=C3=ABl=20Villette?= Date: Sun, 30 Apr 2023 18:12:10 +0200 Subject: [PATCH 2/4] chore: set PHPStan level to max --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 99e0015..55aa6a5 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "scripts": { "test": "phpunit tests --colors", "analyse": [ - "phpstan analyse -l 7 src tests", + "phpstan analyse -l 9 src tests", "psalm --shepherd" ] } From 7f424b5048a0d4ed8914b82ef941d6456de80e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sama=C3=ABl=20Villette?= Date: Sun, 30 Apr 2023 18:13:17 +0200 Subject: [PATCH 3/4] fix: remove nullable in `resetType` method --- src/Duration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Duration.php b/src/Duration.php index 528d519..bf71338 100644 --- a/src/Duration.php +++ b/src/Duration.php @@ -228,7 +228,7 @@ private function calculate(): void } } - private function resetType(string $type, ?int $value): void + private function resetType(string $type, int $value): void { $setMethodName = sprintf('set%s', ucfirst($type)); From fd40b5e67ed2e65ec0ff6fec51f14d6a8851c9a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sama=C3=ABl=20Villette?= Date: Sun, 30 Apr 2023 18:14:44 +0200 Subject: [PATCH 4/4] fix: remove nullable in `format` method --- src/Duration.php | 4 ++-- src/DurationInterface.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Duration.php b/src/Duration.php index bf71338..7ef0f0d 100644 --- a/src/Duration.php +++ b/src/Duration.php @@ -132,7 +132,7 @@ public function create(string $duration): DurationInterface return $this; } - public function format(string $format = self::FORMAT_DEFAULT): ?string + public function format(string $format = self::FORMAT_DEFAULT): string { if (self::FORMAT_DEFAULT === $format) { return $this->formatDefault(); @@ -154,7 +154,7 @@ public function format(string $format = self::FORMAT_DEFAULT): ?string $duration = preg_replace('/%s/', (string) $this->getSeconds(), $duration); } - return $duration; + return $duration ?? ''; } private function formatDefault(): string diff --git a/src/DurationInterface.php b/src/DurationInterface.php index 90e6648..96e0f98 100644 --- a/src/DurationInterface.php +++ b/src/DurationInterface.php @@ -43,5 +43,5 @@ public function subSeconds(int $seconds): self; public function create(string $duration): self; - public function format(string $format = self::FORMAT_DEFAULT): ?string; + public function format(string $format = self::FORMAT_DEFAULT): string; }