Failed Conditions
Pull Request — experimental/3.1 (#2534)
by chihiro
09:59
created

isTraceClassOrSkippedFunction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 4
cp 0.75
crap 3.1406
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 3
    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 3
        $this->level = Logger::toMonologLevel($level);
28 3
        $this->skipClassesPartials = array_merge(array('Monolog\\'), $skipClassesPartials);
29 3
        $this->skipFunctions = array_merge($this->skipFunctions, $skipFunctions);
30 3
        $this->skipStackFramesCount = $skipStackFramesCount;
31
    }
32
33
    /**
34
     * @param  array $record
35
     * @return array
36
     */
37 3
    public function __invoke(array $record)
38
    {
39
        // return if the level is not high enough
40 3
        if ($record['level'] < $this->level) {
41
            return $record;
42
        }
43
44 3
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
45
46
        // skip first since it's always the current method
47 3
        array_shift($trace);
48
        // the call_user_func call is also skipped
49 3
        array_shift($trace);
50
51 3
        $i = 0;
52
53 3
        while ($this->isTraceClassOrSkippedFunction($trace, $i)) {
54 3
            if (isset($trace[$i]['class'])) {
55 3
                foreach ($this->skipClassesPartials as $part) {
56 3
                    if (strpos($trace[$i]['class'], $part) !== false) {
57 3
                        $i++;
58 3
                        continue 2;
59
                    }
60
                }
61
            } elseif (in_array($trace[$i]['function'], $this->skipFunctions)) {
62
                $i++;
63
                continue;
64
            }
65
66 3
            break;
67
        }
68
69 3
        $i += $this->skipStackFramesCount;
70
71
        // we should have the call source now
72 3
        $record['extra'] = array_merge(
73 3
            $record['extra'],
74
            array(
75 3
                'file' => isset($trace[$i - 1]['file']) ? $trace[$i - 1]['file'] : null,
76 3
                'line' => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null,
77 3
                'class' => isset($trace[$i]['class']) ? $trace[$i]['class'] : null,
78 3
                'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null,
79
            )
80
        );
81
82 3
        return $record;
83
    }
84
85 3
    private function isTraceClassOrSkippedFunction(array $trace, $index)
86
    {
87 3
        if (!isset($trace[$index])) {
88
            return false;
89
        }
90
91 3
        return isset($trace[$index]['class']) || in_array($trace[$index]['function'], $this->skipFunctions);
92
    }
93
}
94