Passed
Pull Request — master (#19)
by Rustam
02:33
created

MessageFilterTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 63
ccs 17
cts 17
cp 1
rs 10
c 1
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isCategoryMatched() 0 20 6
A filterMessages() 0 8 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Profiler;
6
7
use Yiisoft\Strings\WildcardPattern;
8
9
trait MessageFilterTrait
10
{
11
    /**
12
     * @var array list of message categories that this target is interested in. Defaults to empty, meaning all
13
     * categories.
14
     *
15
     * You can use an asterisk at the end of a category so that the category may be used to
16
     * match those categories sharing the same common prefix. For example, 'Yiisoft\Db\*' will match
17
     * categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`.
18
     */
19
    public array $include = [];
20
21
    /**
22
     * @var array list of message categories that this target is NOT interested in. Defaults to empty, meaning no
23
     * uninteresting messages.
24
     *
25
     * If this property is not empty, then any category listed here will be excluded from {@see include}.
26
     * You can use an asterisk at the end of a category so that the category can be used to
27
     * match those categories sharing the same common prefix. For example, 'Yiisoft\Db\*' will match
28
     * categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`.
29
     *
30
     * {@see include}
31
     */
32
    public array $exclude = [];
33
34
    /**
35
     * Filters the given messages according to their categories.
36
     *
37
     * @param Message[] $messages messages to be filtered.
38
     * The message structure follows that in {@see Profiler::$messages}.
39
     *
40
     * @return array the filtered messages.
41
     */
42 10
    protected function filterMessages(array $messages): array
43
    {
44 10
        foreach ($messages as $i => $message) {
45 10
            if (!$this->isCategoryMatched($message->level())) {
46 3
                unset($messages[$i]);
47
            }
48
        }
49 10
        return $messages;
50
    }
51
52 10
    private function isCategoryMatched($category): bool
53
    {
54 10
        $matched = empty($this->include);
55
56 10
        foreach ($this->include as $pattern) {
57 2
            if ((new WildcardPattern($pattern))->match($category)) {
58 1
                $matched = true;
59 1
                break;
60
            }
61
        }
62
63 10
        if ($matched) {
64 9
            foreach ($this->exclude as $pattern) {
65 3
                if ((new WildcardPattern($pattern))->match($category)) {
66 2
                    $matched = false;
67 2
                    break;
68
                }
69
            }
70
        }
71 10
        return $matched;
72
    }
73
}
74