Passed
Push — master ( 075c37...f11aa4 )
by Tim
02:36
created

LogoutResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 9
dl 0
loc 12
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\samlp;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
10
use SimpleSAML\SAML2\XML\saml\Issuer;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Utils as XMLUtils;
13
use SimpleSAML\XMLSecurity\XML\ds\Signature;
14
15
use function array_pop;
16
17
/**
18
 * Class for SAML 2 LogoutResponse messages.
19
 *
20
 * @package simplesamlphp/saml2
21
 */
22
class LogoutResponse extends AbstractStatusResponse
23
{
24
    /**
25
     * Constructor for SAML 2 LogoutResponse.
26
     *
27
     * @param \SimpleSAML\SAML2\XML\samlp\Status $status
28
     * @param \SimpleSAML\SAML2\XML\saml\Issuer|null $issuer
29
     * @param string|null $id
30
     * @param int|null $issueInstant
31
     * @param string|null $inResponseTo
32
     * @param string|null $destination
33
     * @param string|null $consent
34
     * @param \SimpleSAML\SAML2\XML\samlp\Extensions|null $extensions
35
     * @param string|null $relayState
36
     *
37
     * @throws \Exception
38
     */
39
    public function __construct(
40
        Status $status,
41
        ?Issuer $issuer = null,
42
        ?string $id = null,
43
        ?int $issueInstant = null,
44
        ?string $inResponseTo = null,
45
        ?string $destination = null,
46
        ?string $consent = null,
47
        ?Extensions $extensions = null,
48
        ?string $relayState = null
49
    ) {
50
        parent::__construct($status, $issuer, $id, $issueInstant, $inResponseTo, $destination, $consent, $extensions, $relayState);
51
    }
52
53
54
    /**
55
     * Convert XML into an LogoutResponse
56
     *
57
     * @param \DOMElement $xml
58
     * @return self
59
     *
60
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
61
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the supplied element is missing one of the mandatory attributes
62
     */
63
    public static function fromXML(DOMElement $xml): object
64
    {
65
        Assert::same($xml->localName, 'LogoutResponse', InvalidDOMElementException::class);
66
        Assert::same($xml->namespaceURI, LogoutResponse::NS, InvalidDOMElementException::class);
67
        Assert::same('2.0', self::getAttribute($xml, 'Version'));
68
69
        $issueInstant = self::getAttribute($xml, 'IssueInstant');
70
        Assert::validDateTimeZulu($issueInstant, ProtocolViolationException::class);
71
        $issueInstant = XMLUtils::xsDateTimeToTimestamp($issueInstant);
72
73
        $issuer = Issuer::getChildrenOfClass($xml);
74
        Assert::countBetween($issuer, 0, 1);
75
76
        $status = Status::getChildrenOfClass($xml);
77
        Assert::count($status, 1);
78
79
        $extensions = Extensions::getChildrenOfClass($xml);
80
        Assert::maxCount($extensions, 1, 'Only one saml:Extensions element is allowed.');
81
82
        $signature = Signature::getChildrenOfClass($xml);
83
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.');
84
85
        $response = new self(
86
            array_pop($status),
87
            array_pop($issuer),
88
            self::getAttribute($xml, 'ID'),
89
            $issueInstant,
90
            self::getAttribute($xml, 'InResponseTo', null),
91
            self::getAttribute($xml, 'Destination', null),
92
            self::getAttribute($xml, 'Consent', null),
93
            empty($extensions) ? null : array_pop($extensions)
94
        );
95
96
        if (!empty($signature)) {
97
            $response->setSignature($signature[0]);
98
            $response->messageContainedSignatureUponConstruction = true;
99
        }
100
101
        return $response;
102
    }
103
104
105
    /**
106
     * Convert status response message to an XML element.
107
     *
108
     * @inheritDoc
109
     * @return \DOMElement This status response.
110
     */
111
    public function toXML(?DOMElement $parent = null): DOMElement
112
    {
113
        $e = parent::toXML($parent);
114
115
        if ($this->signer !== null) {
0 ignored issues
show
Bug Best Practice introduced by
The property signer does not exist on SimpleSAML\SAML2\XML\samlp\LogoutResponse. Did you maybe forget to declare it?
Loading history...
116
            $signedXML = $this->doSign($e);
0 ignored issues
show
Bug introduced by
The method doSign() does not exist on SimpleSAML\SAML2\XML\samlp\LogoutResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
            /** @scrutinizer ignore-call */ 
117
            $signedXML = $this->doSign($e);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
118
            // Test for an Issuer
119
            $responseElements = XPath::xpQuery($signedXML, './saml_assertion:Issuer', XPath::getXPath($signedXML));
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\samlp\XPath was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
120
            $issuer = array_pop($responseElements);
121
122
            $signedXML->insertBefore($this->signature->toXML($signedXML), $issuer->nextSibling);
0 ignored issues
show
Bug introduced by
The method toXML() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
            $signedXML->insertBefore($this->signature->/** @scrutinizer ignore-call */ toXML($signedXML), $issuer->nextSibling);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
            return $signedXML;
124
        }
125
126
        return $e;
127
    }
128
}
129