Completed
Push — master ( 3b5f38...c5ceed )
by Tomáš
04:59
created

ErrorDataCollector   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 78
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getErrorCount() 0 4 1
A getFixableErrorCount() 0 4 1
A getErrorMessages() 0 4 1
A addErrorMessage() 0 15 2
A getSniffFullCode() 0 10 2
A applyDataToMessage() 0 8 2
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