Failed Conditions
Pull Request — master (#3390)
by Chris
12:30
created

DebugStack   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 76%

Importance

Changes 0
Metric Value
wmc 9
eloc 26
dl 0
loc 76
ccs 19
cts 25
cp 0.76
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findQuerySource() 0 8 3
A stopQuery() 0 7 2
A __construct() 0 3 1
A startQuery() 0 19 3
1
<?php
2
3
namespace Doctrine\DBAL\Logging;
4
5
use function microtime;
6
7
/**
8
 * Includes executed SQLs in a Debug Stack.
9
 */
10
class DebugStack implements SQLLogger
11
{
12
    const SOURCE_DISABLED = 0;
13
    const SOURCE_ENABLED = 1;
14
15
    /**
16
     * Executed SQL queries.
17
     *
18
     * @var mixed[][]
19
     */
20
    public $queries = [];
21
22
    /**
23
     * If Debug Stack is enabled (log queries) or not.
24
     *
25
     * @var bool
26
     */
27
    public $enabled = true;
28
29
    /** @var float|null */
30
    public $start = null;
31
32
    /** @var int */
33
    public $currentQuery = 0;
34
35
    private $includeSource;
36
37 7963
    public function __construct($includeSource = self::SOURCE_DISABLED)
38
    {
39 7963
        $this->includeSource = $includeSource;
40 7963
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 7479
    public function startQuery($sql, ?array $params = null, ?array $types = null)
46
    {
47 7479
        if (! $this->enabled) {
48 27
            return;
49
        }
50
        
51 7452
        $this->start = microtime(true);
52
        $log = [
53 7452
            'sql' => $sql,
54 7452
            'params' => $params,
55 7452
            'types' => $types,
56 7452
            'executionMS' => 0,
57
        ];
58
59 7452
        if ($this->includeSource !== self::SOURCE_DISABLED) {
60
            $log['querySource'] = $this->findQuerySource();
61
        }
62
63 7452
        $this->queries[++$this->currentQuery] = $log;
64 7452
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 7307
    public function stopQuery()
70
    {
71 7307
        if (! $this->enabled) {
72 27
            return;
73
        }
74
75 7280
        $this->queries[$this->currentQuery]['executionMS'] = microtime(true) - $this->start;
76 7280
    }
77
78
    private function findQuerySource()
79
    {
80
        foreach (debug_backtrace() as $row) {
81
            if (stripos($row['file'], 'vendor') === false) {
82
                return $row;
83
            }
84
        }
85
        return ['file' => '*unknown*', 'line' => 0];
86
    }
87
}
88