Completed
Push — 4.0 ( 1bebd1...1533b9 )
by Marco
14:16
created

Processor   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 165
Duplicated Lines 12.73 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
c 1
b 0
f 0
lcom 1
cbo 6
dl 21
loc 165
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
D compose() 21 142 19
A parse() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Comodojo\Dispatcher\Output;
2
3
use \Comodojo\Components\Model as DispatcherClassModel;
4
use \Comodojo\Dispatcher\Response\Model as Response;
5
6
/**
7
 * Header manipulation class for dispatcher
8
 *
9
 * This class manage response headers, from the status code to the custom
10
 * header the service will return.
11
 *
12
 * @package     Comodojo dispatcher
13
 * @author      Marco Giovinazzi <[email protected]>
14
 * @license     GPL-3.0+
15
 *
16
 * LICENSE:
17
 *
18
 * This program is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License as
20
 * published by the Free Software Foundation, either version 3 of the
21
 * License, or (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License
29
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30
 */
31
32
// Missing statuses:
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
//      // Informational 1xx
34
//      100 => "Continue",
35
//      101 => "Switching Protocols",
36
//      // Successful 2xx
37
//      203 => "Non-Authoritative Information",
38
//      205 => "Reset Content",
39
//      206 => "Partial Content",
40
//      // Redirection 3xx
41
//      300 => "Multiple Choices",
42
//      305 => "Use Proxy",
43
//      // Client Error 4xx
44
//      401 => "Unauthorized",
45
//      402 => "Payment Required",
46
//      406 => "Not Acceptable",
47
//      407 => "Proxy Authentication Required",
48
//      408 => "Request Timeout",
49
//      409 => "Conflict",
50
//      410 => "Gone",
51
//      411 => "Length Required",
52
//      412 => "Precondition Failed",
53
//      413 => "Request Entity Too Large",
54
//      414 => "Request-URI Too Long",
55
//      415 => "Unsupported Media Type",
56
//      416 => "Requested Range Not Satisfiable",
57
//      417 => "Expectation Failed",
58
//      // Server Error 5xx
59
//      502 => "Bad Gateway",
60
//      504 => "Gateway Timeout",
61
//      505 => "HTTP Version Not Supported"
62
63
class Processor extends DispatcherClassModel {
64
65
    private $response;
66
    
67
    public function __construct(Configuration $configuration, Logger $logger, Response $response) {
68
69
        parent::__construct($configuration, $logger);
70
71
        $this->response = $response;
72
73
    }
74
75
    public function compose() {
0 ignored issues
show
Coding Style introduced by
compose 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...
76
77
        $status = $this->response->status()->get();
78
79
        $content = $this->response->content();
80
81
        $cookies = $this->response->cookies();
82
83
        $headers = $this->response->headers();
84
85
        $location = $this->response->location();
86
87
        // return value, just in case...
88
        $return = $content->get();
0 ignored issues
show
Unused Code introduced by
$return is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
90
        switch ($status) {
91
92
            case 200: //OK
93
94
                header('Content-Length: '.$content->length());
95
96
                break;
97
98 View Code Duplication
            case 202: //Accepted
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
100
                //PLEASE NOTE: according to HTTP/1.1, 202 header SHOULD HAVE status description in body... just in case
101
                header($_SERVER["SERVER_PROTOCOL"].' 202 Accepted');
102
                header('Status: 202 Accepted');
103
                header('Content-Length: '.$content->length());
104
105
                break;
106
107
            case 204: //OK - No Content
108
109
                header($_SERVER["SERVER_PROTOCOL"].' 204 No Content');
110
                header('Status: 204 No Content');
111
                header('Content-Length: 0',true);
112
113
                $return = null;
0 ignored issues
show
Unused Code introduced by
$return is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
114
115
                break;
116
117
            case 201: //Created
118
            case 301: //Moved Permanent
119
            case 302: //Found
120
            case 303: //See Other
121
            case 307: //Temporary Redirect
122
123
                header("Location: ".$location->get(),true,$status);
124
125
                break;
126
127
            case 304: //Not Modified
128
129
                $last_modified = $headers->get('Last-Modified');
130
131
                if ( is_null($last_modified) ) {
132
133
                    header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified');
134
135
                } else if ( is_int($last_modified) ) {
136
137
                    header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified).' GMT', true, 304);
138
139
                } else {
140
141
                    header('Last-Modified: '.$last_modified, true, 304);
142
143
                }
144
145
                header('Content-Length: '.$content->length());
146
                $headers->remove('Last-Modified');
147
148
                break;
149
150 View Code Duplication
            case 400: //Bad Request
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
152
                header($_SERVER["SERVER_PROTOCOL"].' 400 Bad Request', true, 400);
153
                header('Content-Length: '.$content->length());
154
155
                break;
156
157
            case 403:
158
159
                header('Origin not allowed', true, 403); //Not originated from allowed source
160
161
                break;
162
163 View Code Duplication
            case 404: //Not Found
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
165
                header($_SERVER["SERVER_PROTOCOL"].' 404 Not Found');
166
                header('Status: 404 Not Found');
167
                header('Content-Length: '.$content->length());
168
169
                break;
170
171
            case 405: //Not allowed
172
173
            header('Allow: ' . $value, true, 405);
0 ignored issues
show
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
174
175
            break;
176
177
            case 500: //Internal Server Error
178
179
                header('500 Internal Server Error', true, 500);
180
                header('Content-Length: '.$content->length());
181
182
                break;
183
184
            case 501: //Not implemented
185
186
            header('Allow: ' . $value, true, 501);
187
188
            break;
189
190
            case 503: //Service Unavailable
191
192
                header($_SERVER["SERVER_PROTOCOL"].' 503 Service Temporarily Unavailable');
193
                header('Status: 503 Service Temporarily Unavailable');
194
195
                // if ( $value !== false AND @is_int($value) ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
196
                //     header('Retry-After: '.$value);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
197
                // }
198
199
                break;
200
201
            default:
202
203
                header($_SERVER["SERVER_PROTOCOL"].' '.$this->response->status(), true, $status);
204
                header('Content-Length: '.$content->length());
205
206
                break;
207
208
        }
209
210
        $headers->send();
211
212
        $cookies->save();
213
214
        return $content;
215
216
    }
217
218
    public static function parse(Configuration $configuration, Logger $logger, Response $response) {
219
220
        $processor = new Processor($configuration, $logger, $response);
221
222
        return $processor->compose();
223
224
    }
225
226
227
}
228