Completed
Push — master ( af77e8...7d9d07 )
by Zack
11s
created

WP_Action_Logger   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 96.15%

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 25
cts 26
cp 0.9615
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
C log() 0 37 12
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * The \GV\WP_Action_Logger implementation.
11
 *
12
 * Uses the old logging stuff for now.
13
 */
14
class WP_Action_Logger extends Logger {
15
16
	/**
17
	 * Logs with an arbitrary level using `do_action` and our
18
	 *  old action handlers.
19
	 *
20
	 * $context['data'] will be passed to the action.
21
	 *
22
	 * @param mixed $level The log level.
23
	 * @param string $message The message to log.
24
	 * @param array $context The context.
25
	 *
26
	 * @return void
27
	 */
28 1
	protected function log( $level, $message, $context ) {
29
30 1
		$backtrace = debug_backtrace();
31 1
		$location = $this->interpolate( "{class}{type}{function}", $backtrace[2] );
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal {class}{type}{function} 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...
32
33 1
		$message = $this->interpolate( "[$level, $location] $message", $context );
34
35
		switch ( $level ):
36 1
			case LogLevel::EMERGENCY:
37 1
			case LogLevel::ALERT:
38 1
			case LogLevel::CRITICAL:
39 1
			case LogLevel::ERROR:
40 1
				$action = 'error';
41 1
				break;
42 1
			case LogLevel::WARNING:
43 1
			case LogLevel::NOTICE:
44 1
			case LogLevel::INFO:
45
			case LogLevel::DEBUG:
46 1
				$action = 'debug';
47 1
				break;
48
		endswitch;
49
50 1
		if ( defined( 'DOING_GRAVITYVIEW_TESTS' ) ) {
51
			/** Let's make this testable! */
52 1
			do_action(
53 1
				sprintf( 'gravityview_log_%s_test', $action ),
0 ignored issues
show
Bug introduced by
The variable $action does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
54 1
				$this->interpolate( $message, $context ),
55 1
				empty( $context['data'] ) ? array() : $context['data']
56
			);
57
		}
58
		
59 1
		do_action(
60 1
			sprintf( 'gravityview_log_%s', $action ),
61 1
			$this->interpolate( $message, $context ),
62 1
			empty( $context['data'] ) ? array() : $context['data']
63
		);
64 1
	}
65
}
66