AnalysisResult::toArray()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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