Truncation::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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