Completed
Pull Request — master (#235)
by Kévin
03:13
created

TextFormatter::reportEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PHPSA\Report\Formatter;
4
5
use PHPSA\Issue;
6
7
class TextFormatter implements ReportFormatter
8
{
9
    /**
10
     * {@inheritdoc}
11
     */
12
    public function reportBeginning()
13
    {
14
        return '';
15
    }
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function reportEnd()
21
    {
22
        return '';
23
    }
24
25
    /**
26
     * @param Issue $issue
27
     *
28
     * @return string The formatted issue.
29
     */
30
    public function formatIssue(Issue $issue)
31
    {
32
        if ($issue->getCheckName() === 'syntax-error') {
33
            return $this->formatSyntaxError($issue);
34
        }
35
36
        return $this->formatNotice($issue);
37
    }
38
39
    private function formatSyntaxError(Issue $issue)
40
    {
41
        $filepath = $issue->getLocation()->getFilePath();
42
        $line = $issue->getLocation()->getLineStart();
43
        $codeLines = file($filepath);
44
45
        $output = '<error>Syntax error:  ' . $issue->getDescription() . " in {$filepath} </error>\n\n";
46
47
        $lineContent = trim($codeLines[$line-1]);
48
        if (!empty($lineContent)) {
49
            $output .= "<comment>\t {$lineContent} </comment>\n";
50
        }
51
52
        return $output . "\n";
53
    }
54
55
    private function formatNotice(Issue $issue)
56
    {
57
        $filepath = $issue->getLocation()->getFilePath();
58
        $line = $issue->getLocation()->getLineStart();
59
        $codeLines = file($filepath);
60
61
        $output = '<comment>Notice:  ' . $issue->getDescription() . " in {$filepath} on {$line} [{$issue->getCheckName()}]</comment>\n\n";
62
63
        if ($issue->getBlame()) {
64
            $output .= "<comment>\t {$issue->getBlame()}</comment>\n";
65
        } else {
66
            $code = trim($codeLines[$line - 1]);
67
68
            if (!empty($code)) {
69
                $output .= "<comment>\t {$code} </comment>\n";
70
            }
71
        }
72
73
        return $output . "\n";
74
    }
75
}
76