Skip to content

Commit f964d37

Browse files
committed
Merge remote-tracking branch 'origin/master' into v1-prep
2 parents 134dbcf + 136ea13 commit f964d37

File tree

7 files changed

+183
-0
lines changed

7 files changed

+183
-0
lines changed

CreatePhar.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
$phar->setStub(
2626
"#!/usr/bin/env php
2727
<?php
28+
Phar::mapPhar('$pharName');
2829
require 'phar://$pharName/src/Runners/generic.php';
2930
__HALT_COMPILER();"
3031
);

src/Loaders/PhpMndXml.php

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

src/Runners/generic.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
CoverageChecker\addExceptionHandler();
1414
CoverageChecker\findAutoLoader();
1515
$args = new CoverageChecker\ArgParser($argv);
16+
CoverageChecker\checkForVersion($args);
1617
CoverageChecker\checkCallIsCorrect($args);
1718

1819
try {
@@ -52,6 +53,7 @@
5253
'phpmd' => 'PhpMd',
5354
'phpmdStrict' => 'PhpMdStrict',
5455
'phpmnd' => 'PhpMnd',
56+
'phpmndXml' => 'PhpMndXml',
5557
'phpstan' => 'PhpStan',
5658
'phpunit' => 'PhpUnit',
5759
'pylint' => 'Pylint',

src/functions.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,14 @@ function adjustArgument($argument, $tabWidth)
180180
}
181181
return $argument;
182182
}
183+
184+
function checkForVersion(ArgParser $args)
185+
{
186+
try {
187+
$args->getArg("v");
188+
} catch (ArgumentNotFound $e) {
189+
return;
190+
}
191+
192+
throw new Exception('Version: 0.10.3-dev', 0);
193+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace exussum12\CoverageChecker\tests;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use exussum12\CoverageChecker\Loaders\PhpMndXml;
6+
7+
class PhpmndXmlDiffFilterTest extends TestCase
8+
{
9+
/** @var PhpMndXmlLoader */
10+
private $mnd;
11+
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
$file = __DIR__ . "/../fixtures/phpmnd.xml";
16+
$this->mnd = new PhpMndXml($file);
17+
}
18+
19+
public function testValidFiles()
20+
{
21+
$files = $this->mnd->parseLines();
22+
$expected = [
23+
'bin/test/test.php',
24+
'bin/test/test2.php',
25+
'tests/files/test_1.php',
26+
];
27+
28+
$this->assertSame($expected, $files);
29+
}
30+
31+
public function testShowsErrorOnLine()
32+
{
33+
$this->mnd->parseLines();
34+
35+
$this->assertNotEmpty(
36+
$this->mnd->getErrorsOnLine('bin/test/test.php', 3)
37+
);
38+
$this->assertEmpty(
39+
$this->mnd->getErrorsOnLine('bin/test/test.php', 1)
40+
);
41+
}
42+
43+
public function testFileNotFound()
44+
{
45+
$this->assertTrue($this->mnd->handleNotFoundFile());
46+
}
47+
}

tests/VersionOutputTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace exussum12\CoverageChecker\tests;
3+
4+
use Exception;
5+
use PHPUnit\Framework\TestCase;
6+
7+
/**
8+
* Ignored due to acceptance test needing to write values
9+
* @SuppressWarnings(PHPMD.Superglobals)
10+
*/
11+
class VersionOutputTest extends TestCase
12+
{
13+
14+
public function testValid()
15+
{
16+
$this->expectException(Exception::class);
17+
$GLOBALS['argv'] = [
18+
'diffFilter',
19+
'-v',
20+
];
21+
require(__DIR__ . "/../src/Runners/generic.php");
22+
}
23+
}

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)