Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"scripts": {
"test": "phpunit tests --colors",
"analyse": [
"phpstan analyse -l 7 src tests",
"phpstan analyse -l 9 src tests",
"psalm --shepherd"
]
}
Expand Down
154 changes: 38 additions & 116 deletions src/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -25,186 +21,118 @@ 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
public function format(string $format = self::FORMAT_DEFAULT): string
{
if (self::FORMAT_DEFAULT === $format) {
return $this->formatDefault();
Expand All @@ -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);

return $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 = '';
Expand All @@ -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)/';
Expand All @@ -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));
Expand Down Expand Up @@ -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));

Expand Down
Loading