StatusCode::asPhrase()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 63
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 65
rs 8.8072

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Http\Message\Enum;
15
16
use Valkyrja\Http\Message\Constant\StatusText;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Http\Message\Constant\StatusText 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...
17
18
/**
19
 * Enum Status.
20
 *
21
 * @author Melech Mizrachi
22
 *
23
 * @see    http://www.iana.org/assignments/http-status-codes/
24
 * - Hypertext Transfer Protocol (HTTP) Status Code Registry
25
 */
26
enum StatusCode: int
27
{
28
    case CONTINUE                        = 100;
29
    case SWITCHING_PROTOCOLS             = 101;
30
    case PROCESSING                      = 102;
31
    case EARLY_HINTS                     = 103;
32
    case OK                              = 200;
33
    case CREATED                         = 201;
34
    case ACCEPTED                        = 202;
35
    case NON_AUTHORITATIVE_INFORMATION   = 203;
36
    case NO_CONTENT                      = 204;
37
    case RESET_CONTENT                   = 205;
38
    case PARTIAL_CONTENT                 = 206;
39
    case MULTI_STATUS                    = 207;
40
    case ALREADY_REPORTED                = 208;
41
    case IM_USED                         = 226;
42
    case MULTIPLE_CHOICES                = 300;
43
    case MOVED_PERMANENTLY               = 301;
44
    case FOUND                           = 302;
45
    case SEE_OTHER                       = 303;
46
    case NOT_MODIFIED                    = 304;
47
    case USE_PROXY                       = 305;
48
    case TEMPORARY_REDIRECT              = 307;
49
    case PERMANENT_REDIRECT              = 308;
50
    case BAD_REQUEST                     = 400;
51
    case UNAUTHORIZED                    = 401;
52
    case PAYMENT_REQUIRED                = 402;
53
    case FORBIDDEN                       = 403;
54
    case NOT_FOUND                       = 404;
55
    case METHOD_NOT_ALLOWED              = 405;
56
    case NOT_ACCEPTABLE                  = 406;
57
    case PROXY_AUTHENTICATION_REQUIRED   = 407;
58
    case REQUEST_TIMEOUT                 = 408;
59
    case CONFLICT                        = 409;
60
    case GONE                            = 410;
61
    case LENGTH_REQUIRED                 = 411;
62
    case PRECONDITION_FAILED             = 412;
63
    case PAYLOAD_TOO_LARGE               = 413;
64
    case URI_TOO_LONG                    = 414;
65
    case UNSUPPORTED_MEDIA_TYPE          = 415;
66
    case RANGE_NOT_SATISFIABLE           = 416;
67
    case EXPECTATION_FAILED              = 417;
68
    case I_AM_A_TEAPOT                   = 418;
69
    case MISDIRECTED_REQUEST             = 421;
70
    case UNPROCESSABLE_ENTITY            = 422;
71
    case LOCKED                          = 423;
72
    case FAILED_DEPENDENCY               = 424;
73
    case TOO_EARLY                       = 425;
74
    case UPGRADE_REQUIRED                = 426;
75
    case PRECONDITION_REQUIRED           = 428;
76
    case TOO_MANY_REQUESTS               = 429;
77
    case REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
78
    case UNAVAILABLE_FOR_LEGAL_REASONS   = 451;
79
    case INTERNAL_SERVER_ERROR           = 500;
80
    case NOT_IMPLEMENTED                 = 501;
81
    case BAD_GATEWAY                     = 502;
82
    case SERVICE_UNAVAILABLE             = 503;
83
    case GATEWAY_TIMEOUT                 = 504;
84
    case HTTP_VERSION_NOT_SUPPORTED      = 505;
85
    case VARIANT_ALSO_NEGOTIATES         = 506;
86
    case INSUFFICIENT_STORAGE            = 507;
87
    case LOOP_DETECTED                   = 508;
88
    case NOT_EXTENDED_OBSOLETED          = 510;
89
    case NETWORK_AUTHENTICATION_REQUIRED = 511;
90
91
    /**
92
     * Get the code representation of the status.
93
     *
94
     * @return int
95
     */
96
    public function code(): int
97
    {
98
        return $this->value;
99
    }
100
101
    /**
102
     * Check if this is a valid redirect.
103
     *
104
     * @return bool
105
     */
106
    public function isRedirect(): bool
107
    {
108
        return $this->value >= self::MULTIPLE_CHOICES->value && $this->value < self::BAD_REQUEST->value;
109
    }
110
111
    /**
112
     * Check if this is an error code.
113
     *
114
     * @return bool
115
     */
116
    public function isError(): bool
117
    {
118
        return $this->value >= self::INTERNAL_SERVER_ERROR->value;
119
    }
120
121
    /**
122
     * Get the phrase representation of the status.
123
     *
124
     * @return string
125
     */
126
    public function asPhrase(): string
127
    {
128
        return match ($this) {
129
            self::CONTINUE                        => StatusText::CONTINUE,
130
            self::SWITCHING_PROTOCOLS             => StatusText::SWITCHING_PROTOCOLS,
131
            self::PROCESSING                      => StatusText::PROCESSING,
132
            self::EARLY_HINTS                     => StatusText::EARLY_HINTS,
133
            self::OK                              => StatusText::OK,
134
            self::CREATED                         => StatusText::CREATED,
135
            self::ACCEPTED                        => StatusText::ACCEPTED,
136
            self::NON_AUTHORITATIVE_INFORMATION   => StatusText::NON_AUTHORITATIVE_INFORMATION,
137
            self::NO_CONTENT                      => StatusText::NO_CONTENT,
138
            self::RESET_CONTENT                   => StatusText::RESET_CONTENT,
139
            self::PARTIAL_CONTENT                 => StatusText::PARTIAL_CONTENT,
140
            self::MULTI_STATUS                    => StatusText::MULTI_STATUS,
141
            self::ALREADY_REPORTED                => StatusText::ALREADY_REPORTED,
142
            self::IM_USED                         => StatusText::IM_USED,
143
            self::MULTIPLE_CHOICES                => StatusText::MULTIPLE_CHOICES,
144
            self::MOVED_PERMANENTLY               => StatusText::MOVED_PERMANENTLY,
145
            self::FOUND                           => StatusText::FOUND,
146
            self::SEE_OTHER                       => StatusText::SEE_OTHER,
147
            self::NOT_MODIFIED                    => StatusText::NOT_MODIFIED,
148
            self::USE_PROXY                       => StatusText::USE_PROXY,
149
            self::TEMPORARY_REDIRECT              => StatusText::TEMPORARY_REDIRECT,
150
            self::PERMANENT_REDIRECT              => StatusText::PERMANENT_REDIRECT,
151
            self::BAD_REQUEST                     => StatusText::BAD_REQUEST,
152
            self::UNAUTHORIZED                    => StatusText::UNAUTHORIZED,
153
            self::PAYMENT_REQUIRED                => StatusText::PAYMENT_REQUIRED,
154
            self::FORBIDDEN                       => StatusText::FORBIDDEN,
155
            self::NOT_FOUND                       => StatusText::NOT_FOUND,
156
            self::METHOD_NOT_ALLOWED              => StatusText::METHOD_NOT_ALLOWED,
157
            self::NOT_ACCEPTABLE                  => StatusText::NOT_ACCEPTABLE,
158
            self::PROXY_AUTHENTICATION_REQUIRED   => StatusText::PROXY_AUTHENTICATION_REQUIRED,
159
            self::REQUEST_TIMEOUT                 => StatusText::REQUEST_TIMEOUT,
160
            self::CONFLICT                        => StatusText::CONFLICT,
161
            self::GONE                            => StatusText::GONE,
162
            self::LENGTH_REQUIRED                 => StatusText::LENGTH_REQUIRED,
163
            self::PRECONDITION_FAILED             => StatusText::PRECONDITION_FAILED,
164
            self::PAYLOAD_TOO_LARGE               => StatusText::PAYLOAD_TOO_LARGE,
165
            self::URI_TOO_LONG                    => StatusText::URI_TOO_LONG,
166
            self::UNSUPPORTED_MEDIA_TYPE          => StatusText::UNSUPPORTED_MEDIA_TYPE,
167
            self::RANGE_NOT_SATISFIABLE           => StatusText::RANGE_NOT_SATISFIABLE,
168
            self::EXPECTATION_FAILED              => StatusText::EXPECTATION_FAILED,
169
            self::I_AM_A_TEAPOT                   => StatusText::I_AM_A_TEAPOT,
170
            self::MISDIRECTED_REQUEST             => StatusText::MISDIRECTED_REQUEST,
171
            self::UNPROCESSABLE_ENTITY            => StatusText::UNPROCESSABLE_ENTITY,
172
            self::LOCKED                          => StatusText::LOCKED,
173
            self::FAILED_DEPENDENCY               => StatusText::FAILED_DEPENDENCY,
174
            self::TOO_EARLY                       => StatusText::TOO_EARLY,
175
            self::UPGRADE_REQUIRED                => StatusText::UPGRADE_REQUIRED,
176
            self::PRECONDITION_REQUIRED           => StatusText::PRECONDITION_REQUIRED,
177
            self::TOO_MANY_REQUESTS               => StatusText::TOO_MANY_REQUESTS,
178
            self::REQUEST_HEADER_FIELDS_TOO_LARGE => StatusText::REQUEST_HEADER_FIELDS_TOO_LARGE,
179
            self::UNAVAILABLE_FOR_LEGAL_REASONS   => StatusText::UNAVAILABLE_FOR_LEGAL_REASONS,
180
            self::INTERNAL_SERVER_ERROR           => StatusText::INTERNAL_SERVER_ERROR,
181
            self::NOT_IMPLEMENTED                 => StatusText::NOT_IMPLEMENTED,
182
            self::BAD_GATEWAY                     => StatusText::BAD_GATEWAY,
183
            self::SERVICE_UNAVAILABLE             => StatusText::SERVICE_UNAVAILABLE,
184
            self::GATEWAY_TIMEOUT                 => StatusText::GATEWAY_TIMEOUT,
185
            self::HTTP_VERSION_NOT_SUPPORTED      => StatusText::HTTP_VERSION_NOT_SUPPORTED,
186
            self::VARIANT_ALSO_NEGOTIATES         => StatusText::VARIANT_ALSO_NEGOTIATES,
187
            self::INSUFFICIENT_STORAGE            => StatusText::INSUFFICIENT_STORAGE,
188
            self::LOOP_DETECTED                   => StatusText::LOOP_DETECTED,
189
            self::NOT_EXTENDED_OBSOLETED          => StatusText::NOT_EXTENDED_OBSOLETED,
190
            self::NETWORK_AUTHENTICATION_REQUIRED => StatusText::NETWORK_AUTHENTICATION_REQUIRED,
191
        };
192
    }
193
}
194