These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace BeyondCode\QueryDetector\Outputs; |
||
4 | |||
5 | use Illuminate\Support\Collection; |
||
6 | use Symfony\Component\HttpFoundation\Response; |
||
7 | |||
8 | View Code Duplication | class Console implements Output |
|
0 ignored issues
–
show
|
|||
9 | { |
||
10 | public function output(Collection $detectedQueries, Response $response) |
||
11 | { |
||
12 | if (stripos($response->headers->get('Content-Type'), 'text/html') !== 0 || $response->isRedirection()) { |
||
13 | return; |
||
14 | } |
||
15 | |||
16 | $content = $response->getContent(); |
||
17 | |||
18 | $outputContent = $this->getOutputContent($detectedQueries); |
||
19 | |||
20 | $pos = strripos($content, '</body>'); |
||
21 | |||
22 | if (false !== $pos) { |
||
23 | $content = substr($content, 0, $pos) . $outputContent . substr($content, $pos); |
||
24 | } else { |
||
25 | $content = $content . $outputContent; |
||
26 | } |
||
27 | |||
28 | // Update the new content and reset the content length |
||
29 | $response->setContent($content); |
||
30 | |||
31 | $response->headers->remove('Content-Length'); |
||
32 | } |
||
33 | |||
34 | protected function getOutputContent(Collection $detectedQueries) |
||
35 | { |
||
36 | $output = '<script type="text/javascript">'; |
||
37 | $output .= "console.warn('Found the following N+1 queries in this request:\\n\\n"; |
||
38 | foreach ($detectedQueries as $detectedQuery) { |
||
39 | $output .= "Model: ".addslashes($detectedQuery['model']). " => Relation: ".addslashes($detectedQuery['relation']); |
||
40 | $output .= " - You should add \"with(\'".$detectedQuery['relation']."\')\" to eager-load this relation."; |
||
41 | $output .= "\\n"; |
||
42 | } |
||
43 | $output .= "')"; |
||
44 | $output .= '</script>'; |
||
45 | |||
46 | return $output; |
||
47 | } |
||
48 | } |
||
49 |
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.