Skip to content

Commit 05033fc

Browse files
committed
Add support for phpmnd xml
1 parent 50e3d05 commit 05033fc

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

src/PhpMndXmlLoader.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
namespace exussum12\CoverageChecker;
3+
4+
use XMLReader;
5+
6+
class PhpMndXmlLoader implements FileChecker
7+
{
8+
protected $currentLine;
9+
private $invalidLines = [];
10+
11+
private $file;
12+
13+
public function __construct($filename)
14+
{
15+
$this->file = $filename;
16+
}
17+
18+
/**
19+
* @inheritdoc
20+
*/
21+
public function parseLines()
22+
{
23+
$reader = new XMLReader;
24+
$reader->open($this->file);
25+
$currentFile = '';
26+
while ($reader->read()) {
27+
$currentFile = $this->checkForNewFiles($reader, $currentFile);
28+
29+
$this->handleLine($reader, $currentFile);
30+
$this->handleErrors($reader, $currentFile);
31+
}
32+
33+
return array_keys($this->invalidLines);
34+
}
35+
36+
protected function checkForNewFiles(XMLReader $reader, $currentFile)
37+
{
38+
if ((
39+
$reader->name === "file" &&
40+
$reader->nodeType == XMLReader::ELEMENT
41+
)) {
42+
$currentFile = $reader->getAttribute('path');
43+
$this->invalidLines[$currentFile] = [];
44+
}
45+
return $currentFile;
46+
}
47+
48+
protected function handleLine(XMLReader $reader, $currentFile)
49+
{
50+
if ($reader->name === "entry") {
51+
$this->currentLine = $reader->getAttribute("line");
52+
if (!isset($this->invalidLines[$currentFile][$this->currentLine])) {
53+
$this->invalidLines[$currentFile][$this->currentLine] = [];
54+
}
55+
}
56+
}
57+
58+
protected function handleErrors(XMLReader $reader, $currentFile)
59+
{
60+
if ((
61+
$reader->name === "snippet" &&
62+
$reader->nodeType == XMLReader::ELEMENT
63+
)) {
64+
$this->invalidLines[$currentFile][$this->currentLine][] = $reader->readString();
65+
}
66+
}
67+
68+
/**
69+
* @inheritdoc
70+
*/
71+
public function getErrorsOnLine($file, $lineNumber)
72+
{
73+
$errors = [];
74+
if (isset($this->invalidLines[$file][$lineNumber])) {
75+
$errors = $this->invalidLines[$file][$lineNumber];
76+
}
77+
78+
return $errors;
79+
}
80+
81+
/**
82+
* return as true to include files, phpmnd only shows files with errors
83+
*/
84+
public function handleNotFoundFile()
85+
{
86+
return true;
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public static function getDescription()
93+
{
94+
return 'Parses the XML output of phpmnd (Magic Number Detection)';
95+
}
96+
}

src/Runners/generic.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
'phpmd' => 'PhpMdLoader',
4444
'phpmdStrict' => 'PhpMdLoaderStrict',
4545
'phpmnd' => 'PhpMndLoader',
46+
'phpmndXml' => 'PhpMndXmlLoader',
4647
'phpstan' => 'PhpStanLoader',
4748
'phpunit' => 'PhpUnitLoader',
4849
'pylint' => 'PylintLoader',

tests/PhpmndXmlDiffFilterTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace exussum12\CoverageChecker\tests;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use exussum12\CoverageChecker\PhpMndXmlLoader;
6+
7+
class PhpmndXmlDiffFilterTest extends TestCase
8+
{
9+
10+
public function testValidFiles()
11+
{
12+
$file = __DIR__ . "/fixtures/phpmnd.xml";
13+
$mnd = new PhpMndXmlLoader($file);
14+
$files = $mnd->parseLines();
15+
$expected = [
16+
'bin/test/test.php',
17+
'bin/test/test2.php',
18+
'tests/files/test_1.php',
19+
];
20+
21+
$this->assertSame($expected, $files);
22+
}
23+
24+
public function testShowsErrorOnLine()
25+
{
26+
$file = __DIR__ . "/fixtures/phpmnd.xml";
27+
$mnd = new PhpMndXmlLoader($file);
28+
$mnd->parseLines();
29+
30+
$this->assertNotEmpty(
31+
$mnd->getErrorsOnLine('bin/test/test.php', 3)
32+
);
33+
$this->assertEmpty(
34+
$mnd->getErrorsOnLine('bin/test/test.php', 1)
35+
);
36+
}
37+
}

tests/fixtures/phpmnd.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0"?>
2+
<phpmnd version="2.0.0" fileCount="15" errorCount="11"><files><file path="bin/test/test.php" errors="2"><entry line="3" start="14" end="15"><snippet><![CDATA[if ($length < 7) {]]></snippet><suggestions/></entry><entry line="4" start="11" end="13"><snippet><![CDATA[ return 12;]]></snippet><suggestions/></entry></file><file path="bin/test/test2.php" errors="2"><entry line="3" start="14" end="15"><snippet><![CDATA[if ($length < 7) {]]></snippet><suggestions/></entry><entry line="4" start="11" end="13"><snippet><![CDATA[ return 12;]]></snippet><suggestions/></entry></file><file path="tests/files/test_1.php" errors="7"><entry line="14" start="19" end="20"><snippet><![CDATA[ if ($input > 2) {]]></snippet><suggestions/></entry><entry line="15" start="16" end="18"><snippet><![CDATA[ return 15;]]></snippet><suggestions/></entry><entry line="18" start="25" end="27"><snippet><![CDATA[ for ($i = 3; $i <= 10; $i++) {]]></snippet><suggestions/></entry><entry line="20" start="17" end="18"><snippet><![CDATA[ case 5:]]></snippet><suggestions/></entry><entry line="26" start="29" end="30"><snippet><![CDATA[ round($input, $input > 7);]]></snippet><suggestions/></entry><entry line="31" start="29" end="31"><snippet><![CDATA[ 'adult' => $input > 18,]]></snippet><suggestions/></entry><entry line="50" start="13" end="15"><snippet><![CDATA[ return -2;]]></snippet><suggestions/></entry></file></files></phpmnd>

0 commit comments

Comments
 (0)