Passed
Pull Request — master (#49)
by Evgeniy
01:57
created

MessageCategory::isExcluded()   C

Complexity

Conditions 13
Paths 9

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
eloc 17
c 0
b 0
f 0
nc 9
nop 1
dl 0
loc 31
ccs 15
cts 15
cp 1
crap 13
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Log;
6
7
use Psr\Log\InvalidArgumentException;
8
9
use function gettype;
10
use function is_string;
11
use function rtrim;
12
use function substr_compare;
13
use function sprintf;
14
use function strpos;
15
16
/**
17
 * Category is a data object that stores and matches the included and excluded categories of log messages.
18
 */
19
final class MessageCategory
20
{
21
    public const DEFAULT = 'application';
22
23
    /**
24
     * @var string[] The list of included log message categories.
25
     *
26
     * Defaults to empty, which means all categories are included.
27
     *
28
     * You can use an asterisk at the end of a category so that the category may be used to
29
     * match those categories sharing the same common prefix. For example, 'Yiisoft\Db\*' will match
30
     * categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`.
31
     */
32
    private array $include = [];
33
34
    /**
35
     * @var string[] The list of excluded log message categories.
36
     *
37
     * Defaults to empty, which means there are no excluded categories.
38
     *
39
     * You can use an asterisk at the end of a category so that the category can be used to
40
     * match those categories sharing the same common prefix. For example, 'Yiisoft\Db\*' will match
41
     * categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`.
42
     */
43
    private array $exclude = [];
44
45
    /**
46
     * Sets the log message categories to be included.
47
     *
48
     * @param string[] $categories List of log message categories to be included.
49
     * @throws InvalidArgumentException for invalid log message categories structure.
50
     */
51 28
    public function include(array $categories): void
52
    {
53 28
        $this->checkStructure($categories);
54 16
        $this->include = $categories;
55 16
    }
56
57
    /**
58
     * Gets the log message categories to be included.
59
     *
60
     * @return string[] The list of log message categories to be included.
61
     */
62 2
    public function getIncluded(): array
63
    {
64 2
        return $this->include;
65
    }
66
67
    /**
68
     * Sets the log message categories to be excluded.
69
     *
70
     * @param string[] $categories The list of log message categories to be excluded.
71
     * @throws InvalidArgumentException for invalid log message categories structure.
72
     */
73 19
    public function exclude(array $categories): void
74
    {
75 19
        $this->checkStructure($categories);
76 7
        $this->exclude = $categories;
77 7
    }
78
79
    /**
80
     * Gets the log message categories to be excluded.
81
     *
82
     * @return string[] The list of log message categories to be excluded.
83
     */
84 2
    public function getExcluded(): array
85
    {
86 2
        return $this->exclude;
87
    }
88
89
    /**
90
     * Checks whether the specified log message category is excluded.
91
     *
92
     * @param string $category The log message category.
93
     * @return bool The value indicating whether the specified category is excluded.
94
     */
95 31
    public function isExcluded(string $category): bool
96
    {
97 31
        foreach ($this->exclude as $exclude) {
98 5
            $prefix = rtrim($exclude, '*');
99
100
            if (
101 5
                (($category && $category === $exclude) || $prefix !== $exclude)
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($category && $category ...ategory, $prefix) === 0, Probably Intended Meaning: $category && $category =...tegory, $prefix) === 0)
Loading history...
102 5
                && (strpos($category, $prefix) === 0)
103
            ) {
104 5
                return true;
105
            }
106
        }
107
108 31
        if (empty($this->include)) {
109 17
            return false;
110
        }
111
112 14
        foreach ($this->include as $include) {
113
            if (
114 14
                ($category && $category === $include)
115
                || (
116 14
                    !empty($include)
117 14
                    && substr_compare($include, '*', -1, 1) === 0
118 14
                    && strpos($category, rtrim($include, '*')) === 0
119
                )
120
            ) {
121 13
                return false;
122
            }
123
        }
124
125 14
        return true;
126
    }
127
128
    /**
129
     * Checks message categories structure.
130
     *
131
     * @param array $categories The log message categories to be checked.
132
     * @throws InvalidArgumentException for invalid log message categories structure.
133
     */
134 43
    private function checkStructure(array $categories): void
135
    {
136 43
        foreach ($categories as $category) {
137 43
            if (!is_string($category)) {
138 24
                throw new InvalidArgumentException(sprintf(
139 24
                    'The log message category must be a string, %s received.',
140 24
                    gettype($category)
141
                ));
142
            }
143
        }
144 19
    }
145
}
146