Logger   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 87
ccs 20
cts 26
cp 0.7692
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 2 1
A addProfile() 0 12 4
A pretty_format() 0 19 4
A get_log() 0 2 1
A __construct() 0 2 1
1
<?php
2
3
/**
4
 * YOURLS simple logger
5
 *
6
 * @since 1.7.3
7
 */
8
9
namespace YOURLS\Admin;
10
11
use YOURLS\Database\YDB;
12
13
class Logger extends \Aura\Sql\Profiler {
14
15
    /**
16
     * the YDB instance
17
     * @var YDB
18
     */
19
    protected $ydb;
20
21
    /**
22
     * Debug log where messages are stored
23
     * @var array
24
     */
25
    protected $debug_log = array();
26
27
    public function __construct(YDB $ydb) {
28
        $this->ydb = $ydb;
29
        /**
0 ignored issues
show
Coding Style introduced by
Block comments must be started with /*
Loading history...
30
         * @todo Extend this to allow calling an external logger
31
         */
32
    }
33
34
    /**
35
     * @param string $message
36
     */
37 40
    public function log($message) {
38 40
        $this->debug_log[] = $message;
39 40
    }
40
41 1
    public function get_log() {
42 1
        return $this->debug_log;
43
    }
44
45
    /**
46
     * Extends \Aura\Sql\Profiler::addProfile() to log queries in our YOURLS logger
47
     *
48
     * @since  1.7.3
49
     * @param  float  $duration     The query duration.
50
     * @param  string $function     The PDO method that made the entry.
51
     * @param  string $statement    The SQL query statement.
52
     * @param  array  $bind_values  The values bound to the statement.
53
     * @return void
54
     */
55 36
    public function addProfile($duration, $function, $statement, array $bind_values = array() ) {
56 36
        parent::addProfile($duration, $function, $statement, $bind_values);
57
58 36
        if($function == 'connect') {
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
59
            $this->log( sprintf('SQL: CONNECT (%s s)', number_format($duration, 5)) );
60
            return;
61
        }
62
63
        // If we are emulating prepare, don't log 'prepare' statement, as they are not actual queries sent to the server
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 120 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
64
        // See YDB::get_queries() for details
65 36
        if ($this->ydb->get_emulate_state() && $function !== 'prepare') {
66 36
            $this->log( sprintf('SQL: %s (%s s)', $this->pretty_format($statement, $bind_values), number_format($duration, 5) ) );
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 130 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
67
        }
68 36
    }
69
70
    /**
71
     * Format PDO statement with bind/values replacement
72
     *
73
     * This replaces PDO binds such as 'key_name = :name' with corresponding array values, eg array('name'=>'some value')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
74
     * This is merely a cosmetic replacement to allow for readability: the result WILL NOT be valid SQL! (eg no proper quotes)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
75
     *
76
     * @since  1.7.3
77
     * @param  string $statement  SQL query with PDO style named placeholders
78
     * @param  array  $values     Optional array of values corresponding to placeholders
79
     * @return string             Readable SQL query with placeholders replaced
80
     */
81 36
    public function pretty_format($statement, array $values = array() ) {
82 36
        if (!$values) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $values of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
83 3
            return $statement;
84
        }
85
86 34
        return preg_replace_callback(
87 34
            '/:([^\s;)]*)/',
88
89
            /**
0 ignored issues
show
Coding Style introduced by
Block comments must be started with /*
Loading history...
90
             * @param string $matches
91
             */
92
            function ($matches) use ($values) {
93 34
                $replacement = isset( $values[$matches[1]] ) ? $values[$matches[1]] : '';
94 34
                if(is_array($replacement)) {
95
                    $replacement = implode(",", $replacement);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal , does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
96
                }
97 34
                return "'$replacement'";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $replacement instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
98 34
            },
99
            $statement
100
        );
101
    }
102
103
}
104