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