Completed
Pull Request — dev (#11)
by
unknown
05:09
created

php-5.3-compat.php ➔ http_response_code()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 101
Code Lines 89

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 86
CRAP Score 6

Importance

Changes 3
Bugs 3 Features 0
Metric Value
cc 6
eloc 89
c 3
b 3
f 0
nc 6
nop 1
dl 0
loc 101
ccs 86
cts 86
cp 1
crap 6
rs 8.1463

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
namespace Vectorface\SnappyRouter;
4
5
use \Exception;
6
use Vectorface\SnappyRouter\Di\Di;
7
use Vectorface\SnappyRouter\Response\AbstractResponse;
8
9
function http_response_code($code = null)
0 ignored issues
show
Coding Style introduced by
http_response_code uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
10
{
11 12
    if (function_exists('\http_response_code')) {
12
        // @codeCoverageIgnoreStart
13
        return \http_response_code($code);
14
    }
15
    // @codeCoverageIgnoreEnd
16
17 12
    $diKey = 'currentResponseCode';
18 12
    if (null === $code) {
19
        try {
20 1
            return Di::getDefault()->get($diKey);
21 1
        } catch (Exception $e) {
22 1
            return Di::getDefault()->set($diKey, AbstractResponse::RESPONSE_OK)->get($diKey);
23
        }
24
    }
25
26
    $http_status_codes = array(
27 11
        100 => 'Continue',
28 11
        101 => 'Switching Protocols',
29 11
        102 => 'Processing',
30 11
        200 => 'OK',
31 11
        201 => 'Created',
32 11
        202 => 'Accepted',
33 11
        203 => 'Non-Authoritative Information',
34 11
        204 => 'No Content',
35 11
        205 => 'Reset Content',
36 11
        206 => 'Partial Content',
37 11
        207 => 'Multi-Status',
38 11
        300 => 'Multiple Choices',
39 11
        301 => 'Moved Permanently',
40 11
        302 => 'Found',
41 11
        303 => 'See Other',
42 11
        304 => 'Not Modified',
43 11
        305 => 'Use Proxy',
44 11
        306 => '(Unused)',
45 11
        307 => 'Temporary Redirect',
46 11
        308 => 'Permanent Redirect',
47 11
        400 => 'Bad Request',
48 11
        401 => 'Unauthorized',
49 11
        402 => 'Payment Required',
50 11
        403 => 'Forbidden',
51 11
        404 => 'Not Found',
52 11
        405 => 'Method Not Allowed',
53 11
        406 => 'Not Acceptable',
54 11
        407 => 'Proxy Authentication Required',
55 11
        408 => 'Request Timeout',
56 11
        409 => 'Conflict',
57 11
        410 => 'Gone',
58 11
        411 => 'Length Required',
59 11
        412 => 'Precondition Failed',
60 11
        413 => 'Request Entity Too Large',
61 11
        414 => 'Request-URI Too Long',
62 11
        415 => 'Unsupported Media Type',
63 11
        416 => 'Requested Range Not Satisfiable',
64 11
        417 => 'Expectation Failed',
65 11
        418 => 'I\'m a teapot',
66 11
        419 => 'Authentication Timeout',
67 11
        420 => 'Enhance Your Calm',
68 11
        422 => 'Unprocessable Entity',
69 11
        423 => 'Locked',
70 11
        424 => 'Failed Dependency',
71 11
        424 => 'Method Failure',
72 11
        425 => 'Unordered Collection',
73 11
        426 => 'Upgrade Required',
74 11
        428 => 'Precondition Required',
75 11
        429 => 'Too Many Requests',
76 11
        431 => 'Request Header Fields Too Large',
77 11
        444 => 'No Response',
78 11
        449 => 'Retry With',
79 11
        450 => 'Blocked by Windows Parental Controls',
80 11
        451 => 'Unavailable For Legal Reasons',
81 11
        494 => 'Request Header Too Large',
82 11
        495 => 'Cert Error',
83 11
        496 => 'No Cert',
84 11
        497 => 'HTTP to HTTPS',
85 11
        499 => 'Client Closed Request',
86 11
        500 => 'Internal Server Error',
87 11
        501 => 'Not Implemented',
88 11
        502 => 'Bad Gateway',
89 11
        503 => 'Service Unavailable',
90 11
        504 => 'Gateway Timeout',
91 11
        505 => 'HTTP Version Not Supported',
92 11
        506 => 'Variant Also Negotiates',
93 11
        507 => 'Insufficient Storage',
94 11
        508 => 'Loop Detected',
95 11
        509 => 'Bandwidth Limit Exceeded',
96 11
        510 => 'Not Extended',
97 11
        511 => 'Network Authentication Required',
98 11
        598 => 'Network read timeout error',
99
        599 => 'Network connect timeout error'
100 11
    );
101
102 11
    $code = intval($code);
103 11
    if (!isset($http_status_codes[$code])) {
104 1
        throw new Exception('Invalid response code: '.$code);
105
    }
106 10
    $protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
107 10
    @header(sprintf('%s %s %s', $protocol, $code, $http_status_codes[$code]));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
108 10
    Di::getDefault()->set($diKey, $code);
109
}
110