Passed
Pull Request — master (#215)
by Dmitriy
04:28 queued 01:56
created

BacktraceIgnoreMatcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 30
rs 10
c 1
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isIgnoredByFile() 0 8 2
A isIgnoredByClass() 0 3 2
A doesStringMatchPattern() 0 10 3
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 (bool) (isset($backtrace[3]['class']) && in_array($backtrace[3]['class'], $classes, true))
29
30
31
         ;
32
    }
33
34
    public static function doesStringMatchPattern(string $string, array $patterns): bool
35
    {
36
        $result = false;
37
        foreach ($patterns as $ignoredPathPattern) {
38
            if (preg_match($ignoredPathPattern, $string) > 0) {
39
                $result = true;
40
                break;
41
            }
42
        }
43
        return $result;
44
    }
45
}
46