ValidationResultsCollectorImpl::addWarning()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
rs 9.8666
1
<?php
2
3
namespace Xml\Impl\Validation;
4
5
use Xml\Validation\{
6
    ValidationResultCollectorInterface,
7
    ValidationResultType,
8
    ValidationResultsInterface
9
};
10
use Xml\Instance\ModelElementInstanceInterface;
11
12
class ValidationResultsCollectorImpl implements ValidationResultCollector
0 ignored issues
show
Bug introduced by
The type Xml\Impl\Validation\ValidationResultCollector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
{
14
    protected $currentElement;
15
    protected $collectedResults = [];
16
    protected $errorCount = 0;
17
    protected $warningCount = 0;
18
19
    public function addError(int $code, string $message): void
20
    {
21
        $resultsByElement = $this->resultsForCurrentElement();
22
        $resultsByElement['value'][] = new ModelValidationResultImpl(
23
            $this->currentElement,
24
            ValidationResultType::ERROR,
25
            $code,
26
            $message
27
        );
28
        foreach ($this->collectedResults as $key => $collectedResult) {
29
            if ($collectedResult['key'] == $this->currentElement) {
30
                $this->collectedResults[$key] = [
31
                    'key' => $this->currentElement,
32
                    'value' => $resultsByElement['value']
33
                ];
34
                break;
35
            }
36
        }
37
    }
38
39
    public function addWarning(int $code, string $message): void
40
    {
41
        $resultsByElement = $this->resultsForCurrentElement();
42
        $resultsByElement['value'][] = new ModelValidationResultImpl(
43
            $this->currentElement,
44
            ValidationResultType::WARNING,
45
            $code,
46
            $message
47
        );
48
        foreach ($this->collectedResults as $key => $collectedResult) {
49
            if ($collectedResult['key'] == $this->currentElement) {
50
                $this->collectedResults[$key] = [
51
                    'key' => $this->currentElement,
52
                    'value' => $resultsByElement['value']
53
                ];
54
                break;
55
            }
56
        }
57
    }
58
59
    protected function resultsForCurrentElement(): array
60
    {
61
        $exists = false;
62
        foreach ($this->collectedResults as $collectedResult) {
63
            if ($collectedResult['key'] == $this->currentElement) {
64
                $resultsByElement = $collectedResult['value'];
65
                $exists = true;
66
                break;
67
            }
68
        }
69
        if (!$exists) {
70
            $res = [
71
                'key' => $this->currentElement,
72
                'value' => []
73
            ];
74
            $this->collectedResults[] = $res;
75
        } else {
76
            $res = [
77
                'key' => $this->currentElement,
78
                'value' => $resultsByElement
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $resultsByElement does not seem to be defined for all execution paths leading up to this point.
Loading history...
79
            ];
80
            $this->collectedResults[] = $res;
81
        }
82
        return $res;
83
    }
84
85
    public function setCurrentElement(ModelElementInstanceInterface $currentElement): void
86
    {
87
        $this->currentElement = $currentElement;
88
    }
89
90
    public function getResults(): ValidationResultsInterface
91
    {
92
        return new ModelValidationResultsImpl($this->collectedResults, $this->errorCount, $this->warningCount);
93
    }
94
}
95