UuidDriver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 22.58 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A code() 0 12 2
A createVersion1Uuid() 0 4 1
A createVersion4Uuid() 0 4 1
A createVersion3Uuid() 7 7 1
A createVersion5Uuid() 7 7 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
namespace Clarkeash\Doorman\Drivers;
4
5
use InvalidArgumentException;
6
use Ramsey\Uuid\Uuid;
7
8
/**
9
 * Class UuidDriver
10
 *
11
 * @package \Clarkeash\Doorman\Drivers
12
 */
13
class UuidDriver implements DriverInterface
14
{
15
    /**
16
     * Create an invite code.
17
     *
18
     * @return string
19
     */
20 9
    public function code(): string
21
    {
22 9
        $version = config('doorman.uuid.version', 4);
23
24 9
        $method = 'createVersion' . $version . 'Uuid';
25
26 9
        if (method_exists($this, $method)) {
27 8
            return $this->$method();
28
        }
29
30 1
        throw new InvalidArgumentException("Version [$version] not supported.");
31
    }
32
33
    /**
34
     * @return string
35
     * @throws \Exception
36
     */
37 1
    protected function createVersion1Uuid(): string
38
    {
39 1
        return Uuid::uuid1()->toString();
40
    }
41
42
    /**
43
     * @return string
44
     * @throws \Throwable
45
     */
46 3 View Code Duplication
    protected function createVersion3Uuid(): string
0 ignored issues
show
Duplication introduced by
This method 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...
47
    {
48 3
        throw_unless(config('doorman.uuid.namespace'), InvalidArgumentException::class, 'Namespace must be set for uuid version 3');
49 2
        throw_unless(config('doorman.uuid.name'), InvalidArgumentException::class, 'Name must be set for uuid version 3');
50
51 1
        return Uuid::uuid3(config('doorman.uuid.namespace'), config('doorman.uuid.namespace'))->toString();
52
    }
53
54
    /**
55
     * @return string
56
     * @throws \Exception
57
     */
58 1
    protected function createVersion4Uuid(): string
59
    {
60 1
        return Uuid::uuid4()->toString();
61
    }
62
63
    /**
64
     * @return string
65
     * @throws \Throwable
66
     */
67 3 View Code Duplication
    protected function createVersion5Uuid(): string
0 ignored issues
show
Duplication introduced by
This method 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...
68
    {
69 3
        throw_unless(config('doorman.uuid.namespace'), InvalidArgumentException::class, 'Namespace must be set for uuid version 5');
70 2
        throw_unless(config('doorman.uuid.name'), InvalidArgumentException::class, 'Name must be set for uuid version 5');
71
72 1
        return Uuid::uuid5(config('doorman.uuid.namespace'), config('doorman.uuid.namespace'))->toString();
73
    }
74
}
75