Test Failed
Push — 0.3 ( c5ac0f...1929bf )
by Diego
02:49
created

BasicLogging::logError()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php namespace DiegoCaprioli\Larachimp\Traits;
2
3
use Illuminate\Contracts\Logging\Log;
4
5
/**
6
 * Provides quick and convinient log methods based that used the class 
7
 * configured Log service and based in the package log_level configuration
8
 */
9
trait BasicLogging {
10
11
	/**
12
     * The logger to user
13
     * @var \Illuminate\Contracts\Logging\Log
14
     */
15
    protected $log;
16
17
18
    /**
19
     * Sets the log attribute
20
     * 
21
     * @param Log $log
22
     */
23
    public function setLog(Log $log = null)
24
    {
25
        $this->log = $log;
26
    }
27
28
    /**
29
     * If there's a logger defined, it logs the string
30
     * 
31
     * @param string $string The string to log     
32
     */
33
    protected function logInfo($string)
34
    {
35
        if (
36
            !empty($this->log) and 
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
37
            config('diegocaprioli.larachimp.larachimp.log_level', 'error') == 'info'
38
        ) {
39
            $this->log->info($string);
40
        }
41
    }
42
43
    /**
44
     * If there's a logger defined, it logs the string
45
     * 
46
     * @param string $string The string to log     
47
     */
48
    protected function logError($string)
49
    {
50
        if (!empty($this->log)) {
51
            $this->log->error($string);
52
        }
53
    }
54
55
}