MessDetectorReporter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 68
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A parseViolations() 0 18 3
B report() 0 22 5
1
<?php declare(strict_types=1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 22.09.16 at 15:26
5
 */
6
namespace samsonframework\bitbucket;
7
8
use Symfony\Component\Console\Logger\ConsoleLogger;
9
10
/**
11
 * PHP mess detector violations reporter.
12
 *
13
 * @author Vitaly Egorov <[email protected]>
14
 */
15
class MessDetectorReporter extends Reporter implements ViolationReporterInterface
16
{
17
    /** XML File path field */
18
    const FILEPATH = 'name';
19
20
    /** XML File line number  */
21
    const LINENUMBER = 'beginline';
22
23
    /** @var string Base file path marker for normalization */
24
    protected $basePath;
25
26
    /**
27
     * MessDetectorReporter constructor.
28
     *
29
     * @param string $path Reporter source path
30
     * @param string $basePath Base file path marker for normalization
31
     */
32
    public function __construct(string $path, string $basePath = '/src')
33
    {
34
        $this->basePath = $basePath;
35
36
        parent::__construct($path);
37
    }
38
39
    /** {@inheritdoc} */
40
    public function parseViolations() : array
41
    {
42
        // Read XML and convert to array
43
        $xmlData = simplexml_load_string(file_get_contents($this->path));
44
45
        /** @var array $violations Collection of violations grouped by files and lines */
46
        $violations = [];
47
48
        foreach ($xmlData->file as $file) {
49
            $filePath = (string)$file[self::FILEPATH];
50
            $pointer = &$violations[ltrim(substr($filePath, strpos($filePath, $this->basePath)), '/')];
51
            foreach ($file->violation as $violation) {
52
                $pointer[(string)$violation[self::LINENUMBER]][] = trim((string)$violation);
53
            }
54
        }
55
56
        return $violations;
57
    }
58
59
    /** {@inheritdoc} */
60
    public function report(CloudReporter $bitbucket, ConsoleLogger $logger)
61
    {
62
        $violations = $this->parseViolations();
63
64
        $logger->log(ConsoleLogger::INFO, 'Found '.count($violations, COUNT_RECURSIVE).' violations');
65
66
        // Iterate only files changed by pull request
67
        foreach ($bitbucket->getChangedFiles() as $file) {
68
            // Check if we have PMD violations in that files
69
            if (array_key_exists($file, $violations)) {
70
                // Iterate file violations
71
                // TODO: Check lines if they are within this changeset
72
                foreach ($violations[$file] as $line => $violations) {
73
                    // Iterate file line violations
74
                    foreach ($violations as $violation) {
75
                        // Send comment to BitBucket pull request
76
                        $bitbucket->createFileComment($violation, $file, $line);
77
                    }
78
                }
79
            }
80
        }
81
    }
82
}
83