Skip to content

Commit

Permalink
Fixed psalm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Aug 18, 2021
1 parent 780cd53 commit 0b81c29
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/Dates.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,18 @@ public static function isYesterday($time): bool
}

/**
* Convert seconds to human readable format "H:i:s"
* Convert seconds to human-readable format "H:i:s"
*
* @param float $seconds
* @param int $minValuableSeconds
* @return string
*/
public static function formatTime(float $seconds): string
public static function formatTime(float $seconds, int $minValuableSeconds = 2): string
{
$minValuableSeconds = 2;

if ($seconds < $minValuableSeconds) {
return number_format($seconds, 3) . ' sec';
}

return (string)gmdate('H:i:s', (int)round($seconds, 0));
return gmdate('H:i:s', (int)round($seconds, 0)) ?: '';
}
}
10 changes: 5 additions & 5 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public static function isMBString(): bool
public static function len(string $string): int
{
if (self::isMBString()) {
return (int)mb_strlen($string, self::$encoding);
return mb_strlen($string, self::$encoding) ?: 0;
}

return strlen($string);
Expand Down Expand Up @@ -508,7 +508,7 @@ public static function sub(string $string, int $start, int $length = 0): string
$length = self::len($string);
}

return (string)mb_substr($string, $start, $length, self::$encoding);
return mb_substr($string, $start, $length, self::$encoding) ?: '';
}

return (string)substr($string, $start, $length);
Expand All @@ -523,7 +523,7 @@ public static function sub(string $string, int $start, int $length = 0): string
public static function low($string): string
{
if (self::isMBString()) {
return (string)mb_strtolower((string)$string, self::$encoding);
return mb_strtolower((string)$string, self::$encoding) ?: '';
}

return strtolower((string)$string);
Expand All @@ -540,7 +540,7 @@ public static function low($string): string
public static function up($string): string
{
if (self::isMBString()) {
return (string)mb_strtoupper((string)$string, self::$encoding);
return mb_strtoupper((string)$string, self::$encoding) ?: '';
}

return strtoupper((string)$string);
Expand All @@ -556,7 +556,7 @@ public static function up($string): string
public static function subCount(string $haystack, string $needle): int
{
if (self::isMBString()) {
return (int)mb_substr_count($haystack, $needle, self::$encoding);
return mb_substr_count($haystack, $needle, self::$encoding) ?: 0;
}

return substr_count($haystack, $needle);
Expand Down
4 changes: 4 additions & 0 deletions tests/DatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,9 @@ public function testTimeFormat(): void
isSame('00:00:03', Dates::formatTime(2.56789));
isSame('00:00:02', Dates::formatTime(2));
isSame('00:00:50', Dates::formatTime(50));
isSame('00:00:00', Dates::formatTime(0, 0));
isSame('00:00:01', Dates::formatTime(1, 0));
isSame('00:00:02', Dates::formatTime(2, 0));
isSame('00:00:02', Dates::formatTime(1.9999, 0));
}
}

0 comments on commit 0b81c29

Please sign in to comment.