Completed
Branch master (c5ceed)
by Tomáš
33:54 queued 13:17
created

ErrorDataCollector::addErrorMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 6
crap 6
1
<?php
2
3
/*
4
 * This file is part of Symplify
5
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
6
 */
7
8
namespace Symplify\PHP7_CodeSniffer;
9
10
use Symplify\PHP7_CodeSniffer\EventDispatcher\CurrentListenerSniffCodeProvider;
11
use Symplify\PHP7_CodeSniffer\File\File;
12
13
final class ErrorDataCollector
14
{
15
    /**
16
     * @var int
17
     */
18
    private $errorCount = 0;
19
20
    /**
21
     * @var int
22
     */
23
    private $fixableErrorCount = 0;
24
25
    /**
26
     * @var array[]
27
     */
28
    private $errorMessages = [];
29
30
    /**
31
     * @var CurrentListenerSniffCodeProvider
32
     */
33
    private $currentListenerSniffCodeProvider;
34
35
    public function __construct(CurrentListenerSniffCodeProvider $currentListenerSniffCodeProvider)
36
    {
37
        $this->currentListenerSniffCodeProvider = $currentListenerSniffCodeProvider;
38
    }
39
40
    public function getErrorCount() : int
41
    {
42
        return $this->errorCount;
43
    }
44
45
    public function getFixableErrorCount() : int
46
    {
47
        return $this->fixableErrorCount;
48
    }
49
50
    public function getErrorMessages() : array
51
    {
52
        return $this->errorMessages;
53
    }
54
55
    public function addErrorMessage(string $filePath, string $message, int $line, string $sniffCode, array $data, bool $isFixable)
56
    {
57
        $this->errorCount++;
58
59
        if ($isFixable) {
60
            $this->fixableErrorCount++;
61
        }
62
63
        $this->errorMessages[$filePath][] = [
64
            'line' => $line,
65
            'message' => $this->applyDataToMessage($message, $data),
66
            'sniffCode' => $this->getSniffFullCode($sniffCode),
67
            'isFixable'  => $isFixable
68
        ];
69
    }
70
71
    private function getSniffFullCode(string $sniffCode) : string
72
    {
73
        $parts = explode('.', $sniffCode);
74
        if ($parts[0] !== $sniffCode) {
75
            return $sniffCode;
76
        }
77
78
        $listenerSniffCode = $this->currentListenerSniffCodeProvider->getCurrentListenerSniffCode();
79
        return $listenerSniffCode.'.'.$sniffCode;
80
    }
81
82
    private function applyDataToMessage(string $message, array $data) : string
83
    {
84
        if (count($data)) {
85
            $message = vsprintf($message, $data);
86
        }
87
88
        return $message;
89
    }
90
}
91