GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

DatabaseQueryExecuted   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
eloc 18
c 3
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sanitize() 0 17 2
A handleEvent() 0 9 1
1
<?php
2
3
namespace Honeybadger\HoneybadgerLaravel\Breadcrumbs;
4
5
use Honeybadger\HoneybadgerLaravel\Facades\Honeybadger;
6
use Illuminate\Database\Connection;
7
use Illuminate\Database\Events\QueryExecuted;
8
9
class DatabaseQueryExecuted extends Breadcrumb
10
{
11
    public $handles = QueryExecuted::class;
12
13
    public function handleEvent(QueryExecuted $event)
14
    {
15
        $metadata = [
16
            'connectionName' => $event->connectionName,
17
            'sql' => $this->sanitize($event->sql, $event->connection),
18
            'duration' => number_format($event->time, 2, '.', '').'ms',
19
        ];
20
21
        Honeybadger::addBreadcrumb('Database query executed', $metadata, 'query');
22
    }
23
24
    /**
25
     * Even though Laravel gives us the sanitized query, let's err on the side of caution by removing any quoted data.
26
     */
27
    public function sanitize(string $sql, Connection $connection): string
28
    {
29
        $escapedQuotes = '#/(\\"|\\\')/#';
30
        $numericData = '#\b\d+\b#';
31
        $singleQuotedData = "#'(?:[^']|'')*'#";
32
        $newlines = '#\n#';
33
        $doubleQuotedData = '#"(?:[^"]|"")*"#';
34
35
        $sql = preg_replace($escapedQuotes, '', $sql);
36
        $sql = preg_replace([$numericData, $singleQuotedData, $newlines], '?', $sql);
37
38
        $doubleQuoters = ['pgsql', 'sqlite', 'postgis'];
39
        if (in_array($connection->getConfig('driver'), $doubleQuoters)) {
40
            $sql = preg_replace($doubleQuotedData, '?', $sql);
41
        }
42
43
        return $sql;
44
    }
45
}
46