Passed
Push — master ( bace86...bb4cad )
by Dmitriy
05:30 queued 02:57
created

BacktraceIgnoreMatcher::isIgnoredByFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Debug\Helper;
6
7
/**
8
 * All backtrace parameters should contain at least 4 elements in the following order:
9
 * 0 – Called method
10
 * 1 – Proxy
11
 * 2 – Real using place / Composer\ClassLoader include function
12
 * 3 – Whatever / Composer\ClassLoader
13
 */
14
final class BacktraceIgnoreMatcher
15
{
16
    public static function isIgnoredByFile(array $backtrace, array $patterns): bool
17
    {
18
        if (!isset($backtrace[2])) {
19
            return false;
20
        }
21
        $path = $backtrace[2]['file'];
22
23
        return self::doesStringMatchPattern($path, $patterns);
24
    }
25
26
    public static function isIgnoredByClass(array $backtrace, array $classes): bool
27
    {
28
        return isset($backtrace[3]['class']) && in_array($backtrace[3]['class'], $classes, true);
29
    }
30
31
    public static function doesStringMatchPattern(string $string, array $patterns): bool
32
    {
33
        foreach ($patterns as $ignoredPathPattern) {
34
            if (preg_match($ignoredPathPattern, $string) > 0) {
35
                return true;
36
            }
37
        }
38
        return false;
39
    }
40
}
41