Failed Conditions
Pull Request — master (#1874)
by chihiro
237:31 queued 230:10
created

IntrospectionProcessor   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
C __invoke() 0 47 11
A isTraceClassOrSkippedFunction() 0 8 3
1
<?php
2
3
namespace Eccube\Log\Monolog\Processor;
4
5
use Monolog\Logger;
6
7
/**
8
 * IntrospectionProcessor拡張クラス
9
 *
10
 * @package Eccube\Log\Monolog\Processor
11
 */
12
class IntrospectionProcessor
13
{
14
    private $level;
15
16
    protected $skipClassesPartials;
17
18
    protected $skipStackFramesCount;
19
20
    protected $skipFunctions = array(
21
        'call_user_func',
22
        'call_user_func_array',
23
    );
24
25
    public function __construct($level = Logger::DEBUG, array $skipClassesPartials = array(), array $skipFunctions = array(), $skipStackFramesCount = 0)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
26
    {
27
        $this->level = Logger::toMonologLevel($level);
28
        $this->skipClassesPartials = array_merge(array('Monolog\\'), $skipClassesPartials);
29
        $this->skipFunctions = array_merge($this->skipFunctions, $skipFunctions);
30
        $this->skipStackFramesCount = $skipStackFramesCount;
31
    }
32
33
    /**
34
     * @param  array $record
35
     * @return array
36
     */
37
    public function __invoke(array $record)
38
    {
39
        // return if the level is not high enough
40
        if ($record['level'] < $this->level) {
41
            return $record;
42
        }
43
44
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
45
46
        // skip first since it's always the current method
47
        array_shift($trace);
48
        // the call_user_func call is also skipped
49
        array_shift($trace);
50
51
        $i = 0;
52
53
        while ($this->isTraceClassOrSkippedFunction($trace, $i)) {
54
            if (isset($trace[$i]['class'])) {
55
                foreach ($this->skipClassesPartials as $part) {
56
                    if (strpos($trace[$i]['class'], $part) !== false) {
57
                        $i++;
58
                        continue 2;
59
                    }
60
                }
61
            } elseif (in_array($trace[$i]['function'], $this->skipFunctions)) {
62
                $i++;
63
                continue;
64
            }
65
66
            break;
67
        }
68
69
        $i += $this->skipStackFramesCount;
70
71
        // we should have the call source now
72
        $record['extra'] = array_merge(
73
            $record['extra'],
74
            array(
75
                'file' => isset($trace[$i - 1]['file']) ? $trace[$i - 1]['file'] : null,
76
                'line' => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null,
77
                'class' => isset($trace[$i]['class']) ? $trace[$i]['class'] : null,
78
                'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null,
79
            )
80
        );
81
82
        return $record;
83
    }
84
85
    private function isTraceClassOrSkippedFunction(array $trace, $index)
86
    {
87
        if (!isset($trace[$index])) {
88
            return false;
89
        }
90
91
        return isset($trace[$index]['class']) || in_array($trace[$index]['function'], $this->skipFunctions);
92
    }
93
}
94