Completed
Push — master ( 8e06a1...b0dbe4 )
by Jaime Pérez
05:41
created

BaseIDType::toXML()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 24
Code Lines 15

Duplication

Lines 6
Ratio 25 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 8
nop 1
dl 6
loc 24
rs 8.6845
c 0
b 0
f 0
1
<?php
2
/**
3
 * Base class corresponding to the BaseID element.
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
use SAML2\Constants;
13
use SAML2\DOMDocumentFactory;
14
15
abstract class BaseIDType
16
{
17
    /**
18
     * The security or administrative domain that qualifies the identifier.
19
     * This attribute provides a means to federate identifiers from disparate user stores without collision.
20
     *
21
     * @see saml-core-2.0-os
22
     *
23
     * @var string|null
24
     */
25
    public $NameQualifier = null;
26
27
    /**
28
     * Further qualifies an identifier with the name of a service provider or affiliation of providers.
29
     * This attribute provides an additional means to federate identifiers on the basis of the relying party or parties.
30
     *
31
     * @see saml-core-2.0-os
32
     *
33
     * @var string|null
34
     */
35
    public $SPNameQualifier = null;
36
37
    /**
38
     * The name for this BaseID.
39
     *
40
     * Override in classes extending this class to get the desired name.
41
     *
42
     * @var string
43
     */
44
    protected $nodeName;
45
46
47
    /**
48
     * Initialize a saml:BaseID, either from scratch or from an existing \DOMElement.
49
     *
50
     * @param \DOMElement|null $xml The XML element we should load, if any.
51
     */
52
    public function __construct(\DOMElement $xml = null)
53
    {
54
        if ($xml === null) {
55
            return;
56
        }
57
58
        $this->element = $xml;
0 ignored issues
show
Bug introduced by
The property element does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
60
        if ($xml->hasAttribute('NameQualifier')) {
61
            $this->NameQualifier = $xml->getAttribute('NameQualifier');
62
        }
63
64
        if ($xml->hasAttribute('SPNameQualifier')) {
65
            $this->SPNameQualifier = $xml->getAttribute('SPNameQualifier');
66
        }
67
    }
68
69
70
    /**
71
     * Convert this BaseID to XML.
72
     *
73
     * @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...
74
     * @return \DOMElement The XML element after adding the data corresponding to this BaseID.
75
     */
76
    public function toXML(\DOMElement $parent = null)
77
    {
78
        assert('is_string($this->NameQualifier) || is_null($this->NameQualifier');
79
        assert('is_string($this->SPNameQualifier) || is_null($this->SPNameQualifier');
80
81 View Code Duplication
        if ($parent === 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...
82
            $parent = DOMDocumentFactory::create();
83
            $doc = $parent;
84
        } else {
85
            $doc = $parent->ownerDocument;
86
        }
87
        $element = $doc->createElementNS(Constants::NS_SAML, $this->nodeName);
88
        $parent->appendChild($element);
89
90
        if ($this->NameQualifier !== null) {
91
            $element->setAttribute('NameQualifier', $this->NameQualifier);
92
        }
93
94
        if ($this->SPNameQualifier !== null) {
95
            $element->setAttribute('SPNameQualifier', $this->SPNameQualifier);
96
        }
97
98
        return $element;
99
    }
100
101
102
    /**
103
     * Get a string representation of this BaseIDType object.
104
     *
105
     * @return string The resulting XML, as a string.
106
     */
107
    public function __toString()
108
    {
109
        assert('!is_null($this->element)');
110
111
        $doc = DOMDocumentFactory::create();
112
        $root = $doc->createElementNS(Constants::NS_SAML, 'root');
113
        $ele = $this->toXML($root);
114
115
        return $doc->saveXML($ele);
116
    }
117
}
118