JSONRenderer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 76
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initReportData() 0 10 1
A renderReport() 0 36 3
A encodeReport() 0 10 2
1
<?php
2
/**
3
 * This file is part of PHP Mess Detector.
4
 *
5
 * Copyright (c) Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Licensed under BSD License
9
 * For full copyright and license information, please see the LICENSE file.
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @author Manuel Pichler <[email protected]>
13
 * @copyright Manuel Pichler. All rights reserved.
14
 * @license https://opensource.org/licenses/bsd-license.php BSD License
15
 * @link http://phpmd.org/
16
 */
17
18
namespace PHPMD\Renderer;
19
20
use PHPMD\AbstractRenderer;
21
use PHPMD\PHPMD;
22
use PHPMD\Report;
23
use PHPMD\RuleViolation;
24
25
/**
26
 * This class will render a JSON report.
27
 */
28
class JSONRenderer extends AbstractRenderer
29
{
30
    /**
31
     * Create report data and add renderer meta properties
32
     *
33
     * return array
34
     */
35 2
    private function initReportData()
36
    {
37
        $data = array(
38 2
            'version' => PHPMD::VERSION,
39 2
            'package' => 'phpmd',
40 2
            'timestamp' => date('c'),
41
        );
42
43 2
        return $data;
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49 2
    public function renderReport(Report $report)
50
    {
51 2
        $data = $this->initReportData();
52 2
        $filesList = array();
53
        /** @var RuleViolation $violation */
54 2
        foreach ($report->getRuleViolations() as $violation) {
55 1
            $fileName = $violation->getFileName();
56 1
            $rule = $violation->getRule();
57 1
            $filesList[$fileName]['file'] = $fileName;
58 1
            $filesList[$fileName]['violations'][] = array(
59 1
                'beginLine' => $violation->getBeginLine(),
60 1
                'endLine' => $violation->getEndLine(),
61 1
                'package' => $violation->getNamespaceName(),
62 1
                'function' => $violation->getFunctionName(),
63 1
                'class' => $violation->getClassName(),
64 1
                'method' => $violation->getMethodName(),
65 1
                'description' => $violation->getDescription(),
66 1
                'rule' => $rule->getName(),
67 1
                'ruleSet' => $rule->getRuleSetName(),
68 1
                'externalInfoUrl' => $rule->getExternalInfoUrl(),
69 1
                'priority' => $rule->getPriority(),
70
            );
71
        }
72 2
        $errorsList = array();
73 2
        foreach ($report->getErrors() as $error) {
74 1
            $errorsList[] = array(
75 1
                'fileName' => $error->getFile(),
76 1
                'message' => $error->getMessage(),
77
            );
78
        }
79 2
        $data['files'] = array_values($filesList);
80 2
        $data['errors'] = $errorsList;
81 2
        $json = $this->encodeReport($data);
82 2
        $writer = $this->getWriter();
83 2
        $writer->write($json . PHP_EOL);
84 2
    }
85
86
    /**
87
     * Encode report data to the JSON representation string
88
     *
89
     * @param $data array The report data
90
     *
91
     * @return string
92
     */
93 2
    private function encodeReport($data)
94
    {
95 2
        $encodeOptions = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP;
96
        // JSON_PRETTY_PRINT Available since PHP 5.4.0.
97 2
        if (defined('JSON_PRETTY_PRINT')) {
98 2
            $encodeOptions |= JSON_PRETTY_PRINT;
99
        }
100
101 2
        return json_encode($data, $encodeOptions);
102
    }
103
}
104