Conditions | 8 |
Paths | 25 |
Total Lines | 60 |
Code Lines | 34 |
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 declare(strict_types = 1); |
||
56 | public function query(RequestInterface $request): ResponseInterface |
||
57 | { |
||
58 | $method = $request->getMethod(); |
||
59 | |||
60 | $result = null; |
||
61 | $callback = null; |
||
62 | |||
63 | if ($method == 'GET' or $method == 'HEAD') { |
||
64 | list ($query, $path, $callback) = $this->parseRequest($request); |
||
65 | |||
66 | # TODO: detect conflicting parameters? |
||
67 | # if (isset($params['uri']) and isset($params['search'])) { |
||
68 | # $error = new Error(422, 'request_error', 'Conflicting request parameters uri & search'); |
||
69 | # } |
||
70 | |||
71 | try { |
||
72 | $result = $this->service->query($query, $path); |
||
73 | } catch(Error $e) { |
||
74 | $error = $e; |
||
75 | } |
||
76 | # TODO: catch other kinds of errors: |
||
77 | # } catch (\Exception $e) { |
||
78 | # $this->logger->error('Service Exception', ['exception' => $e]); |
||
79 | # $error = new Error(500, 'Internal server error'); |
||
80 | } elseif ($request->getMethod() == 'OPTIONS') { |
||
81 | return $this->optionsResponse(); |
||
82 | } else { |
||
83 | $error = new Error(405, 'Method not allowed'); |
||
84 | } |
||
85 | |||
86 | |||
87 | if ($result) { |
||
88 | $code = 200; |
||
89 | $headers = [ |
||
90 | 'Access-Control-Allow-Origin' => '*', |
||
91 | 'Content-Type' => 'application/json; charset=UTF-8', |
||
92 | 'X-Total-Count' => $result->getTotalCount() |
||
93 | ]; |
||
94 | $body = $result->json(); |
||
95 | } else { |
||
96 | $code = $error->code; |
||
97 | $headers = [ |
||
98 | 'Access-Control-Allow-Origin' => '*', |
||
99 | 'Content-Type' => 'application/json; charset=UTF-8', |
||
100 | ]; |
||
101 | $body = $error->json(); |
||
102 | } |
||
103 | |||
104 | $headers['Content-Length'] = strlen($body); |
||
105 | if ($method == 'HEAD') { |
||
106 | $body = ''; |
||
107 | } |
||
108 | |||
109 | if ($callback) { |
||
110 | $body = "/**/$callback($body);"; |
||
111 | $headers['Content-Type'] = 'application/javascript; charset=UTF-8'; |
||
112 | } |
||
113 | |||
114 | return $this->responseFactory->createResponse($code, null, $headers, $body); |
||
115 | } |
||
116 | |||
189 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: