| Conditions | 7 |
| Paths | 8 |
| Total Lines | 63 |
| Code Lines | 40 |
| Lines | 22 |
| Ratio | 34.92 % |
| 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 |
||
| 125 | public function recordUsage(Request $request, $tool, $project, $token) |
||
| 126 | { |
||
| 127 | // Validate method and token. |
||
| 128 | if ($request->getMethod() !== 'PUT' || !$this->isCsrfTokenValid('intention', $token)) { |
||
| 129 | throw $this->createAccessDeniedException('This endpoint is for internal use only.'); |
||
| 130 | } |
||
| 131 | |||
| 132 | // Ready the response object. |
||
| 133 | $response = new Response(); |
||
| 134 | $response->headers->set('Content-Type', 'application/json'); |
||
| 135 | |||
| 136 | // Don't update counts for tools that aren't enabled |
||
| 137 | if (!$this->container->getParameter("enable.$tool")) { |
||
| 138 | $response->setStatusCode(Response::HTTP_FORBIDDEN); |
||
| 139 | $response->setContent(json_encode([ |
||
| 140 | 'error' => 'This tool is disabled' |
||
| 141 | ])); |
||
| 142 | return $response; |
||
| 143 | } |
||
| 144 | |||
| 145 | $conn = $this->container->get('doctrine')->getManager('default')->getConnection(); |
||
| 146 | $date = date('Y-m-d'); |
||
| 147 | |||
| 148 | // Increment count in timeline |
||
| 149 | $existsSql = "SELECT 1 FROM usage_timeline |
||
| 150 | WHERE date = '$date' |
||
| 151 | AND tool = '$tool'"; |
||
| 152 | |||
| 153 | View Code Duplication | if (count($conn->query($existsSql)->fetchAll()) === 0) { |
|
|
|
|||
| 154 | $createSql = "INSERT INTO usage_timeline |
||
| 155 | VALUES(NULL, '$date', '$tool', 1)"; |
||
| 156 | $conn->query($createSql); |
||
| 157 | } else { |
||
| 158 | $updateSql = "UPDATE usage_timeline |
||
| 159 | SET count = count + 1 |
||
| 160 | WHERE tool = '$tool' |
||
| 161 | AND date = '$date'"; |
||
| 162 | $conn->query($updateSql); |
||
| 163 | } |
||
| 164 | |||
| 165 | // Update per-project usage, if applicable |
||
| 166 | if (!$this->container->getParameter('app.single_wiki')) { |
||
| 167 | $existsSql = "SELECT 1 FROM usage_projects |
||
| 168 | WHERE tool = '$tool' |
||
| 169 | AND project = '$project'"; |
||
| 170 | |||
| 171 | View Code Duplication | if (count($conn->query($existsSql)->fetchAll()) === 0) { |
|
| 172 | $createSql = "INSERT INTO usage_projects |
||
| 173 | VALUES(NULL, '$tool', '$project', 1)"; |
||
| 174 | $conn->query($createSql); |
||
| 175 | } else { |
||
| 176 | $updateSql = "UPDATE usage_projects |
||
| 177 | SET count = count + 1 |
||
| 178 | WHERE tool = '$tool' |
||
| 179 | AND project = '$project'"; |
||
| 180 | $conn->query($updateSql); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | $response->setStatusCode(Response::HTTP_NO_CONTENT); |
||
| 185 | $response->setContent(json_encode([])); |
||
| 186 | return $response; |
||
| 187 | } |
||
| 188 | } |
||
| 189 |
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.