StatementValidator::validateAuthnContext()   B
last analyzed

Complexity

Conditions 11
Paths 9

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 11

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
ccs 16
cts 16
cp 1
rs 7.3166
cc 11
nc 9
nop 1
crap 11

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Validator\Model\Statement;
13
14
use LightSaml\Error\LightSamlValidationException;
15
use LightSaml\Helper;
16
use LightSaml\Model\Assertion\AbstractStatement;
17
use LightSaml\Model\Assertion\Attribute;
18
use LightSaml\Model\Assertion\AttributeStatement;
19
use LightSaml\Model\Assertion\AuthnContext;
20
use LightSaml\Model\Assertion\AuthnStatement;
21
22
class StatementValidator implements StatementValidatorInterface
23
{
24
    /**
25
     * @param AbstractStatement $statement
26
     *
27
     * @throws \LightSaml\Error\LightSamlValidationException
28
     *
29
     * @return void
30
     */
31 15
    public function validateStatement(AbstractStatement $statement)
32
    {
33 15
        if ($statement instanceof AuthnStatement) {
34 11
            $this->validateAuthnStatement($statement);
35 5
        } elseif ($statement instanceof AttributeStatement) {
36 4
            $this->validateAttributeStatement($statement);
37
        } else {
38 1
            throw new LightSamlValidationException(sprintf("Unsupported Statement type '%s'", get_class($statement)));
39
        }
40 3
    }
41
42 11
    private function validateAuthnStatement(AuthnStatement $statement)
43
    {
44 11
        if (false == $statement->getAuthnInstantTimestamp()) {
45 1
            throw new LightSamlValidationException('AuthnStatement MUST have an AuthnInstant attribute');
46
        }
47 10
        if (false == Helper::validateOptionalString($statement->getSessionIndex())) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
48 1
            throw new LightSamlValidationException('SessionIndex attribute of AuthnStatement must contain at least one non-whitespace character');
49
        }
50 9
        if ($statement->getSubjectLocality()) {
51 3
            if (false == Helper::validateOptionalString($statement->getSubjectLocality()->getAddress())) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
52 1
                throw new LightSamlValidationException('Address attribute of SubjectLocality must contain at least one non-whitespace character');
53
            }
54 2
            if (false == Helper::validateOptionalString($statement->getSubjectLocality()->getDnsName())) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
55 1
                throw new LightSamlValidationException('DNSName attribute of SubjectLocality must contain at least one non-whitespace character');
56
            }
57
        }
58 7
        if (false == $statement->getAuthnContext()) {
59 1
            throw new LightSamlValidationException('AuthnStatement MUST have an AuthnContext element');
60
        }
61 6
        $this->validateAuthnContext($statement->getAuthnContext());
62 2
    }
63
64 6
    private function validateAuthnContext(AuthnContext $authnContext)
65
    {
66 6
        if (false == $authnContext->getAuthnContextClassRef() &&
67 6
            false == $authnContext->getAuthnContextDecl() &&
68 6
            false == $authnContext->getAuthnContextDeclRef()
69
        ) {
70 1
            throw new LightSamlValidationException('AuthnContext element MUST contain at least one AuthnContextClassRef, AuthnContextDecl or AuthnContextDeclRef element');
71
        }
72
73 5
        if ($authnContext->getAuthnContextClassRef() &&
74 5
            $authnContext->getAuthnContextDecl() &&
75 5
            $authnContext->getAuthnContextDeclRef()
0 ignored issues
show
Bug Best Practice introduced by
The expression $authnContext->getAuthnContextDeclRef() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
76
        ) {
77 1
            throw new LightSamlValidationException('AuthnContext MUST NOT contain more than two elements.');
78
        }
79
80 4
        if ($authnContext->getAuthnContextClassRef()) {
81 3
            if (false == Helper::validateWellFormedUriString($authnContext->getAuthnContextClassRef())) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
82 1
                throw new LightSamlValidationException('AuthnContextClassRef has a value which is not a wellformed absolute uri');
83
            }
84
        }
85 3
        if ($authnContext->getAuthnContextDeclRef()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $authnContext->getAuthnContextDeclRef() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
86 1
            if (false === Helper::validateWellFormedUriString($authnContext->getAuthnContextDeclRef())) {
87 1
                throw new LightSamlValidationException('AuthnContextDeclRef has a value which is not a wellformed absolute uri');
88
            }
89
        }
90 2
    }
91
92 4
    private function validateAttributeStatement(AttributeStatement $statement)
93
    {
94 4
        if (false == $statement->getAllAttributes()) {
95 1
            throw new LightSamlValidationException('AttributeStatement MUST contain at least one Attribute or EncryptedAttribute');
96
        }
97
98 3
        foreach ($statement->getAllAttributes() as $attribute) {
99 3
            $this->validateAttribute($attribute);
100
        }
101 2
    }
102
103
    /**
104
     * @param Attribute $attribute
105
     *
106
     * @throws LightSamlValidationException
107
     *
108
     * @return void
109
     */
110 3
    private function validateAttribute(Attribute $attribute)
111
    {
112 3
        if (false == Helper::validateRequiredString($attribute->getName())) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
113 1
            throw new LightSamlValidationException('Name attribute of Attribute element MUST contain at least one non-whitespace character');
114
        }
115 2
    }
116
}
117