Completed
Push — master ( 6ebd4b...5a00d9 )
by Thijs
03:05
created

NameIDType   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 128
Duplicated Lines 9.38 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 128
rs 10
wmc 17
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
C fromArray() 12 22 10
A __construct() 0 18 4
A toXML() 0 21 3

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
 * SAML NameIDType abstract data type.
4
 *
5
 * @author Jaime Pérez Crespo, UNINETT AS <[email protected]>
6
 * @package simplesamlphp/saml2
7
 */
8
9
namespace SAML2\XML\saml;
10
11
12
abstract class NameIDType extends BaseIDType
13
{
14
    /**
15
     * A URI reference representing the classification of string-based identifier information. See Section 8.3 for the
16
     * SAML-defined URI references that MAY be used as the value of the Format attribute and their associated
17
     * descriptions and processing rules. Unless otherwise specified by an element based on this type, if no Format
18
     * value is provided, then the value urn:oasis:names:tc:SAML:1.0:nameid-format:unspecified (see Section 8.3.1) is in
19
     * effect.
20
     *
21
     * When a Format value other than one specified in Section 8.3 is used, the content of an element of this type is to
22
     * be interpreted according to the definition of that format as provided outside of this specification. If not
23
     * otherwise indicated by the definition of the format, issues of anonymity, pseudonymity, and the persistence of
24
     * the identifier with respect to the asserting and relying parties are implementation-specific.
25
     *
26
     * @var string|null
27
     *
28
     * @see saml-core-2.0-os
29
     */
30
    public $Format = null;
31
32
    /**
33
     * A name identifier established by a service provider or affiliation of providers for the entity, if different from
34
     * the primary name identifier given in the content of the element. This attribute provides a means of integrating
35
     * the use of SAML with existing identifiers already in use by a service provider. For example, an existing
36
     * identifier can be "attached" to the entity using the Name Identifier Management protocol defined in Section 3.6.
37
     *
38
     * @var string|null
39
     *
40
     * @see saml-core-2.0-os
41
     */
42
    public $SPProvidedID = null;
43
44
    /**
45
     * The NameIDType complex type is used when an element serves to represent an entity by a string-valued name.
46
     *
47
     * @var string|null
48
     */
49
    public $value = null;
50
51
52
    /**
53
     * Initialize a saml:NameIDType, either from scratch or from an existing \DOMElement.
54
     *
55
     * @param \DOMElement|null $xml The XML element we should load, if any.
56
     */
57
    public function __construct(\DOMElement $xml = null)
58
    {
59
        parent::__construct($xml);
60
61
        if ($xml === null) {
62
            return;
63
        }
64
65
        if ($xml->hasAttribute('Format')) {
66
            $this->Format = $xml->getAttribute('Format');
67
        }
68
69
        if ($xml->hasAttribute('SPProvidedID')) {
70
            $this->SPProvidedID = $xml->getAttribute('SPProvidedID');
71
        }
72
73
        $this->value = trim($xml->textContent);
74
    }
75
76
77
    /**
78
     * Create a \SAML2\XML\saml\NameID object from an array with its contents.
79
     *
80
     * @param array $nameId An array whose keys correspond to the fields of a NameID.
81
     * @return \SAML2\XML\saml\NameID The corresponding NameID object.
82
     *
83
     * @throws \InvalidArgumentException If the array does not contain the "Value" key.
84
     *
85
     * @deprecated
86
     */
87
    public static function fromArray(array $nameId)
88
    {
89
        $nid = new NameID();
90
        if (!array_key_exists('Value', $nameId)) {
91
            throw new \InvalidArgumentException('Missing "Value" in array, cannot create NameID from it.');
92
        }
93
        $nid->value = $nameId['Value'];
94
95 View Code Duplication
        if (array_key_exists('NameQualifier', $nameId) && $nameId['NameQualifier'] !== null) {
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...
96
            $nid->NameQualifier = $nameId['NameQualifier'];
97
        }
98 View Code Duplication
        if (array_key_exists('SPNameQualifier', $nameId) && $nameId['SPNameQualifier'] !== null) {
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...
99
            $nid->SPNameQualifier = $nameId['SPNameQualifier'];
100
        }
101 View Code Duplication
        if (array_key_exists('SPProvidedID', $nameId) && $nameId['SPProvidedId'] !== null) {
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...
102
            $nid->SPProvidedID = $nameId['SPProvidedID'];
103
        }
104 View Code Duplication
        if (array_key_exists('Format', $nameId) && $nameId['Format'] !== null) {
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...
105
            $nid->Format = $nameId['Format'];
106
        }
107
        return $nid;
108
    }
109
110
111
    /**
112
     * Convert this NameIDType to XML.
113
     *
114
     * @param \DOMElement $element The element we are converting to XML.
0 ignored issues
show
Bug introduced by
There is no parameter named $element. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
115
     *
116
     * @return \DOMElement The XML element after adding the data corresponding to this NameIDType.
117
     */
118
    public function toXML(\DOMElement $parent = null)
119
    {
120
        assert('is_string($this->Format) || is_null($this->Format)');
121
        assert('is_string($this->SPProvidedID) || is_null($this->SPProvidedID)');
122
        assert('is_string($this->value)');
123
124
        $element = parent::toXML($parent);
125
126
        if ($this->Format !== null) {
127
            $element->setAttribute('Format', $this->Format);
128
        }
129
130
        if ($this->SPProvidedID !== null) {
131
            $element->setAttribute('SPProvidedID', $this->SPProvidedID);
132
        }
133
134
        $value = $element->ownerDocument->createTextNode($this->value);
135
        $element->appendChild($value);
136
137
        return $element;
138
    }
139
}
140