Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ The ``--exclude-path`` option will exclude a path, which must be relative to the

The ``--exclude`` option will exclude a directory, which must be relative to the source, from the code analysis. Multiple values are allowed (e.g. --exclude=tests --exclude=examples).

The ``--extensions`` option lets you extend the code analysis. The provided extensions must be comma separated.

The ``--hint`` option will suggest replacements for magic numbers based on your codebase constants.

The ``--ignore-funcs`` option will exclude a list of comma separated functions from the code analysis, when using the "argument" extension. Defaults to `intval`, `floatval`, `strval`.
Expand All @@ -118,6 +116,11 @@ The ``--suffixes`` option will configure a comma separated list of valid source
The ``--whitelist`` option will only process the files listed in the file specified. This is useful for incremental analysis.

The ``--xml-output`` option will generate an report in an Xml format to the path specified by the option.

The ``--checkstyle-output`` option will generate a report in checkstyle xml format to the path specified by the option.

The ``--extensions`` option lets you extend the code analysis. The provided extensions must be comma separated.

**By default it analyses conditions, return statements, and switch cases.**
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line "**By default it analyses ~", was seemed to intended to add after --extensions . So I move "The --extensions ~" line.

ce6fe2e


#### Extensions
Expand Down
11 changes: 11 additions & 0 deletions src/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ protected function configure(): void
InputOption::VALUE_REQUIRED,
'Generate an XML output to the specified path'
)
->addOption(
'checkstyle-output',
null,
InputOption::VALUE_REQUIRED,
'Generate an checkstyle output to the specified path'
)
->addOption(
'whitelist',
null,
Expand Down Expand Up @@ -198,6 +204,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$xmlOutput->printData($output, $fileReportList, $hintList);
}

if ($input->getOption('checkstyle-output')) {
$checkStyleOutput = new Printer\CheckStyle($input->getOption('checkstyle-output'));
$checkStyleOutput->printData($output, $fileReportList, $hintList);
}

if ($output->getVerbosity() !== OutputInterface::VERBOSITY_QUIET) {
$output->writeln('');
$printer->printData($output, $fileReportList, $hintList);
Expand Down
57 changes: 57 additions & 0 deletions src/Printer/CheckStyle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Povils\PHPMND\Printer;

use Povils\PHPMND\FileReportList;
use Povils\PHPMND\HintList;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class CheckStyle
*
* @package Povils\PHPMND\Printer
*/
class CheckStyle implements Printer
{
/** @var string */
private $outputPath;

public function __construct(string $outputPath)
{
$this->outputPath = $outputPath;
}

public function printData(OutputInterface $output, FileReportList $fileReportList, HintList $hintList): void
{
$output->writeln('Generate checkstyle report output...');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if these should be printed to stderr ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just did find what you pointed :)
squizlabs/PHP_CodeSniffer#1612

$dom = new \DOMDocument('1.0', 'UTF-8');
$rootNode = $dom->createElement('checkstyle');

$total = 0;
foreach ($fileReportList->getFileReports() as $fileReport) {
$fileNode = $dom->createElement('file');
$fileNode->setAttribute('name', $fileReport->getFile()->getRelativePathname());

$entries = $fileReport->getEntries();
$total += count($entries);
foreach ($entries as $entry) {
$snippet = Util::getSnippet($fileReport->getFile()->getContents(), $entry['line'], $entry['value']);
$errorNode = $dom->createElement('error');
$errorNode->setAttribute('line', $entry['line']);
$errorNode->setAttribute('column', $snippet['col']);
$errorNode->setAttribute('severity', 'error');
$errorNode->setAttribute('message', sprintf('Magic number: %s', $entry['value']));

$fileNode->appendChild($errorNode);
}
$rootNode->appendChild($fileNode);
}

$dom->appendChild($rootNode);

$dom->save($this->outputPath);

$output->writeln('Total of Magic Numbers ' . $total);
$output->writeln('checkstyle XML generated at '. $this->outputPath);
}
}
35 changes: 35 additions & 0 deletions src/Printer/Util.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Povils\PHPMND\Printer;

/**
* @internal
*/
abstract class Util
{

/**
* Get the snippet and information about it
*
* @param string $content
* @param int $line
* @param int|string $text
* @return array{snippet: string, line: int, magic: int|string, col: false|int}
*/
final public static function getSnippet(string $content, int $line, $text): array
{
$content = str_replace(["\r\n", "\r"], "\n", $content);
$lines = explode("\n", $content);

$lineContent = array_slice($lines, $line-1, 1);
$lineContent = reset($lineContent);
$start = strpos($lineContent, (string) $text);

return [
'snippet' => $lineContent,
'line' => $line,
'magic' => $text,
'col' => $start
];
}
}
27 changes: 1 addition & 26 deletions src/Printer/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function printData(OutputInterface $output, FileReportList $fileReportLis

$total += count($entries);
foreach ($entries as $entry) {
$snippet = $this->getSnippet($fileReport->getFile()->getContents(), $entry['line'], $entry['value']);
$snippet = Util::getSnippet($fileReport->getFile()->getContents(), $entry['line'], $entry['value']);
$entryNode = $dom->createElement('entry');
$entryNode->setAttribute('line', $entry['line']);
$entryNode->setAttribute('start', $snippet['col']);
Expand Down Expand Up @@ -80,29 +80,4 @@ public function printData(OutputInterface $output, FileReportList $fileReportLis

$output->writeln('XML generated at '.$this->outputPath);
}

/**
* Get the snippet and information about it
*
* @param string $content
* @param int $line
* @param int|string $text
* @return array
*/
private function getSnippet(string $content, int $line, $text): array
{
$content = str_replace(array("\r\n", "\r"), "\n", $content);
$lines = explode("\n", $content);

$lineContent = array_slice($lines, $line-1, 1);
$lineContent = reset($lineContent);
$start = strpos($lineContent, $text.'');

return [
'snippet' => $lineContent,
'line' => $line,
'magic' => $text,
'col' => $start
];
}
}
74 changes: 74 additions & 0 deletions tests/Printer/CheckStyleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Povils\PHPMND\Tests\Printer;

use Povils\PHPMND\FileReport;
use Povils\PHPMND\FileReportList;
use Povils\PHPMND\HintList;
use Povils\PHPMND\Printer\CheckStyle;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Finder\SplFileInfo;

/**
* Class XmlTest
*
* @package Povils\PHPMND\Tests
*/
class CheckStyleTest extends TestCase
{
public function testEmpty() : void
{
$outputPath = tempnam(sys_get_temp_dir(), 'phpmnd_');

$xmlPrinter = new CheckStyle($outputPath);
$xmlPrinter->printData(new NullOutput(), new FileReportList(), new HintList());

$this->assertXmlStringEqualsXmlString(
<<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle />
XML
,
file_get_contents($outputPath)
);
}

public function testPrintData() : void
{
$testMagicNumber = 12;

$splFileInfo = $this->createMock(SplFileInfo::class);
$splFileInfo
->method('getRelativePathname')
->willReturn('Foo/Bar.php');
$splFileInfo
->method('getContents')
->willReturn(sprintf(
'$rootNode->setAttribute(\'fileCount\', count($fileReportList->getFileReports()) + %d);',
$testMagicNumber
));

$fileReport = new FileReport($splFileInfo);
$fileReport->addEntry(1, $testMagicNumber);
$fileReportList = new FileReportList();
$fileReportList->addFileReport($fileReport);

$outputPath = tempnam(sys_get_temp_dir(), 'phpmnd_');
$xmlPrinter = new CheckStyle($outputPath);
$xmlPrinter->printData(new NullOutput(), $fileReportList, new HintList());

$this->assertXmlStringEqualsXmlString(
<<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="Foo/Bar.php">
<error line="1" column="80" severity="error" message="Magic number: 12"/>
</file>
</checkstyle>
XML
,
file_get_contents($outputPath)
);
}
}
13 changes: 9 additions & 4 deletions tests/Printer/XmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testEmpty() : void
<?xml version="1.0"?>
<phpmnd version="%%PHPMND_VERSION%%" fileCount="0" errorCount="0"><files/></phpmnd>
XML
,
,
$outputPath
);
}
Expand All @@ -45,7 +45,10 @@ public function testPrintData() : void
->willReturn('Foo/Bar.php');
$splFileInfo
->method('getContents')
->willReturn(sprintf('$rootNode->setAttribute(\'fileCount\', count($fileReportList->getFileReports()) + %d);', $testMagicNumber));
->willReturn(sprintf(
'$rootNode->setAttribute(\'fileCount\', count($fileReportList->getFileReports()) + %d);',
$testMagicNumber
));

$fileReport = new FileReport($splFileInfo);
$fileReport->addEntry(1, $testMagicNumber);
Expand All @@ -66,7 +69,9 @@ public function testPrintData() : void
<files>
<file errors="1" path="Foo/Bar.php">
<entry end="82" line="1" start="80">
<snippet><![CDATA[$rootNode->setAttribute('fileCount', count($fileReportList->getFileReports()) + 12);]]></snippet>
<snippet>
<![CDATA[$rootNode->setAttribute('fileCount', count($fileReportList->getFileReports()) + 12);]]>
</snippet>
<suggestions>
<suggestion>Povils\PHPMND\Tests\Printer\XmlTest::WELL_KNOWN_MAGIC</suggestion>
</suggestions>
Expand All @@ -75,7 +80,7 @@ public function testPrintData() : void
</files>
</phpmnd>
XML
,
,
$outputPath
);
}
Expand Down