Truncation::truncate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
ccs 4
cts 7
cp 0.5714
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2.3149
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 1
    public function __construct(string $payload, int $threshold = 1024)
12
    {
13 1
        $this->payload = $payload;
14 1
        $this->threshold = $threshold;
15 1
    }
16
17 1
    public function truncate(): string
18
    {
19 1
        $size = \mb_strlen($this->payload);
20
21 1
        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 1
        return $this->payload;
28
    }
29
}
30