Completed
Push — master ( dac7df...b079db )
by Daniel
03:09
created

NotificationType   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 20.55 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 2
cbo 2
dl 15
loc 73
ccs 0
cts 33
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C fromString() 15 38 7
B toString() 0 21 5

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
 * This file is part of the Ssdp project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Ssdp;
9
10
use Ramsey\Uuid\Uuid;
11
12
/**
13
 * Notification type
14
 *
15
 * @package GravityMedia\Ssdp
16
 */
17
class NotificationType extends AbstractIdentifier
18
{
19
    /**
20
     * Create notification type from string.
21
     *
22
     * @param string $searchTarget
23
     *
24
     * @return self
25
     */
26
    public static function fromString($searchTarget)
27
    {
28
        $instance = new static();
29
        $searchTarget = strtolower(trim($searchTarget));
30
31
        if ($searchTarget === 'upnp:rootdevice') {
32
            return $instance->setRootDevice(true);
33
        }
34
35
        if (substr($searchTarget, 0, 4) === 'upnp') {
36
            return $instance->setId(Uuid::fromString(substr($searchTarget, 5)));
37
        }
38
39
        $parts = explode(':', $searchTarget, 5);
40 View Code Duplication
        if (5 !== count($parts) || 'urn' !== array_shift($parts)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
41
            throw new \InvalidArgumentException('Invalid string.');
42
        }
43
44
        $instance->setDomainName(array_shift($parts));
45
46
        $name = array_shift($parts);
47
48 View Code Duplication
        if ('device' === $name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
49
            return $instance
50
                ->setDevice(true)
51
                ->setType(array_shift($parts))
52
                ->setVersion((int)array_shift($parts));
53
        }
54
55 View Code Duplication
        if ('service' === $name) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56
            return $instance
57
                ->setService(true)
58
                ->setType(array_shift($parts))
59
                ->setVersion((int)array_shift($parts));
60
        }
61
62
        throw new \InvalidArgumentException('Invalid string.');
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function toString()
69
    {
70
        if ($this->isRootDevice()) {
71
            return 'upnp:rootdevice';
72
        }
73
74
        if ($this->isDevice()) {
75
            return sprintf('urn:%s:device:%s:%u', $this->getDomainName(), $this->getType(), $this->getVersion());
76
        }
77
78
        if ($this->isService()) {
79
            return sprintf('urn:%s:service:%s:%u', $this->getDomainName(), $this->getType(), $this->getVersion());
80
        }
81
82
        $id = $this->getId();
83
        if (null === $id) {
84
            $id = Uuid::fromString(Uuid::NIL);
85
        }
86
87
        return sprintf('uuid:%s', $id);
88
    }
89
}
90