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 |
||
9 | class IncomingIlluminateRequest implements IHTTPRequest |
||
10 | { |
||
11 | /** |
||
12 | * The Illuminate request. |
||
13 | * |
||
14 | * @var Request |
||
15 | */ |
||
16 | private $request; |
||
17 | |||
18 | /** |
||
19 | * The request headers. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | private $headers = []; |
||
24 | |||
25 | /** |
||
26 | * The incoming url in raw format. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | private $rawUrl = null; |
||
31 | |||
32 | /** |
||
33 | * The request method (GET, POST, PUT, DELETE or MERGE). |
||
34 | * |
||
35 | * @var HTTPRequestMethod HttpVerb |
||
36 | */ |
||
37 | private $method; |
||
38 | |||
39 | /** |
||
40 | * The query options as key value. |
||
41 | * |
||
42 | * @var array(string, string); |
||
43 | */ |
||
44 | private $queryOptions = []; |
||
45 | |||
46 | /** |
||
47 | * A collection that represents mapping between query |
||
48 | * option and its count. |
||
49 | * |
||
50 | * @var array(string, int) |
||
51 | */ |
||
52 | private $queryOptionsCount = []; |
||
53 | |||
54 | /** |
||
55 | * IncomingIlluminateRequest constructor. |
||
56 | * |
||
57 | * @param Request $request |
||
58 | */ |
||
59 | View Code Duplication | public function __construct(Request $request) |
|
67 | |||
68 | /** |
||
69 | * @return string RequestURI called by User with the value of QueryString |
||
70 | */ |
||
71 | public function getRawUrl() |
||
77 | |||
78 | /** |
||
79 | * @param string $key The header name |
||
80 | * |
||
81 | * @return array|null|string |
||
82 | */ |
||
83 | public function getRequestHeader($key) |
||
93 | |||
94 | /** |
||
95 | * Returns the Query String Parameters (QSPs) as an array of KEY-VALUE pairs. If a QSP appears twice |
||
96 | * it will have two entries in this array. |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | public function getQueryParameters() |
||
101 | { |
||
102 | //TODO: the contract is more specific than this, it requires the name and values to be decoded |
||
103 | //not sure how to test that... |
||
104 | //Have to convert to the stranger format known to POData that deals with multiple query strings. |
||
105 | //this makes this request a bit non compliant as it doesn't expose duplicate keys, something POData will |
||
106 | //check for instead whatever parameter was last in the query string is set. IE |
||
107 | //odata.svc/?$format=xml&$format=json the format will be json |
||
108 | $this->queryOptions = []; |
||
109 | $this->queryOptionsCount = []; |
||
110 | |||
111 | foreach ($this->request->all() as $key => $value) { |
||
112 | $keyBitz = explode(';', $key); |
||
113 | $newKey = strtolower($keyBitz[count($keyBitz) - 1]); |
||
114 | $this->queryOptions[] = [$newKey => $value]; |
||
115 | if (!array_key_exists($key, $this->queryOptionsCount)) { |
||
116 | $this->queryOptionsCount[$newKey] = 0; |
||
117 | } |
||
118 | $this->queryOptionsCount[$newKey]++; |
||
119 | } |
||
120 | |||
121 | return $this->queryOptions; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return HTTPRequestMethod |
||
126 | */ |
||
127 | public function getMethod() |
||
131 | |||
132 | /** |
||
133 | * @return array|mixed |
||
134 | */ |
||
135 | public function getAllInput() |
||
140 | } |
||
141 |