Test Failed
Push — master ( 5f8db2...b3d8d2 )
by Waaaaaaaaaa
02:26
created

Truncation::truncate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php declare(strict_types=1);
2
3
namespace Logfile\Inspection;
4
5
class Truncation
6
{
7
    protected $payload;
8
9
    protected $threshold;
10
11
    public function __construct(string $payload, int $threshold = 1024)
12
    {
13
        $this->payload = $payload;
14
        $this->threshold = $threshold;
15
    }
16
17
    public function truncate(): string
18
    {
19
        $size = \mb_strlen($this->payload);
20
21
        if ($size > $this->threshold) {
22
            $newPayload = \mb_substr($this->payload, 0, $this->threshold);
23
            $truncated = $size - $this->threshold;
24
            return \sprintf('%s .. (truncated %d)', $newPayload, $truncated);
25
        }
26
27
        return $this->payload;
28
    }
29
}
30