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

CustomAssertionTrait::validURI()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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

34
    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...
35
    {
36
        try {
37
            BaseAssert::validDateTime($value, '\'%s\' is not a valid xs:dateTime');
38
        } catch (AssertionFailedException $e) {
39
            throw new SchemaValidationException(sprintf(
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Assert\SchemaValidationException 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...
40
                $e->getMessage(),
41
                $value,
42
            ));
43
        }
44
45
        try {
46
            BaseAssert::endsWith(
47
                $value,
48
                'Z',
49
                '\'%s\' is not a DateTime expressed in the UTC timezone using the \'Z\' timezone identifier.',
50
            );
51
        } catch (AssertionFailedException $e) {
52
            throw new ProtocolViolationException(sprintf(
53
                $e->getMessage(),
54
                $value,
55
            ));
56
        }
57
    }
58
59
60
    /**
61
     * @param string $value
62
     * @param string $message
63
     */
64
    private static function validURI(string $value, string $message = ''): void
65
    {
66
        try {
67
            BaseAssert::validURI($value, '\'%s\' is not a valid RFC3986 compliant URI');
68
        } catch (AssertionFailedException $e) {
69
            throw new SchemaViolationException(sprintf(
70
                $e->getMessage(),
71
                $value,
72
            ));
73
        }
74
75
        try {
76
            BaseAssert::notWhitespaceOnly($value, $message ?: '\'%s\' is not a SAML2-compliant URI');
77
78
            // If it doesn't have a scheme, it's not an absolute URI
79
            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...
80
        } catch (AssertionFailedException $e) {
81
            throw new ProtocolViolationException(sprintf(
82
                $e->getMessage(),
83
                $value,
84
            ));
85
        }
86
    }
87
}
88