Logger::getParsedSql()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 10
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 10
rs 10
cc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the PHALCON-EXT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace PhalconExt\Db;
13
14
use Phalcon\Db\Adapter;
0 ignored issues
show
Bug introduced by
The type Phalcon\Db\Adapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Phalcon\Events\Event;
0 ignored issues
show
Bug introduced by
The type Phalcon\Events\Event was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use PhalconExt\Logger\LogsToFile;
17
18
/**
19
 * SQL logger implemented as db event listener.
20
 *
21
 * @author  Jitendra Adhikari <[email protected]>
22
 * @license MIT
23
 *
24
 * @link    https://github.com/adhocore/phalcon-ext
25
 */
26
class Logger
27
{
28
    use LogsToFile;
29
30
    /** @var int The current count of fired sqls */
31
    protected $index = -1;
32
33
    /** @var array */
34
    protected $config = [];
35
36
    /** @var string */
37
    protected $logFormat = "%message%;\n";
38
39
    /** @var string */
40
    protected $fileExtension = '.sql';
41
42
    public function __construct(array $config)
43
    {
44
        $this->config = $config + ['skipFirst' => 0, 'addHeader' => false, 'backtraceLevel' => 0];
45
46
        if (!empty($this->config['enabled']) && !empty($this->config['logPath'])) {
47
            $this->activate($this->config['logPath']);
48
        }
49
    }
50
51
    /**
52
     * Run before firing query.
53
     *
54
     * @param Event   $event
55
     * @param Adapter $connection
56
     *
57
     * @return void
58
     */
59
    public function beforeQuery(Event $event, Adapter $connection)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

59
    public function beforeQuery(/** @scrutinizer ignore-unused */ Event $event, Adapter $connection)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
        if (!$this->activated) {
62
            return;
63
        }
64
65
        if (++$this->index < $this->config['skipFirst']) {
66
            return;
67
        }
68
69
        $this->log($this->getHeader() . $this->getBacktrace() . $this->getParsedSql($connection));
70
    }
71
72
    /**
73
     * Get log header like request uri and datetime.
74
     *
75
     * @return string
76
     */
77
    protected function getHeader(): string
78
    {
79
        if ($this->index !== $this->config['skipFirst'] || !$this->config['addHeader']) {
80
            return '';
81
        }
82
83
        $header = '-- ' . ($_SERVER['REQUEST_URI'] ?? '') . \date(' [Y-m-d H:i:s]') . "\n";
84
85
        return $header
86
            . \str_pad('-- -', \strlen($header), '-', STR_PAD_RIGHT)
87
            . "\n";
88
    }
89
90
    /**
91
     * Get the backtrace of paths leading to logged query.
92
     *
93
     * @return string
94
     */
95
    protected function getBacktrace(): string
96
    {
97
        if ($this->config['backtraceLevel'] < 1) {
98
            return '';
99
        }
100
101
        $trace = '';
102
        foreach (\array_slice(\debug_backtrace(), 2, $this->config['backtraceLevel']) as $tr) {
103
            $tr += ['function' => 'n/a', 'line' => 0, 'file' => 'n/a'];
104
105
            $trace .= "  -- {$tr['file']}:{$tr['line']}#{$tr['function']}()\n";
106
        }
107
108
        return $trace;
109
    }
110
111
    /**
112
     * Get the properly interpolated sql.
113
     *
114
     * @param Adapter $connection
115
     *
116
     * @return string
117
     */
118
    protected function getParsedSql($connection): string
119
    {
120
        $parts = $connection->convertBoundParams(
121
            $connection->getSqlStatement(),
122
            $connection->getSQLVariables() ?: []
123
        );
124
125
        $binds = $parts['params'] ?: $connection->getSQLVariables();
126
127
        return \vsprintf(\str_replace('?', "'%s'", $parts['sql']), $binds);
128
    }
129
130
    /**
131
     * Run after firing query.
132
     *
133
     * @param Event   $event
134
     * @param Adapter $connection
135
     *
136
     * @return void
137
     */
138
    public function afterQuery(Event $event, $connection)
0 ignored issues
show
Unused Code introduced by
The parameter $connection is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

138
    public function afterQuery(Event $event, /** @scrutinizer ignore-unused */ $connection)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

138
    public function afterQuery(/** @scrutinizer ignore-unused */ Event $event, $connection)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140
        // Do nothing.
141
    }
142
}
143