Scope   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 43
loc 43
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A build() 22 22 5
A __toString() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}