Description::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damianopetrungaro\PHPCommitizen\Section;
6
7
use Damianopetrungaro\PHPCommitizen\Configuration;
8
use Damianopetrungaro\PHPCommitizen\Exception\InvalidArgumentException;
9
use function strlen;
10
11
class Description
12
{
13
    /**
14
     * @var string
15
     */
16
    private $description;
17
18 5
    private function __construct(string $description)
19
    {
20 5
        $this->description = $description;
21 5
    }
22
23
    /**
24
     * @throws InvalidArgumentException
25
     */
26 8
    public static function build(string $description, Configuration $configuration): self
27
    {
28 8
        $description = trim($description);
29 8
        $descriptionLength = strlen($description);
30
31 8
        if ($descriptionLength > $configuration->maxLengthDescription() ||
32 8
            $descriptionLength < $configuration->minLengthDescription()
33
        ) {
34 3
            $errorMessage = sprintf("Invalid length for description: '%s'. Must be between %s and %s",
35 3
                $description,
36 3
                $configuration->minLengthDescription(),
37 3
                $configuration->maxLengthDescription()
38
            );
39 3
            throw new InvalidArgumentException($errorMessage);
40
        }
41
42 5
        return new self($description);
43
    }
44
45 5
    public function __toString(): string
46
    {
47 5
        return $this->description;
48
    }
49
}