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

TextFormatter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 69
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A reportBeginning() 0 4 1
A reportEnd() 0 4 1
A formatIssue() 0 8 2
A formatSyntaxError() 0 15 2
A formatNotice() 0 20 3
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