Passed
Pull Request — master (#359)
by Tim
02:55
created

CustomAssertionTrait::validDateTime()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Assert;
6
7
use SimpleSAML\Assert\Assert as BaseAssert;
8
use SimpleSAML\Assert\AssertionFailedException;
9
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
12
/**
13
 * @package simplesamlphp/assert
14
 */
15
trait CustomAssertionTrait
16
{
17
    private const SCHEME_REGEX = '/^([a-z][a-z0-9\+\-\.]+[:])/i';
18
19
    /***********************************************************************************
20
     *  NOTE:  Custom assertions may be added below this line.                         *
21
     *         They SHOULD be marked as `private` to ensure the call is forced         *
22
     *          through __callStatic().                                                *
23
     *         Assertions marked `public` are called directly and will                 *
24
     *          not handle any custom exception passed to it.                          *
25
     ***********************************************************************************/
26
27
28
    /**
29
     * @param string $value
30
     * @param string $message
31
     */
32
    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

32
    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...
33
    {
34
        try {
35
            BaseAssert::validDateTime($value);
36
        } catch (AssertionFailedException $e) {
37
            throw new SchemaViolationException($e->getMessage());
38
        }
39
40
        try {
41
            BaseAssert::endsWith(
42
                $value,
43
                'Z',
44
                '%s is not a DateTime expressed in the UTC timezone using the \'Z\' timezone identifier.',
45
            );
46
        } catch (AssertionFailedException $e) {
47
            throw new ProtocolViolationException($e->getMessage());
48
        }
49
    }
50
51
52
    /**
53
     * @param string $value
54
     * @param string $message
55
     */
56
    private static function validURI(string $value, string $message = ''): void
57
    {
58
        try {
59
            BaseAssert::validURI($value);
60
        } catch (AssertionFailedException $e) {
61
            throw new SchemaViolationException($e->getMessage());
62
        }
63
64
        try {
65
            BaseAssert::notWhitespaceOnly($value, $message ?: '\'%s\' is not a SAML2-compliant URI');
66
67
            // If it doesn't have a scheme, it's not an absolute URI
68
            BaseAssert::regex($value, self::SCHEME_REGEX, $message ?: '\'%s\' is not a SAML2-compliant URI');
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\SAML2\Assert\...tionTrait::SCHEME_REGEX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
69
        } catch (AssertionFailedException $e) {
70
            throw new ProtocolViolationException($e->getMessage());
71
        }
72
    }
73
}
74