Passed
Pull Request — master (#19)
by Alexander
02:37 queued 15s
created

MessageFilterTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B filterMessages() 0 28 9
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
            $matched = empty($this->include);
46
47 10
            if (!$matched) {
48 2
                foreach ($this->include as $category) {
49 2
                    if ((new WildcardPattern($category))->match($message->level())) {
50 1
                        $matched = true;
51 1
                        break;
52
                    }
53
                }
54
            }
55
56 10
            if ($matched) {
57 9
                foreach ($this->exclude as $category) {
58 3
                    if ((new WildcardPattern($category))->match($message->level())) {
59 2
                        $matched = false;
60 2
                        break;
61
                    }
62
                }
63
            }
64
65 10
            if (!$matched) {
66 3
                unset($messages[$i]);
67
            }
68
        }
69 10
        return $messages;
70
    }
71
}
72