Skip to content

Commit

Permalink
PHPMD and PHPCS #14
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Feb 18, 2017
1 parent 52fff2c commit 308d12e
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 90 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

Changelog of Git Changelog.

## Unreleased
### GitHub [#14](https://github.com/tomasbjerre/violations-lib/issues/14) Support php checkers

**PHPMD and PHPCS**


[7a14ff2c22834d4](https://github.com/tomasbjerre/violations-lib/commit/7a14ff2c22834d4) Tomas Bjerre *2017-02-18 19:57:29*


## 1.22
### No issue

**Finding findbugsmessages and correcting codenarc**

* Was finding findbugs messages xml incorrectly in classpath.
* Was not handling codenarc reports with empty line numbers.

[60d19fedbefdf85](https://github.com/tomasbjerre/violations-lib/commit/60d19fedbefdf85) Tomas Bjerre *2017-02-16 20:51:05*

**doc**


[84e505343e497a2](https://github.com/tomasbjerre/violations-lib/commit/84e505343e497a2) Tomas Bjerre *2017-02-07 05:42:45*


## 1.21
### GitHub [#12](https://github.com/tomasbjerre/violations-lib/issues/12) Support pydocstyle (old pep257)

Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ This is a library for parsing report files from static code analysis.

It supports:
* [_AndoidLint_](http://developer.android.com/tools/help/lint.html)
* [_Checkstyle_](http://checkstyle.sourceforge.net/) ([_ESLint_](https://github.com/sindresorhus/grunt-eslint) with `format: 'checkstyle'`)
* [_Checkstyle_](http://checkstyle.sourceforge.net/)
*[_ESLint_](https://github.com/sindresorhus/grunt-eslint) with `format: 'checkstyle'`.
*[_PHPCS_](https://github.com/squizlabs/PHP_CodeSniffer) with `phpcs api.php --report=checkstyle`.
* [_CodeNarc_](http://codenarc.sourceforge.net/)
* [_CPD_](http://pmd.sourceforge.net/pmd-4.3.0/cpd.html)
* [_CPPLint_](https://github.com/theandrewdavis/cpplint)
* [_CPPCheck_](http://cppcheck.sourceforge.net/)
* [_CSSLint_](https://github.com/CSSLint/csslint)
* [_Findbugs_](http://findbugs.sourceforge.net/)
* [_Flake8_](http://flake8.readthedocs.org/en/latest/) ([_Pep8_](https://github.com/PyCQA/pycodestyle), [_Mccabe_](https://pypi.python.org/pypi/mccabe), [_PyFlakes_](https://pypi.python.org/pypi/pyflakes))
* [_Flake8_](http://flake8.readthedocs.org/en/latest/)
* [_Pep8_](https://github.com/PyCQA/pycodestyle)
* [_Mccabe_](https://pypi.python.org/pypi/mccabe)
* [_PyFlakes_](https://pypi.python.org/pypi/pyflakes)
* [_FxCop_](https://en.wikipedia.org/wiki/FxCop)
* [_Gendarme_](http://www.mono-project.com/docs/tools+libraries/tools/gendarme/)
* [_JSHint_](http://jshint.com/)
Expand All @@ -23,6 +28,7 @@ It supports:
* [_PyDocStyle_](https://pypi.python.org/pypi/pydocstyle)
* [_PyLint_](https://www.pylint.org/)
* [_PMD_](https://pmd.github.io/)
* [_PHPPMD_](https://phpmd.org/) with `phpmd api.php xml ruleset.xml`.
* [_ReSharper_](https://www.jetbrains.com/resharper/)
* [_Simian_](http://www.harukizaemon.com/simian/)
* [_StyleCop_](https://stylecop.codeplex.com/)
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/se/bjurr/violations/lib/parsers/PMDParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.findIntegerAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getChunks;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getIntegerAttribute;
Expand All @@ -14,6 +15,7 @@

import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.util.Optional;

public class PMDParser implements ViolationsParser {

Expand All @@ -27,7 +29,7 @@ public List<Violation> parseReportOutput(String string) throws Exception {
for (String violationChunk : violationsChunks) {
Integer beginLine = getIntegerAttribute(violationChunk, "beginline");
Integer endLine = getIntegerAttribute(violationChunk, "endline");
Integer beginColumn = getIntegerAttribute(violationChunk, "begincolumn");
Optional<Integer> beginColumn = findIntegerAttribute(violationChunk, "begincolumn");
String rule = getAttribute(violationChunk, "rule").trim();
String ruleSet = getAttribute(violationChunk, "ruleset").trim();
String externalInfoUrl = getAttribute(violationChunk, "externalInfoUrl").trim();
Expand All @@ -39,7 +41,7 @@ public List<Violation> parseReportOutput(String string) throws Exception {
.setReporter(PMD)//
.setStartLine(beginLine)//
.setEndLine(endLine)//
.setColumn(beginColumn)//
.setColumn(beginColumn.orNull())//
.setFile(filename)//
.setSeverity(severity)//
.setRule(rule)//
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/se/bjurr/violations/lib/AccumulatedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ public void testThatViolationsCanBeFiltered() {
.withAtLeastSeverity(ERROR)//
.orderedBy(FILE)//
.violations())//
.hasSize(1);
.hasSize(7);

assertThat(violationsAccumulatedReporterApi//
.withAtLeastSeverity(WARN)//
.orderedBy(FILE)//
.violations())//
.hasSize(8);
.hasSize(14);

assertThat(violationsAccumulatedReporterApi//
.withAtLeastSeverity(INFO)//
.orderedBy(FILE)//
.violations())//
.hasSize(10);
.hasSize(16);
}

@Test
Expand Down
130 changes: 74 additions & 56 deletions src/test/java/se/bjurr/violations/lib/CheckstyleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,80 @@

public class CheckstyleTest {

@Test
public void testThatViolationsCanBeParsed() {
String rootFolder = getRootFolder();
@Test
public void testThatViolationsCanBeParsed() {
String rootFolder = getRootFolder();

List<Violation> actual = violationsReporterApi() //
.withPattern(".*/checkstyle/.*\\.xml$") //
.inFolder(rootFolder) //
.findAll(CHECKSTYLE) //
.violations();
List<Violation> actual = violationsReporterApi() //
.withPattern(".*/checkstyle/main\\.xml$") //
.inFolder(rootFolder) //
.findAll(CHECKSTYLE) //
.violations();

assertThat(actual)//
.containsExactly(//
violationBuilder()//
.setReporter(CHECKSTYLE)//
.setFile("/src/main/java/se/bjurr/violations/lib/example/MyClass.java")//
.setSource(null)//
.setStartLine(0)//
.setEndLine(0)//
.setColumn(null)//
.setMessage("Missing package-info.java file.")//
.setRule("com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck")//
.setSeverity(ERROR)//
.build(), //
violationBuilder()//
.setReporter(CHECKSTYLE)//
.setFile("/src/main/java/se/bjurr/violations/lib/example/MyClass.java")//
.setSource(null)//
.setStartLine(9)//
.setEndLine(9)//
.setColumn(10)//
.setMessage("Must have at least one statement.")//
.setRule("com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck")//
.setSeverity(INFO)//
.build(), //
violationBuilder()//
.setReporter(CHECKSTYLE)//
.setFile("/src/main/java/se/bjurr/violations/lib/example/OtherClass.java")//
.setSource(null)//
.setStartLine(10)//
.setEndLine(10)//
.setColumn(31)//
.setMessage("Must have at least one statement.")//
.setRule("com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck")//
.setSeverity(INFO)//
.build(), //
violationBuilder()//
.setReporter(CHECKSTYLE)//
.setFile("/src/main/java/se/bjurr/violations/lib/example/OtherClass.java")//
.setSource(null)//
.setStartLine(26)//
.setEndLine(26)//
.setColumn(3)//
.setMessage("Boolean expression complexity is 8 (max allowed is 1).")//
.setRule("com.puppycrawl.tools.checkstyle.checks.metrics.BooleanExpressionComplexityCheck")//
.setSeverity(WARN)//
.build()//
);
}
assertThat(actual)//
.containsExactly(//
violationBuilder()//
.setReporter(CHECKSTYLE)//
.setFile("/src/main/java/se/bjurr/violations/lib/example/MyClass.java")//
.setSource(null)//
.setStartLine(0)//
.setEndLine(0)//
.setColumn(null)//
.setMessage("Missing package-info.java file.")//
.setRule("com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck")//
.setSeverity(ERROR)//
.build(), //
violationBuilder()//
.setReporter(CHECKSTYLE)//
.setFile("/src/main/java/se/bjurr/violations/lib/example/MyClass.java")//
.setSource(null)//
.setStartLine(9)//
.setEndLine(9)//
.setColumn(10)//
.setMessage("Must have at least one statement.")//
.setRule("com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck")//
.setSeverity(INFO)//
.build(), //
violationBuilder()//
.setReporter(CHECKSTYLE)//
.setFile("/src/main/java/se/bjurr/violations/lib/example/OtherClass.java")//
.setSource(null)//
.setStartLine(10)//
.setEndLine(10)//
.setColumn(31)//
.setMessage("Must have at least one statement.")//
.setRule("com.puppycrawl.tools.checkstyle.checks.blocks.EmptyBlockCheck")//
.setSeverity(INFO)//
.build(), //
violationBuilder()//
.setReporter(CHECKSTYLE)//
.setFile("/src/main/java/se/bjurr/violations/lib/example/OtherClass.java")//
.setSource(null)//
.setStartLine(26)//
.setEndLine(26)//
.setColumn(3)//
.setMessage("Boolean expression complexity is 8 (max allowed is 1).")//
.setRule(
"com.puppycrawl.tools.checkstyle.checks.metrics.BooleanExpressionComplexityCheck")//
.setSeverity(WARN)//
.build()//
);
}

@Test
public void testThatPHPViolationsCanBeParsed() {
String rootFolder = getRootFolder();

List<Violation> actual = violationsReporterApi() //
.withPattern(".*/checkstyle/phpcheckstyle\\.xml$") //
.inFolder(rootFolder) //
.findAll(CHECKSTYLE) //
.violations();

assertThat(actual)//
.hasSize(6);

assertThat(actual.get(0).getMessage())//
.isEqualTo("Missing file doc comment");
}
}
71 changes: 44 additions & 27 deletions src/test/java/se/bjurr/violations/lib/PMDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,48 @@

public class PMDTest {

@Test
public void testThatViolationsCanBeParsed() {
String rootFolder = getRootFolder();

List<Violation> actual = violationsReporterApi() //
.withPattern(".*/pmd/.*\\.xml$") //
.inFolder(rootFolder) //
.findAll(PMD) //
.violations();

assertThat(actual)//
.hasSize(4);

assertThat(actual.get(0).getFile())//
.isEqualTo("/src/main/java/se/bjurr/violations/lib/example/MyClass.java");
assertThat(actual.get(0).getMessage())//
.startsWith("Empty Code http://")//
.doesNotContain("CDATA");
assertThat(actual.get(0).getStartLine())//
.isEqualTo(9);
assertThat(actual.get(0).getEndLine())//
.isEqualTo(11);
assertThat(actual.get(0).getRule().get())//
.isEqualTo("EmptyIfStmt");
assertThat(actual.get(0).getSeverity())//
.isEqualTo(WARN);
}
@Test
public void testThatViolationsCanBeParsed() {
String rootFolder = getRootFolder();

List<Violation> actual = violationsReporterApi() //
.withPattern(".*/pmd/main\\.xml$") //
.inFolder(rootFolder) //
.findAll(PMD) //
.violations();

assertThat(actual)//
.hasSize(4);

assertThat(actual.get(0).getFile())//
.isEqualTo("/src/main/java/se/bjurr/violations/lib/example/MyClass.java");
assertThat(actual.get(0).getMessage())//
.startsWith("Empty Code http://")//
.doesNotContain("CDATA");
assertThat(actual.get(0).getStartLine())//
.isEqualTo(9);
assertThat(actual.get(0).getEndLine())//
.isEqualTo(11);
assertThat(actual.get(0).getRule().get())//
.isEqualTo("EmptyIfStmt");
assertThat(actual.get(0).getSeverity())//
.isEqualTo(WARN);
}

@Test
public void testThatPHPMDViolationsCanBeParsed() {
String rootFolder = getRootFolder();

List<Violation> actual = violationsReporterApi() //
.withPattern(".*/pmd/phpmd\\.xml$") //
.inFolder(rootFolder) //
.findAll(PMD) //
.violations();

assertThat(actual)//
.hasSize(2);

assertThat(actual.get(0).getFile())//
.isEqualTo("/home/bjerre/workspace/pull-request-notifier-for-stash/api.php");
}
}
11 changes: 11 additions & 0 deletions src/test/resources/checkstyle/phpcheckstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="2.5.1">
<file name="/home/bjerre/workspace/pull-request-notifier-for-stash/api.php">
<error line="2" column="1" severity="error" message="Missing file doc comment" source="PEAR.Commenting.FileComment.Missing"/>
<error line="2" column="1" severity="error" message="Missing class doc comment" source="PEAR.Commenting.ClassComment.Missing"/>
<error line="4" column="20" severity="error" message="Private member variable &quot;FOO&quot; must be prefixed with an underscore" source="PEAR.NamingConventions.ValidVariableName.PrivateNoUnderscore"/>
<error line="5" column="13" severity="error" message="Private member variable &quot;i&quot; must be prefixed with an underscore" source="PEAR.NamingConventions.ValidVariableName.PrivateNoUnderscore"/>
<error line="6" column="13" severity="error" message="Private member variable &quot;j&quot; must be prefixed with an underscore" source="PEAR.NamingConventions.ValidVariableName.PrivateNoUnderscore"/>
<error line="7" column="12" severity="error" message="Missing function doc comment" source="PEAR.Commenting.FunctionComment.Missing"/>
</file>
</checkstyle>
11 changes: 11 additions & 0 deletions src/test/resources/pmd/phpmd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<pmd version="@project.version@" timestamp="2017-02-18T19:43:19+00:00">
<file name="/home/bjerre/workspace/pull-request-notifier-for-stash/api.php">
<violation beginline="4" endline="4" rule="UnusedPrivateField" ruleset="Unused Code Rules" externalInfoUrl="http://phpmd.org/rules/unusedcode.html#unusedprivatefield" priority="3">
Avoid unused private fields such as '$FOO'.
</violation>
<violation beginline="5" endline="5" rule="UnusedPrivateField" ruleset="Unused Code Rules" externalInfoUrl="http://phpmd.org/rules/unusedcode.html#unusedprivatefield" priority="3">
Avoid unused private fields such as '$i'.
</violation>
</file>
</pmd>

0 comments on commit 308d12e

Please sign in to comment.