Completed
Push — master ( a68eb8...eb5798 )
by Vitaly
03:48
created

MessDetectorReporter::report()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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