Completed
Push — master ( d6fd04...fc71a7 )
by Nikolay
10s
created

DefaultMessageFilter::belowErrorCountThreshold()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Kolyunya\Codeception\Lib\MarkupValidator;
4
5
use Exception;
6
use Kolyunya\Codeception\Lib\Base\Component;
7
use Kolyunya\Codeception\Lib\MarkupValidator\MessageFilterInterface;
8
use Kolyunya\Codeception\Lib\MarkupValidator\MarkupValidatorMessageInterface;
9
10
/**
11
 * Default markup validation message filter.
12
 */
13
class DefaultMessageFilter extends Component implements MessageFilterInterface
14
{
15
    const ERROR_COUNT_THRESHOLD_KEY = 'errorCountThreshold';
16
17
    const IGNORE_WARNINGS_CONFIG_KEY = 'ignoreWarnings';
18
19
    const IGNORED_ERRORS_CONFIG_KEY = 'ignoredErrors';
20
21
    /**
22
     * Configuration parameters.
23
     *
24
     * @var array
25
     */
26
    protected $configuration = array(
27
        self::ERROR_COUNT_THRESHOLD_KEY => 0,
28
        self::IGNORE_WARNINGS_CONFIG_KEY => true,
29
        self::IGNORED_ERRORS_CONFIG_KEY => array(),
30
    );
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function filterMessages(array $messages)
36
    {
37
        $filteredMessages = array();
38
39
        foreach ($messages as $message) {
40
            /* @var $message MarkupValidatorMessageInterface */
41
            $messageType = $message->getType();
42
43
            if ($messageType === MarkupValidatorMessageInterface::TYPE_UNDEFINED ||
44
                $messageType === MarkupValidatorMessageInterface::TYPE_INFO
45
            ) {
46
                continue;
47
            }
48
49
            if ($messageType === MarkupValidatorMessageInterface::TYPE_WARNING &&
50
                $this->ignoreWarnings() === true
51
            ) {
52
                continue;
53
            }
54
55
            if ($this->ignoreError($message->getSummary()) === true) {
56
                continue;
57
            }
58
59
            $filteredMessages[] = $message;
60
        }
61
62
        if ($this->belowErrorCountThreshold($filteredMessages) === true) {
63
            // Error count threshold was not reached.
64
            return array();
65
        }
66
67
        return $filteredMessages;
68
    }
69
70
    /**
71
     * Returns a boolean indicating whether messages count
72
     * is below the threshold or not.
73
     *
74
     * @param array $messages Messages to report about.
75
     *
76
     * @return boolean Whether messages count is below the threshold or not.
77
     */
78
    private function belowErrorCountThreshold(array $messages)
79
    {
80
        if (is_int($this->configuration[self::ERROR_COUNT_THRESHOLD_KEY]) === false) {
81
            throw new Exception(sprintf('Invalid «%s» config key.', self::ERROR_COUNT_THRESHOLD_KEY));
82
        }
83
84
        $threshold = $this->configuration[self::ERROR_COUNT_THRESHOLD_KEY];
85
        $belowThreshold = count($messages) <= $threshold;
86
87
        return $belowThreshold;
88
    }
89
90
    /**
91
     * Returns a boolean indicating whether the filter ignores warnings or not.
92
     *
93
     * @return bool Whether the filter ignores warnings or not.
94
     */
95
    private function ignoreWarnings()
96
    {
97
        if (is_bool($this->configuration[self::IGNORE_WARNINGS_CONFIG_KEY]) === false) {
98
            throw new Exception(sprintf('Invalid «%s» config key.', self::IGNORE_WARNINGS_CONFIG_KEY));
99
        }
100
101
        /* @var $ignoreWarnings bool */
102
        $ignoreWarnings = $this->configuration[self::IGNORE_WARNINGS_CONFIG_KEY];
103
104
        return $ignoreWarnings;
105
    }
106
107
    /**
108
     * Returns a boolean indicating whether an error is ignored or not.
109
     *
110
     * @param string|null $summary Error summary.
111
     * @return boolean Whether an error is ignored or not.
112
     */
113
    private function ignoreError($summary)
114
    {
115
        if (is_array($this->configuration[self::IGNORED_ERRORS_CONFIG_KEY]) === false) {
116
            throw new Exception(sprintf('Invalid «%s» config key.', self::IGNORED_ERRORS_CONFIG_KEY));
117
        }
118
119
        $ignoreError = false;
120
121
        if ($summary === null) {
122
            return $ignoreError;
123
        }
124
125
        $ignoredErrors = $this->configuration[self::IGNORED_ERRORS_CONFIG_KEY];
126
        foreach ($ignoredErrors as $ignoredError) {
127
            $erorIsIgnored = preg_match($ignoredError, $summary) === 1;
128
            if ($erorIsIgnored) {
129
                $ignoreError = true;
130
                break;
131
            }
132
        }
133
134
        return $ignoreError;
135
    }
136
}
137