PhpcsViolationsMapper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 20
dl 0
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B map() 0 28 7
1
<?php
2
3
namespace PhpcsDiff\Mapper;
4
5
class PhpcsViolationsMapper implements MapperInterface
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $changedLinesPerFile;
11
12
    /**
13
     * @var string
14
     */
15
    protected $currentDirectory;
16
17
    /**
18
     * @param array $changedLinesPerFile
19
     * @param string $currentDirectory
20
     */
21
    public function __construct(array $changedLinesPerFile, string $currentDirectory)
22
    {
23
        $this->changedLinesPerFile = $changedLinesPerFile;
24
        $this->currentDirectory = $currentDirectory;
25
    }
26
27
    /**
28
     * @param array $data
29
     * @return array
30
     */
31
    public function map(array $data): array
32
    {
33
        $mappedData = [];
34
35
        foreach ($data as $file => $report) {
36
            if (!isset($this->changedLinesPerFile[$file]) || !is_array($this->changedLinesPerFile[$file])) {
37
                continue;
38
            }
39
40
            $changedLinesFromDiff = $this->changedLinesPerFile[$file];
41
42
            $output = [];
43
            foreach ($report['messages'] as $message) {
44
                if (!in_array($message['line'], $changedLinesFromDiff, true)) {
45
                    continue;
46
                }
47
                $output[] = ' - Line ' . $message['line'] . ' (' . $message['type'] . ') ' . $message['message'];
48
            }
49
50
            if (empty($output)) {
51
                continue;
52
            }
53
54
            $mappedData[] = str_replace($this->currentDirectory . '/', '', $file) . PHP_EOL .
55
                implode(PHP_EOL, $output) . PHP_EOL;
56
        }
57
58
        return $mappedData;
59
    }
60
}
61