Passed
Push — master ( adb24b...f5ed45 )
by Tim
12:31
created

CustomAssertionTrait::validEntityID()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\Assert;
6
7
use SimpleSAML\Assert\Assert as BaseAssert;
8
use SimpleSAML\Assert\AssertionFailedException;
9
use SimpleSAML\SAML11\Constants as C;
10
use SimpleSAML\SAML11\Exception\ProtocolViolationException;
11
use SimpleSAML\XML\Exception\SchemaViolationException;
12
13
/**
14
 * @package simplesamlphp/assert
15
 */
16
trait CustomAssertionTrait
17
{
18
    private static string $scheme_regex = '/^([a-z][a-z0-9\+\-\.]+[:])/i';
19
20
    /***********************************************************************************
21
     *  NOTE:  Custom assertions may be added below this line.                         *
22
     *         They SHOULD be marked as `private` to ensure the call is forced         *
23
     *          through __callStatic().                                                *
24
     *         Assertions marked `public` are called directly and will                 *
25
     *          not handle any custom exception passed to it.                          *
26
     ***********************************************************************************/
27
28
29
    /**
30
     * @param string $value
31
     * @param string $message
32
     */
33
    private static function validDateTime(string $value, string $message = ''): void
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed. ( Ignorable by Annotation )

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

33
    private static function validDateTime(string $value, /** @scrutinizer ignore-unused */ string $message = ''): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
34
    {
35
        try {
36
            BaseAssert::validDateTime($value);
37
        } catch (AssertionFailedException $e) {
38
            throw new SchemaViolationException($e->getMessage());
39
        }
40
41
        try {
42
            BaseAssert::endsWith(
43
                $value,
44
                'Z',
45
                '%s is not a DateTime expressed in the UTC timezone using the \'Z\' timezone identifier.',
46
            );
47
        } catch (AssertionFailedException $e) {
48
            throw new ProtocolViolationException($e->getMessage());
49
        }
50
    }
51
52
53
    /**
54
     * @param string $value
55
     * @param string $message
56
     */
57
    private static function validURI(string $value, string $message = ''): void
58
    {
59
        try {
60
            BaseAssert::validURI($value);
61
        } catch (AssertionFailedException $e) {
62
            throw new SchemaViolationException($e->getMessage());
63
        }
64
65
        try {
66
            BaseAssert::notWhitespaceOnly($value, $message ?: '%s is not a SAML1.1-compliant URI');
67
68
            // If it doesn't have a scheme, it's not an absolute URI
69
            BaseAssert::regex($value, self::$scheme_regex, $message ?: '%s is not a SAML1.1-compliant URI');
70
        } catch (AssertionFailedException $e) {
71
            throw new ProtocolViolationException($e->getMessage());
72
        }
73
    }
74
75
76
    /**
77
     * @param string $value
78
     * @param string $message
79
     */
80
    private static function validEntityID(string $value, string $message = ''): void
0 ignored issues
show
Unused Code introduced by
The parameter $message is not used and could be removed. ( Ignorable by Annotation )

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

80
    private static function validEntityID(string $value, /** @scrutinizer ignore-unused */ string $message = ''): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
        static::validURI($value);
83
84
        try {
85
            BaseAssert::maxLength(
86
                $value,
87
                C::ENTITYID_MAX_LENGTH,
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\SAML11\Constants::ENTITYID_MAX_LENGTH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
88
                sprintf('An entityID cannot be longer than %d characters.', C::ENTITYID_MAX_LENGTH),
89
            );
90
        } catch (AssertionFailedException $e) {
91
            throw new ProtocolViolationException($e->getMessage());
92
        }
93
    }
94
}
95