AnalysisResult::setResultsFilter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Finder\Logic;
3
4
use Finder\Logic\Output\Filter\OutputFilterInterface;
5
6
/**
7
 * Code analysis result.
8
 */
9
class AnalysisResult
10
{
11
    /**
12
     * Result data.
13
     *
14
     * @var array analysis result data.
15
     */
16
    protected $data = [];
17
18
    /**
19
     * Analysis result filter.
20
     *
21
     * @var OutputFilterInterface filter instance.
22
     */
23
    protected $filter;
24
25
    /**
26
     * Register an issue.
27
     *
28
     * @param  string  $fileName  File name.
29
     * @param  integer $line      Line number.
30
     * @param  string  $toolName  Analysis tool name.
31
     * @param  string  $issueType Issue type.
32
     * @param  string  $message   Issue description.
33
     * @return void
34
     */
35
    public function addIssue($fileName, $line, $toolName, $issueType, $message)
36
    {
37
        if (!isset($this->data[$fileName])) {
38
            $this->data[$fileName] = [];
39
        }
40
41
        if (!isset($this->data[$fileName][$line])) {
42
            $this->data[$fileName][$line] = [];
43
        }
44
45
        $this->data[$fileName][$line][] = [
46
            'tool' => $toolName,
47
            'type' => $issueType,
48
            'message' => $message,
49
        ];
50
    }
51
52
    /**
53
     * Check if there are any code issues.
54
     *
55
     * @return boolean true if there are issues.
56
     */
57
    public function hasIssues()
58
    {
59
        // Required by PHP 5.4
60
        $array = $this->toArray();
61
        return !empty($array);
62
    }
63
64
    /**
65
     * Return result data as an array.
66
     * <code>
67
     * file_name => [
68
     *     line_number => [
69
     *         issue 1,
70
     *         issue 2,
71
     *         ...
72
     *     ]
73
     * ]
74
     * </code>
75
     *
76
     * @return array result daya.
77
     */
78
    public function toArray()
79
    {
80
        $data = [];
81
82
        foreach ($this->data as $fileName => $lines) {
83
            ksort($lines);
84
            $data[$fileName] = $lines;
85
        }
86
87
        ksort($data);
88
89
        if (null !== $this->filter) {
90
            $data = $this->filter->filter($data);
91
        }
92
93
        return $data;
94
    }
95
96
    /**
97
     * Merge the data of another result into this one.
98
     *
99
     * @param  AnalysisResult $other another analysis result object.
100
     * @return AnalysisResult returns itself.
101
     */
102
    public function mergeWith(AnalysisResult $other)
103
    {
104
        foreach ($other->toArray() as $fileName => $lines) {
105
            foreach ($lines as $line => $issues) {
106
                foreach ($issues as $issue) {
107
                    $this->addIssue(
108
                        $fileName,
109
                        $line,
110
                        $issue['tool'],
111
                        $issue['type'],
112
                        $issue['message']
113
                    );
114
                }
115
            }
116
        }
117
        return $this;
118
    }
119
120
    /**
121
     * Add an output filter to delegate to the analysis result object.
122
     *
123
     * @param OutputFilterInterface $filter filter instance.
124
     */
125
    public function setResultsFilter(OutputFilterInterface $filter)
126
    {
127
        $this->filter = $filter;
128
    }
129
}
130