From 8ff8b473fb82f8424c8f9780e24fb843fc0f3124 Mon Sep 17 00:00:00 2001 From: Felipe Figueroa Date: Wed, 30 Dec 2020 07:40:37 -0300 Subject: renderer for phpmd using checkstyle --- .reviewdog.yml | 28 ++++++++------- Renderer.php | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 12 deletions(-) create mode 100644 Renderer.php diff --git a/.reviewdog.yml b/.reviewdog.yml index 7a3f065a..a4b3f5da 100644 --- a/.reviewdog.yml +++ b/.reviewdog.yml @@ -4,20 +4,24 @@ runner: level: info psalm: - cmd: vendor/bin/psalm --config=psalm.xml --diff --diff-methods --output-format=checkstyle + cmd: vendor/bin/psalm --config=psalm.xml --diff --output-format=checkstyle level: info - name: checkstyle csfixer: - cmd: vendor/bin/php-cs-fixer fix --config=.php_cs.php --cache-file=.build/phpcs/csfixer.cache --format=checkstyle --dry-run --diff - level: info - name: checkstyle - - phpcs: - cmd: phpcs --standard=.phpcs.xml --parallel=2 --cache=.build/phpcs/php-cs.cache --report=checkstyle src/* + cmd: vendor/bin/php-cs-fixer fix --config=.php_cs.php --cache-file=.build/phpcs/csfixer.cache --format=checkstyle --dry-run --diff level: info - name: checkstyle + format: checkstyle + + phpmd: + cmd: phpmd src/classes Renderer .phpmd.xml + level: info + format: checkstyle + + #phpcs: + #cmd: phpcs --standard=.phpcs.xml --parallel=2 --cache=.build/phpcs/php-cs.cache --report=checkstyle src/* + #level: info + #name: checkstyle - eslint: - cmd: node_modules/.bin/eslint --ext js --ignore-path .eslintignore assets - level: info + #eslint: + #cmd: node_modules/.bin/eslint --ext js --ignore-path .eslintignore assets + #level: info diff --git a/Renderer.php b/Renderer.php new file mode 100644 index 00000000..d3c59538 --- /dev/null +++ b/Renderer.php @@ -0,0 +1,108 @@ +getWriter()->write(''); + $this->getWriter()->write(\PHP_EOL); + } + + /** + * This method will be called when the engine has finished the source analysis + * phase. + * + * @param \PHPMD\Report $report + */ + public function renderReport(Report $report): void + { + $writer = $this->getWriter(); + $writer->write(''); + $writer->write(\PHP_EOL); + + foreach ($report->getRuleViolations() as $violation) { + $fileName = \str_replace(__DIR__ . \DIRECTORY_SEPARATOR, '', $violation->getFileName()); + + if ($this->fileName !== $fileName) { + // Not first file + if (null !== $this->fileName) { + $writer->write(' ' . \PHP_EOL); + } + // Store current file name + $this->fileName = $fileName; + + $writer->write(' ' . \PHP_EOL); + } + + $rule = $violation->getRule(); + + $writer->write(' write(' line="' . $violation->getBeginLine() . '"'); + $writer->write(' endline="' . $violation->getEndLine() . '"'); + $writer->write(\sprintf(' severity="%s"', 2 < $rule->getPriority() ? 'warning' : 'error')); + $writer->write(\sprintf(' message="%s (%s, %s) "', \htmlspecialchars($violation->getDescription()), $rule->getName(), $rule->getRuleSetName())); + + $this->maybeAdd('package', $violation->getNamespaceName()); + $this->maybeAdd('externalInfoUrl', $rule->getExternalInfoUrl()); + $this->maybeAdd('function', $violation->getFunctionName()); + $this->maybeAdd('class', $violation->getClassName()); + $this->maybeAdd('method', $violation->getMethodName()); + //$this->_maybeAdd('variable', $violation->getVariableName()); + + $writer->write(' />' . \PHP_EOL); + } + + // Last file and at least one violation + if (null !== $this->fileName) { + $writer->write(' ' . \PHP_EOL); + } + + foreach ($report->getErrors() as $error) { + $writer->write(' '); + $writer->write($error->getFile()); + $writer->write('' . \PHP_EOL); + } + + $writer->write('' . \PHP_EOL); + } + + /** + * This method will write a xml attribute named $attr to the output + * when the given $value is not an empty string and is not null. + * + * @param string $attr the xml attribute name + * @param string $value the attribute value + */ + private function maybeAdd($attr, $value): void + { + if (null === $value || '' === \trim($value)) { + return; + } + $this->getWriter()->write(' ' . $attr . '="' . $value . '"'); + } +} -- cgit v1.2.3