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
|
|
|
|