Complex classes like HttpStatus often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HttpStatus, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class HttpStatus |
||
10 | { |
||
11 | const CODE_CONTINUE = 100; |
||
12 | const CODE_SWITCHING_PROTOCOLS = 101; |
||
13 | const CODE_OK = 200; |
||
14 | const CODE_CREATED = 201; |
||
15 | const CODE_ACCEPTED = 202; |
||
16 | const CODE_NON_AUTHRATIVE_INFORMATION = 203; |
||
17 | const CODE_NOCONTENT = 204; |
||
18 | const CODE_RESET_CONTENT = 205; |
||
19 | const CODE_PARTIAL_CONTENT = 206; |
||
20 | const CODE_MULTIPLE_CHOICE = 300; |
||
21 | const CODE_MOVED_PERMANENTLY = 301; |
||
22 | const CODE_FOUND = 302; |
||
23 | const CODE_SEE_OTHER = 303; |
||
24 | const CODE_NOT_MODIFIED = 304; |
||
25 | const CODE_USE_PROXY = 305; |
||
26 | const CODE_UNUSED = 306; |
||
27 | const CODE_TEMP_REDIRECT = 307; |
||
28 | const CODE_BAD_REQUEST = 400; |
||
29 | const CODE_UNAUTHORIZED = 401; |
||
30 | const CODE_PAYMENT_REQ = 402; |
||
31 | const CODE_FORBIDDEN = 403; |
||
32 | const CODE_NOT_FOUND = 404; |
||
33 | const CODE_METHOD_NOT_ALLOWED = 405; |
||
34 | const CODE_NOT_ACCEPTABLE = 406; |
||
35 | const CODE_PROXY_AUTHENTICATION_REQUIRED = 407; |
||
36 | const CODE_REQUEST_TIMEOUT = 408; |
||
37 | const CODE_CONFLICT = 409; |
||
38 | const CODE_GONE = 410; |
||
39 | const CODE_LENGTH_REQUIRED = 411; |
||
40 | const CODE_PRECONDITION_FAILED = 412; |
||
41 | const CODE_REQUEST_ENTITY_TOOLONG = 413; |
||
42 | const CODE_REQUEST_URI_TOOLONG = 414; |
||
43 | const CODE_UNSUPPORTED_MEDIATYPE = 415; |
||
44 | const CODE_REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
||
45 | const CODE_EXPECTATION_FAILED = 417; |
||
46 | const CODE_INTERNAL_SERVER_ERROR = 500; |
||
47 | const CODE_NOT_IMPLEMENTED = 501; |
||
48 | const CODE_BAD_GATEWAY = 502; |
||
49 | const CODE_SERVICE_UNAVAILABLE = 503; |
||
50 | const CODE_GATEWAY_TIMEOUT = 504; |
||
51 | const CODE_HTTP_VERSION_NOT_SUPPORTED = 505; |
||
52 | |||
53 | /** |
||
54 | * Get status description from status code. |
||
55 | * |
||
56 | * @param int $statusCode status code |
||
57 | * |
||
58 | * @return string|null |
||
59 | */ |
||
60 | public static function getStatusDescription($statusCode) |
||
149 | } |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.