Passed
Pull Request — master (#285)
by Tim
02:36
created

AbstractProtocolException::getSubStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Exception\Protocol;
6
7
use RuntimeException;
8
9
/**
10
 * Class for representing a SAML 2 error.
11
 *
12
 * @package simplesaml\saml2
13
 */
14
abstract class AbstractProtocolException extends RuntimeException
15
{
16
    /**
17
     * The top-level status code.
18
     *
19
     * @var string
20
     */
21
    protected string $status;
22
23
    /**
24
     * The second-level status code, or NULL if no second-level status code is defined.
25
     *
26
     * @var string|null
27
     */
28
    protected ?string $subStatus;
29
30
    /**
31
     * The status message, or NULL if no status message is defined.
32
     *
33
     * @var string|null
34
     */
35
    protected ?string $statusMessage;
36
37
38
    /**
39
     * Create a SAML 2 error.
40
     *
41
     * @param string $status  The top-level status code.
42
     * @param string|null $subStatus  The second-level status code.
43
     *   Can be NULL, in which case there is no second-level status code.
44
     * @param string|null $statusMessage  The status message.
45
     *   Can be NULL, in which case there is no status message.
46
     */
47
    public function __construct(
48
        string $status,
49
        ?string $subStatus = null,
50
        ?string $statusMessage = null
51
    ) {
52
        $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

52
        /** @scrutinizer ignore-call */ 
53
        $st = self::shortStatus($status);
Loading history...
53
        if ($subStatus !== null) {
54
            $st .= '/' . $this->shortStatus($subStatus);
55
        }
56
        if ($statusMessage !== null) {
57
            $st .= ': ' . $statusMessage;
58
        }
59
        parent::__construct($st);
60
61
        $this->status = $status;
62
        $this->subStatus = $subStatus;
63
        $this->statusMessage = $statusMessage;
64
    }
65
66
67
    /**
68
     * Get the top-level status code.
69
     *
70
     * @return string  The top-level status code.
71
     */
72
    public function getStatus(): string
73
    {
74
        return $this->status;
75
    }
76
77
78
    /**
79
     * Get the second-level status code.
80
     *
81
     * @return string|null  The second-level status code or NULL if no second-level status code is present.
82
     */
83
    public function getSubStatus(): ?string
84
    {
85
        return $this->subStatus;
86
    }
87
88
89
    /**
90
     * Get the status message.
91
     *
92
     * @return string|null  The status message or NULL if no status message is present.
93
     */
94
    public function getStatusMessage(): ?string
95
    {
96
        return $this->statusMessage;
97
    }
98
99
100
    /**
101
     * Create a short version of the status code.
102
     *
103
     * Remove the 'urn:oasis:names:tc:SAML:2.0:status:'-prefix of status codes
104
     *   if it is present.
105
     *
106
     * @param string $status  The status code.
107
     * @return string  A shorter version of the status code.
108
     */
109
    private function shortStatus(string $status): string
110
    {
111
        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...
112
    }
113
}
114