Passed
Pull Request — master (#285)
by Tim
05:22 queued 03:08
created

AbstractProtocolException::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
c 2
b 0
f 0
nc 4
nop 3
dl 0
loc 29
rs 9.7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Exception\Protocol;
6
7
use RuntimeException;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
12
/**
13
 * Class for representing a SAML 2 error.
14
 *
15
 * @package simplesaml\saml2
16
 */
17
abstract class AbstractProtocolException extends RuntimeException
18
{
19
    /**
20
     * The top-level status code.
21
     *
22
     * @var string
23
     */
24
    protected string $status;
25
26
    /**
27
     * The second-level status code, or NULL if no second-level status code is defined.
28
     *
29
     * @var string|null
30
     */
31
    protected ?string $subStatus;
32
33
    /**
34
     * The status message, or NULL if no status message is defined.
35
     *
36
     * @var string|null
37
     */
38
    protected ?string $statusMessage;
39
40
41
    /**
42
     * Create a SAML 2 error.
43
     *
44
     * @param string $status  The top-level status code.
45
     * @param string|null $subStatus  The second-level status code.
46
     *   Can be NULL, in which case there is no second-level status code.
47
     * @param string|null $statusMessage  The status message.
48
     *   Can be NULL, in which case there is no status message.
49
     */
50
    public function __construct(
51
        string $status,
52
        ?string $subStatus = null,
53
        ?string $statusMessage = null
54
    ) {
55
        Assert::oneOf(
56
            $status,
57
            [
58
                C::STATUS_SUCCESS,
59
                C::STATUS_REQUESTER,
60
                C::STATUS_RESPONDER,
61
                C::STATUS_VERSION_MISMATCH,
62
            ],
63
            'Invalid top-level status code',
64
            ProtocolViolationException::class
65
        );
66
67
        $st = self::shortStatus($status);
0 ignored issues
show
Bug Best Practice introduced by
The method SimpleSAML\SAML2\Excepti...xception::shortStatus() is not static, but was called statically. ( Ignorable by Annotation )

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

67
        /** @scrutinizer ignore-call */ 
68
        $st = self::shortStatus($status);
Loading history...
68
        if ($subStatus !== null) {
69
            $st .= '/' . $this->shortStatus($subStatus);
70
        }
71
        if ($statusMessage !== null) {
72
            $st .= ': ' . $statusMessage;
73
        }
74
        parent::__construct($st);
75
76
        $this->status = $status;
77
        $this->subStatus = $subStatus;
78
        $this->statusMessage = $statusMessage;
79
    }
80
81
82
    /**
83
     * Get the top-level status code.
84
     *
85
     * @return string  The top-level status code.
86
     */
87
    public function getStatus(): string
88
    {
89
        return $this->status;
90
    }
91
92
93
    /**
94
     * Get the second-level status code.
95
     *
96
     * @return string|null  The second-level status code or NULL if no second-level status code is present.
97
     */
98
    public function getSubStatus(): ?string
99
    {
100
        return $this->subStatus;
101
    }
102
103
104
    /**
105
     * Get the status message.
106
     *
107
     * @return string|null  The status message or NULL if no status message is present.
108
     */
109
    public function getStatusMessage(): ?string
110
    {
111
        return $this->statusMessage;
112
    }
113
114
115
    /**
116
     * Create a short version of the status code.
117
     *
118
     * Remove the 'urn:oasis:names:tc:SAML:2.0:status:'-prefix of status codes
119
     *   if it is present.
120
     *
121
     * @param string $status  The status code.
122
     * @return string  A shorter version of the status code.
123
     */
124
    private function shortStatus(string $status): string
125
    {
126
        return preg_filter(sprintf('/^%s/', Constants::STATUS_PREFIX), '', $status) ?? $status;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Exception\Protocol\Constants 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...
Bug Best Practice introduced by
The expression return preg_filter(sprin...'', $status) ?? $status could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
127
    }
128
}
129