Passed
Push — master ( 77de30...691bbc )
by Tim
01:35
created

NameIDMappingResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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

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\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Constants 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...
10
use SimpleSAML\SAML2\Exception\Protocol\RequestVersionTooHighException;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Excepti...VersionTooHighException 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...
11
use SimpleSAML\SAML2\Exception\Protocol\RequestVersionTooLowException;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Excepti...tVersionTooLowException 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...
12
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
13
use SimpleSAML\SAML2\Type\SAMLDateTimeValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Type\SAMLDateTimeValue 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...
14
use SimpleSAML\SAML2\Type\SAMLStringValue;
15
use SimpleSAML\SAML2\XML\IdentifierTrait;
16
use SimpleSAML\SAML2\XML\saml\Assertion;
17
use SimpleSAML\SAML2\XML\saml\EncryptedAssertion;
18
use SimpleSAML\SAML2\XML\saml\EncryptedID;
19
use SimpleSAML\SAML2\XML\saml\Issuer;
20
use SimpleSAML\SAML2\XML\saml\NameID;
21
use SimpleSAML\XML\SchemaValidatableElementInterface;
22
use SimpleSAML\XML\SchemaValidatableElementTrait;
23
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
24
use SimpleSAML\XMLSchema\Exception\MissingElementException;
25
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
26
use SimpleSAML\XMLSchema\Type\IDValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSchema\Type\IDValue 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...
27
use SimpleSAML\XMLSchema\Type\NCNameValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSchema\Type\NCNameValue 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...
28
use SimpleSAML\XMLSecurity\XML\ds\Signature;
29
30
use function array_merge;
31
use function array_pop;
32
use function strval;
33
34
/**
35
 * Class for SAML 2 NameIDMappingResponse messages.
36
 *
37
 * @package simplesamlphp/saml2
38
 */
39
class NameIDMappingResponse extends AbstractStatusResponse implements SchemaValidatableElementInterface
40
{
41
    use IdentifierTrait;
42
    use SchemaValidatableElementTrait;
43
44
45
    /**
46
     * Constructor for SAML 2 response messages.
47
     *
48
     * @param \SimpleSAML\SAML2\XML\saml\NameID|\SimpleSAML\SAML2\XML\saml\EncryptedID $identifier
49
     * @param \SimpleSAML\XMLSchema\Type\IDValue $id
50
     * @param \SimpleSAML\SAML2\XML\samlp\Status $status
51
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue $issueInstant
52
     * @param \SimpleSAML\SAML2\XML\saml\Issuer|null $issuer
53
     * @param \SimpleSAML\XMLSchema\Type\NCNameValue|null $inResponseTo
54
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $destination
55
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $consent
56
     * @param \SimpleSAML\SAML2\XML\samlp\Extensions $extensions
57
     */
58
    final public function __construct(
59
        NameID|EncryptedID $identifier,
60
        IDValue $id,
61
        Status $status,
62
        SAMLDateTimeValue $issueInstant,
63
        ?Issuer $issuer = null,
64
        ?NCNameValue $inResponseTo = null,
65
        ?SAMLAnyURIValue $destination = null,
66
        ?SAMLAnyURIValue $consent = null,
67
        ?Extensions $extensions = null,
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\samlp\Extensions 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...
68
    ) {
69
        $this->setIdentifier($identifier);
70
71
        parent::__construct(
72
            $id,
73
            $status,
74
            $issueInstant,
75
            $issuer,
76
            $inResponseTo,
77
            $destination,
78
            $consent,
79
            $extensions,
80
        );
81
    }
82
83
84
    /**
85
     * Convert XML into a Response element.
86
     *
87
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
88
     *   if the qualified name of the supplied element is wrong
89
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
90
     *   if the supplied element is missing one of the mandatory attributes
91
     * @throws \SimpleSAML\XMLSchema\Exception\MissingElementException
92
     *   if one of the mandatory child-elements is missing
93
     */
94
    public static function fromXML(DOMElement $xml): static
95
    {
96
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
97
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
98
99
        $version = self::getAttribute($xml, 'Version', SAMLStringValue::class);
100
        Assert::true(version_compare('2.0', strval($version), '<='), RequestVersionTooLowException::class);
101
        Assert::true(version_compare('2.0', strval($version), '>='), RequestVersionTooHighException::class);
102
103
        $signature = Signature::getChildrenOfClass($xml);
104
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.', TooManyElementsException::class);
105
106
        $issuer = Issuer::getChildrenOfClass($xml);
107
        Assert::countBetween($issuer, 0, 1);
108
109
        $status = Status::getChildrenOfClass($xml);
110
        Assert::minCount($status, 1, MissingElementException::class);
111
        Assert::maxCount($status, 1, TooManyElementsException::class);
112
113
        $extensions = Extensions::getChildrenOfClass($xml);
114
        Assert::maxCount(
115
            $extensions,
116
            1,
117
            'Only one saml:Extensions element is allowed.',
118
            TooManyElementsException::class,
119
        );
120
121
        $response = new static(
122
            self::getIdentifierFromXML($xml),
0 ignored issues
show
Bug introduced by
It seems like self::getIdentifierFromXML($xml) can also be of type null; however, parameter $identifier of SimpleSAML\SAML2\XML\sam...Response::__construct() does only seem to accept SimpleSAML\SAML2\XML\sam...L\SAML2\XML\saml\NameID, maybe add an additional type check? ( Ignorable by Annotation )

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

122
            /** @scrutinizer ignore-type */ self::getIdentifierFromXML($xml),
Loading history...
123
            self::getAttribute($xml, 'ID', IDValue::class),
124
            array_pop($status),
125
            self::getAttribute($xml, 'IssueInstant', SAMLDateTimeValue::class),
126
            empty($issuer) ? null : array_pop($issuer),
127
            self::getOptionalAttribute($xml, 'InResponseTo', NCNameValue::class, null),
128
            self::getOptionalAttribute($xml, 'Destination', SAMLAnyURIValue::class, null),
129
            self::getOptionalAttribute($xml, 'Consent', SAMLAnyURIValue::class, null),
130
            empty($extensions) ? null : array_pop($extensions),
131
        );
132
133
        if (!empty($signature)) {
134
            $response->setSignature($signature[0]);
135
            $response->messageContainedSignatureUponConstruction = true;
136
            $response->setXML($xml);
137
        }
138
139
        return $response;
140
    }
141
142
143
    /**
144
     * Convert the response message to an XML element.
145
     */
146
    protected function toUnsignedXML(?DOMElement $parent = null): DOMElement
147
    {
148
        $e = parent::toUnsignedXML($parent);
149
150
        $this->getIdentifier()->toXML($e);
0 ignored issues
show
Bug introduced by
The method toXML() does not exist on SimpleSAML\SAML2\XML\saml\IdentifierInterface. It seems like you code against a sub-type of said class. However, the method does not exist in SimpleSAML\SAML2\XML\saml\BaseIdentifierInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

150
        $this->getIdentifier()->/** @scrutinizer ignore-call */ toXML($e);
Loading history...
151
152
        return $e;
153
    }
154
}
155