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::handleEvent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 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