Scope::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 4
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 implode;
10
use function in_array;
11
use function strlen;
12
13 View Code Duplication
class Scope
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...
14
{
15
    /**
16
     * @var string
17
     */
18
    private $scope;
19
20 3
    private function __construct(string $scope)
21
    {
22 3
        $this->scope = $scope;
23 3
    }
24
25
    /**
26
     * @throws InvalidArgumentException
27
     */
28 6
    public static function build(string $scope, Configuration $configuration): self
29
    {
30 6
        $scope = trim($scope);
31 6
        $scopeLength = strlen($scope);
32
33 6
        if ($scopeLength > $configuration->maxLengthScope() || $scopeLength < $configuration->minLengthScope()) {
34 2
            $errorMessage = sprintf("Invalid length for scope: '%s'. Must be between %s and %s",
35 2
                $scope,
36 2
                $configuration->minLengthScope(),
37 2
                $configuration->maxLengthScope()
38
            );
39 2
            throw new InvalidArgumentException($errorMessage);
40
        }
41
42 4
        if (!$configuration->acceptExtraScope() && !in_array($scope, $configuration->scopes(), true)) {
43 1
            $validScopes = implode(', ', $configuration->scopes());
44 1
            throw new InvalidArgumentException("Invalid scope: '$scope'. Valid scopes are: [$validScopes]");
45
        }
46
47
48 3
        return new self($scope);
49
    }
50
51 3
    public function __toString(): string
52
    {
53 3
        return "($this->scope)";
54
    }
55
}