From 308d12e0bb9eb38a53d6eac45a129bf2ebbd1fc9 Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Sat, 18 Feb 2017 20:57:29 +0100 Subject: [PATCH] PHPMD and PHPCS #14 --- CHANGELOG.md | 25 ++++ README.md | 10 +- .../violations/lib/parsers/PMDParser.java | 6 +- .../bjurr/violations/lib/AccumulatedTest.java | 6 +- .../bjurr/violations/lib/CheckstyleTest.java | 130 ++++++++++-------- .../java/se/bjurr/violations/lib/PMDTest.java | 71 ++++++---- .../resources/checkstyle/phpcheckstyle.xml | 11 ++ src/test/resources/pmd/phpmd.xml | 11 ++ 8 files changed, 180 insertions(+), 90 deletions(-) create mode 100644 src/test/resources/checkstyle/phpcheckstyle.xml create mode 100644 src/test/resources/pmd/phpmd.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ac63a81..0af88a20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index dfc2deef..9ba548e6 100644 --- a/README.md +++ b/README.md @@ -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/) @@ -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/) diff --git a/src/main/java/se/bjurr/violations/lib/parsers/PMDParser.java b/src/main/java/se/bjurr/violations/lib/parsers/PMDParser.java index 4a7d60c2..48f2ac7e 100644 --- a/src/main/java/se/bjurr/violations/lib/parsers/PMDParser.java +++ b/src/main/java/se/bjurr/violations/lib/parsers/PMDParser.java @@ -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; @@ -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 { @@ -27,7 +29,7 @@ public List 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 beginColumn = findIntegerAttribute(violationChunk, "begincolumn"); String rule = getAttribute(violationChunk, "rule").trim(); String ruleSet = getAttribute(violationChunk, "ruleset").trim(); String externalInfoUrl = getAttribute(violationChunk, "externalInfoUrl").trim(); @@ -39,7 +41,7 @@ public List parseReportOutput(String string) throws Exception { .setReporter(PMD)// .setStartLine(beginLine)// .setEndLine(endLine)// - .setColumn(beginColumn)// + .setColumn(beginColumn.orNull())// .setFile(filename)// .setSeverity(severity)// .setRule(rule)// diff --git a/src/test/java/se/bjurr/violations/lib/AccumulatedTest.java b/src/test/java/se/bjurr/violations/lib/AccumulatedTest.java index 46e36eba..a97dacce 100644 --- a/src/test/java/se/bjurr/violations/lib/AccumulatedTest.java +++ b/src/test/java/se/bjurr/violations/lib/AccumulatedTest.java @@ -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 diff --git a/src/test/java/se/bjurr/violations/lib/CheckstyleTest.java b/src/test/java/se/bjurr/violations/lib/CheckstyleTest.java index f3c49b6d..54a8dd59 100644 --- a/src/test/java/se/bjurr/violations/lib/CheckstyleTest.java +++ b/src/test/java/se/bjurr/violations/lib/CheckstyleTest.java @@ -17,62 +17,80 @@ public class CheckstyleTest { - @Test - public void testThatViolationsCanBeParsed() { - String rootFolder = getRootFolder(); + @Test + public void testThatViolationsCanBeParsed() { + String rootFolder = getRootFolder(); - List actual = violationsReporterApi() // - .withPattern(".*/checkstyle/.*\\.xml$") // - .inFolder(rootFolder) // - .findAll(CHECKSTYLE) // - .violations(); + List 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 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"); + } } diff --git a/src/test/java/se/bjurr/violations/lib/PMDTest.java b/src/test/java/se/bjurr/violations/lib/PMDTest.java index c6db5510..ee1e4b8a 100644 --- a/src/test/java/se/bjurr/violations/lib/PMDTest.java +++ b/src/test/java/se/bjurr/violations/lib/PMDTest.java @@ -14,31 +14,48 @@ public class PMDTest { - @Test - public void testThatViolationsCanBeParsed() { - String rootFolder = getRootFolder(); - - List 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 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 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"); + } } diff --git a/src/test/resources/checkstyle/phpcheckstyle.xml b/src/test/resources/checkstyle/phpcheckstyle.xml new file mode 100644 index 00000000..4b9f3d03 --- /dev/null +++ b/src/test/resources/checkstyle/phpcheckstyle.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/src/test/resources/pmd/phpmd.xml b/src/test/resources/pmd/phpmd.xml new file mode 100644 index 00000000..e8adfdf1 --- /dev/null +++ b/src/test/resources/pmd/phpmd.xml @@ -0,0 +1,11 @@ + + + + + Avoid unused private fields such as '$FOO'. + + + Avoid unused private fields such as '$i'. + + +