Completed
Push — master ( 4f6af7...d55336 )
by Thijs
14s queued 10s
created

EndpointType   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 179
Duplicated Lines 9.5 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 17
loc 179
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hasAttributeNS() 9 9 1
A getAttributeNS() 0 12 2
A setAttributeNS() 0 18 2
A removeAttributeNS() 8 8 1
C __construct() 0 32 7
B toXML() 0 23 4

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 SAML2\XML\md;
4
5
use SAML2\Constants;
6
7
/**
8
 * Class representing SAML 2 EndpointType.
9
 *
10
 * @package SimpleSAMLphp
11
 */
12
class EndpointType
13
{
14
    /**
15
     * The binding for this endpoint.
16
     *
17
     * @var string
18
     */
19
    public $Binding;
20
21
    /**
22
     * The URI to this endpoint.
23
     *
24
     * @var string
25
     */
26
    public $Location;
27
28
    /**
29
     * The URI where responses can be delivered.
30
     *
31
     * @var string|null
32
     */
33
    public $ResponseLocation = null;
34
35
    /**
36
     * Extra (namespace qualified) attributes.
37
     *
38
     * @var array
39
     */
40
    private $attributes = array();
41
42
    /**
43
     * Initialize an EndpointType.
44
     *
45
     * @param \DOMElement|null $xml The XML element we should load.
46
     * @throws \Exception
47
     */
48
    public function __construct(\DOMElement $xml = null)
49
    {
50
        if ($xml === null) {
51
            return;
52
        }
53
54
        if (!$xml->hasAttribute('Binding')) {
55
            throw new \Exception('Missing Binding on ' . $xml->tagName);
56
        }
57
        $this->Binding = $xml->getAttribute('Binding');
58
59
        if (!$xml->hasAttribute('Location')) {
60
            throw new \Exception('Missing Location on ' . $xml->tagName);
61
        }
62
        $this->Location = $xml->getAttribute('Location');
63
64
        if ($xml->hasAttribute('ResponseLocation')) {
65
            $this->ResponseLocation = $xml->getAttribute('ResponseLocation');
66
        }
67
68
        foreach ($xml->attributes as $a) {
69
            if ($a->namespaceURI === null) {
70
                continue; /* Not namespace-qualified -- skip. */
71
            }
72
            $fullName = '{' . $a->namespaceURI . '}' . $a->localName;
73
            $this->attributes[$fullName] = array(
74
                'qualifiedName' => $a->nodeName,
75
                'namespaceURI' => $a->namespaceURI,
76
                'value' => $a->value,
77
            );
78
        }
79
    }
80
81
    /**
82
     * Check if a namespace-qualified attribute exists.
83
     *
84
     * @param  string  $namespaceURI The namespace URI.
85
     * @param  string  $localName    The local name.
86
     * @return boolean true if the attribute exists, false if not.
87
     */
88 View Code Duplication
    public function hasAttributeNS($namespaceURI, $localName)
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...
89
    {
90
        assert(is_string($namespaceURI));
91
        assert(is_string($localName));
92
93
        $fullName = '{' . $namespaceURI . '}' . $localName;
94
95
        return isset($this->attributes[$fullName]);
96
    }
97
98
    /**
99
     * Get a namespace-qualified attribute.
100
     *
101
     * @param  string $namespaceURI The namespace URI.
102
     * @param  string $localName    The local name.
103
     * @return string The value of the attribute, or an empty string if the attribute does not exist.
104
     */
105
    public function getAttributeNS($namespaceURI, $localName)
106
    {
107
        assert(is_string($namespaceURI));
108
        assert(is_string($localName));
109
110
        $fullName = '{' . $namespaceURI . '}' . $localName;
111
        if (!isset($this->attributes[$fullName])) {
112
            return '';
113
        }
114
115
        return $this->attributes[$fullName]['value'];
116
    }
117
118
    /**
119
     * Get a namespace-qualified attribute.
120
     *
121
     * @param string $namespaceURI  The namespace URI.
122
     * @param string $qualifiedName The local name.
123
     * @param string $value         The attribute value.
124
     * @throws \Exception
125
     */
126
    public function setAttributeNS($namespaceURI, $qualifiedName, $value)
127
    {
128
        assert(is_string($namespaceURI));
129
        assert(is_string($qualifiedName));
130
131
        $name = explode(':', $qualifiedName, 2);
132
        if (count($name) < 2) {
133
            throw new \Exception('Not a qualified name.');
134
        }
135
        $localName = $name[1];
136
137
        $fullName = '{' . $namespaceURI . '}' . $localName;
138
        $this->attributes[$fullName] = array(
139
            'qualifiedName' => $qualifiedName,
140
            'namespaceURI' => $namespaceURI,
141
            'value' => $value,
142
        );
143
    }
144
145
    /**
146
     * Remove a namespace-qualified attribute.
147
     *
148
     * @param string $namespaceURI The namespace URI.
149
     * @param string $localName    The local name.
150
     */
151 View Code Duplication
    public function removeAttributeNS($namespaceURI, $localName)
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...
152
    {
153
        assert(is_string($namespaceURI));
154
        assert(is_string($localName));
155
156
        $fullName = '{' . $namespaceURI . '}' . $localName;
157
        unset($this->attributes[$fullName]);
158
    }
159
160
    /**
161
     * Add this endpoint to an XML element.
162
     *
163
     * @param \DOMElement $parent The element we should append this endpoint to.
164
     * @param string     $name   The name of the element we should create.
165
     * @return \DOMElement
166
     */
167
    public function toXML(\DOMElement $parent, $name)
168
    {
169
        assert(is_string($name));
170
        assert(is_string($this->Binding));
171
        assert(is_string($this->Location));
172
        assert(is_null($this->ResponseLocation) || is_string($this->ResponseLocation));
173
174
        $e = $parent->ownerDocument->createElementNS(Constants::NS_MD, $name);
175
        $parent->appendChild($e);
176
177
        $e->setAttribute('Binding', $this->Binding);
178
        $e->setAttribute('Location', $this->Location);
179
180
        if (isset($this->ResponseLocation)) {
181
            $e->setAttribute('ResponseLocation', $this->ResponseLocation);
182
        }
183
184
        foreach ($this->attributes as $a) {
185
            $e->setAttributeNS($a['namespaceURI'], $a['qualifiedName'], $a['value']);
186
        }
187
188
        return $e;
189
    }
190
}
191