Conditions | 31 |
Paths | > 20000 |
Total Lines | 134 |
Code Lines | 96 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
38 | public function collect(Request $request, Response $response, \Exception $exception = null) |
||
39 | { |
||
40 | $responseHeaders = $response->headers->all(); |
||
41 | foreach ($response->headers->getCookies() as $cookie) { |
||
42 | $responseHeaders['set-cookie'][] = (string) $cookie; |
||
43 | } |
||
44 | |||
45 | // attributes are serialized and as they can be anything, they need to be converted to strings. |
||
46 | $attributes = array(); |
||
47 | foreach ($request->attributes->all() as $key => $value) { |
||
48 | if ('_route' === $key && is_object($value)) { |
||
49 | $attributes[$key] = $this->varToString($value->getPath()); |
||
50 | } elseif ('_route_params' === $key) { |
||
51 | // we need to keep route params as an array (see getRouteParams()) |
||
52 | foreach ($value as $k => $v) { |
||
53 | $value[$k] = $this->varToString($v); |
||
54 | } |
||
55 | $attributes[$key] = $value; |
||
56 | } else { |
||
57 | $attributes[$key] = $this->varToString($value); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | $content = null; |
||
|
|||
62 | try { |
||
63 | $content = $request->getContent(); |
||
64 | } catch (\LogicException $e) { |
||
65 | // the user already got the request content as a resource |
||
66 | $content = false; |
||
67 | } |
||
68 | |||
69 | $sessionMetadata = array(); |
||
70 | $sessionAttributes = array(); |
||
71 | $flashes = array(); |
||
72 | if ($request->hasSession()) { |
||
73 | $session = $request->getSession(); |
||
74 | if ($session->isStarted()) { |
||
75 | $sessionMetadata['Created'] = date(DATE_RFC822, $session->getMetadataBag()->getCreated()); |
||
76 | $sessionMetadata['Last used'] = date(DATE_RFC822, $session->getMetadataBag()->getLastUsed()); |
||
77 | $sessionMetadata['Lifetime'] = $session->getMetadataBag()->getLifetime(); |
||
78 | $sessionAttributes = $session->all(); |
||
79 | $flashes = $session->getFlashBag()->peekAll(); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | $statusCode = $response->getStatusCode(); |
||
84 | |||
85 | $this->data = array( |
||
86 | 'format' => $request->getRequestFormat(), |
||
87 | 'content' => $content, |
||
88 | 'content_type' => $response->headers->get('Content-Type', 'text/html'), |
||
89 | 'status_text' => isset(Response::$statusTexts[$statusCode]) ? Response::$statusTexts[$statusCode] : '', |
||
90 | 'status_code' => $statusCode, |
||
91 | 'request_query' => $request->query->all(), |
||
92 | 'request_request' => $request->request->all(), |
||
93 | 'request_headers' => $request->headers->all(), |
||
94 | 'request_server' => $request->server->all(), |
||
95 | 'request_cookies' => $request->cookies->all(), |
||
96 | 'request_attributes' => $attributes, |
||
97 | 'response_headers' => $responseHeaders, |
||
98 | 'session_metadata' => $sessionMetadata, |
||
99 | 'session_attributes' => $sessionAttributes, |
||
100 | 'flashes' => $flashes, |
||
101 | 'path_info' => $request->getPathInfo(), |
||
102 | 'controller' => 'n/a', |
||
103 | 'locale' => $request->getLocale(), |
||
104 | ); |
||
105 | |||
106 | if (isset($this->data['request_headers']['php-auth-pw'])) { |
||
107 | $this->data['request_headers']['php-auth-pw'] = '******'; |
||
108 | } |
||
109 | |||
110 | if (isset($this->data['request_server']['PHP_AUTH_PW'])) { |
||
111 | $this->data['request_server']['PHP_AUTH_PW'] = '******'; |
||
112 | } |
||
113 | |||
114 | if (isset($this->data['request_request']['_password'])) { |
||
115 | $this->data['request_request']['_password'] = '******'; |
||
116 | } |
||
117 | |||
118 | foreach ($this->data as $key => $value) { |
||
119 | if (!is_array($value)) { |
||
120 | continue; |
||
121 | } |
||
122 | if ('request_headers' === $key || 'response_headers' === $key) { |
||
123 | $value = array_map(function ($v) { return isset($v[0]) && !isset($v[1]) ? $v[0] : $v; }, $value); |
||
124 | } |
||
125 | if ('request_server' !== $key && 'request_cookies' !== $key) { |
||
126 | $this->data[$key] = $value; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | if (isset($this->controllers[$request])) { |
||
131 | $controller = $this->controllers[$request]; |
||
132 | if (is_array($controller)) { |
||
133 | try { |
||
134 | $r = new \ReflectionMethod($controller[0], $controller[1]); |
||
135 | $this->data['controller'] = array( |
||
136 | 'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0], |
||
137 | 'method' => $controller[1], |
||
138 | 'file' => $r->getFileName(), |
||
139 | 'line' => $r->getStartLine(), |
||
140 | ); |
||
141 | } catch (\ReflectionException $e) { |
||
142 | if (is_callable($controller)) { |
||
143 | // using __call or __callStatic |
||
144 | $this->data['controller'] = array( |
||
145 | 'class' => is_object($controller[0]) ? get_class($controller[0]) : $controller[0], |
||
146 | 'method' => $controller[1], |
||
147 | 'file' => 'n/a', |
||
148 | 'line' => 'n/a', |
||
149 | ); |
||
150 | } |
||
151 | } |
||
152 | } elseif ($controller instanceof \Closure) { |
||
153 | $r = new \ReflectionFunction($controller); |
||
154 | $this->data['controller'] = array( |
||
155 | 'class' => $r->getName(), |
||
156 | 'method' => null, |
||
157 | 'file' => $r->getFileName(), |
||
158 | 'line' => $r->getStartLine(), |
||
159 | ); |
||
160 | } elseif (is_object($controller)) { |
||
161 | $r = new \ReflectionClass($controller); |
||
162 | $this->data['controller'] = array( |
||
163 | 'class' => $r->getName(), |
||
164 | 'method' => null, |
||
165 | 'file' => $r->getFileName(), |
||
166 | 'line' => $r->getStartLine(), |
||
167 | ); |
||
168 | } else { |
||
169 | $this->data['controller'] = (string) $controller ?: 'n/a'; |
||
170 | } |
||
171 | unset($this->controllers[$request]); |
||
172 | } |
||
312 |