Passed
Push — master ( f97da3...20d4ac )
by Caen
03:25 queued 12s
created

ReadingTime::getMinutesAsFloat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Support;
6
7
use Closure;
8
use Hyde\Facades\Filesystem;
9
use function floor;
10
use function sprintf;
11
use function str_word_count;
12
13
/**
14
 * Calculate the estimated reading time for a text.
15
 *
16
 * @see \Hyde\Framework\Testing\Feature\ReadingTimeTest
17
 */
18
class ReadingTime
19
{
20
    /** @var int How many words per minute is read. Inversely proportional. Increase for a shorter reading time. */
21
    protected const WORDS_PER_MINUTE = 240;
22
23
    /** @var string The text to calculate the reading time for. */
24
    protected readonly string $text;
25
26
    /** @var int The number of words in the text. */
27
    protected int $wordCount;
28
29
    /** @var int The number of seconds it takes to read the text. */
30
    protected int $seconds;
31
32
    public static function fromString(string $text): static
33
    {
34
        return new static($text);
35
    }
36
37
    public static function fromFile(string $path): static
38
    {
39
        return static::fromString(Filesystem::getContents($path));
40
    }
41
42
    public function __construct(string $text)
43
    {
44
        $this->text = $text;
0 ignored issues
show
Bug introduced by
The property text is declared read-only in Hyde\Support\ReadingTime.
Loading history...
45
46
        $this->generate();
47
    }
48
49
    public function getWordCount(): int
50
    {
51
        return $this->wordCount;
52
    }
53
54
    public function getMinutes(): int
55
    {
56
        return (int) floor($this->getMinutesAsFloat());
57
    }
58
59
    public function getSeconds(): int
60
    {
61
        return $this->seconds;
62
    }
63
64
    protected function getMinutesAsFloat(): float
65
    {
66
        return $this->seconds / 60;
67
    }
68
69
    public function getSecondsOver(): int
70
    {
71
        return (int) round(($this->getMinutesAsFloat() - $this->getMinutes()) * 60);
72
    }
73
74
    public function getFormatted(string $format = '%dmin, %dsec'): string
75
    {
76
        return sprintf($format, $this->getMinutes(), $this->getSecondsOver());
77
    }
78
79
    /** @param  \Closure(int, int): string $closure The closure will receive the minutes and seconds as integers and should return a string. */
80
    public function formatUsingClosure(Closure $closure): string
81
    {
82
        return $closure($this->getMinutes(), $this->getSecondsOver());
83
    }
84
85
    protected function generate(): void
86
    {
87
        $wordCount = str_word_count($this->text);
88
89
        $minutes = $wordCount / static::WORDS_PER_MINUTE;
90
        $seconds = (int) floor($minutes * 60);
91
92
        $this->wordCount = $wordCount;
0 ignored issues
show
Documentation Bug introduced by
It seems like $wordCount can also be of type array. However, the property $wordCount is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
93
        $this->seconds = $seconds;
94
    }
95
}
96