Type::build()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 2
dl 21
loc 21
ccs 13
cts 13
cp 1
crap 5
rs 9.2728
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 implode;
10
use function in_array;
11
use function sprintf;
12
use function strlen;
13
14 View Code Duplication
class Type
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    /**
17
     * @var string
18
     */
19
    private $type;
20
21 3
    private function __construct(string $type)
22
    {
23 3
        $this->type = $type;
24 3
    }
25
26
    /**
27
     * @throws InvalidArgumentException
28
     */
29 6
    public static function build(string $type, Configuration $configuration): self
30
    {
31 6
        $type = trim($type);
32 6
        $typeLength = strlen($type);
33
34 6
        if ($typeLength > $configuration->maxLengthType() || $typeLength < $configuration->minLengthType()) {
35 2
            $errorMessage = sprintf("Invalid length for type: '%s'. Must be between %s and %s",
36 2
                $type,
37 2
                $configuration->minLengthType(),
38 2
                $configuration->maxLengthType()
39
            );
40 2
            throw new InvalidArgumentException($errorMessage);
41
        }
42
43 4
        if (!$configuration->acceptExtraType() && !in_array($type, $configuration->types(), true)) {
44 1
            $validTypes = implode(', ', $configuration->types());
45 1
            throw new InvalidArgumentException("Invalid type: '$type'. Valid types are: [$validTypes]");
46
        }
47
48 3
        return new self($type);
49
    }
50
51 3
    public function __toString(): string
52
    {
53 3
        return $this->type;
54
    }
55
}