Passed
Push — master ( 4f923e...f2cf92 )
by 世昌
01:48
created

Caller::isIgnore()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\component\debug;
3
4
/**
5
 * 回溯调用者
6
 */
7
class Caller
8
{
9
    protected $ignorePath = [__FILE__];
10
    protected $backtrace;
11
12
    public function __construct(array $backtrace, array $ignorePath =[])
13
    {
14
        $this->ignorePath = \array_merge($this->ignorePath, $ignorePath);
15
        $this->backtrace = $backtrace;
16
    }
17
18
    public function getCallerTrace():?array
19
    {
20
        foreach ($this->backtrace as $trace) {
21
            if (array_key_exists('file', $trace)) {
22
                if (!$this->isIgnore($trace['file'])) {
23
                    return $trace;
24
                }
25
            }
26
        }
27
        return null;
28
    }
29
30
    protected function isIgnore(string $file):bool {
31
        foreach ($this->ignorePath as $path) {
32
            if (strpos($file, $path) === 0) {
33
                return true;
34
            }
35
        }
36
        return false;
37
    }
38
}
39