Skip to content

Issues are sorted for all formatters #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 7, 2020
Merged
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: 6 additions & 1 deletion src/Application/Console/Formatters/Checkstyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use NunoMaduro\PhpInsights\Application\Console\Contracts\Formatter;
use NunoMaduro\PhpInsights\Domain\Contracts\HasDetails;
use NunoMaduro\PhpInsights\Domain\Details;
use NunoMaduro\PhpInsights\Domain\DetailsComparator;
use NunoMaduro\PhpInsights\Domain\Insights\Insight;
use NunoMaduro\PhpInsights\Domain\Insights\InsightCollection;
use RuntimeException;
Expand Down Expand Up @@ -42,6 +43,7 @@ public function format(
throw new RuntimeException('To use checkstyle format install simplexml extension.');
}
$checkstyle = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><checkstyle/>');
$detailsComparator = new DetailsComparator();

foreach ($metrics as $metricClass) {
/** @var Insight $insight */
Expand All @@ -50,8 +52,11 @@ public function format(
continue;
}

$details = $insight->getDetails();
usort($details, $detailsComparator);

/** @var Details $detail */
foreach ($insight->getDetails() as $detail) {
foreach ($details as $detail) {
$fileName = $this->getFileName($detail, $dir);

if (isset($checkstyle->file) && (string) $checkstyle->file->attributes()['name'] === $fileName) {
Expand Down
3 changes: 3 additions & 0 deletions src/Application/Console/Formatters/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use NunoMaduro\PhpInsights\Domain\Contracts\FileLinkFormatter;
use NunoMaduro\PhpInsights\Domain\Contracts\HasDetails;
use NunoMaduro\PhpInsights\Domain\Details;
use NunoMaduro\PhpInsights\Domain\DetailsComparator;
use NunoMaduro\PhpInsights\Domain\Insights\ForbiddenSecurityIssues;
use NunoMaduro\PhpInsights\Domain\Insights\InsightCollection;
use NunoMaduro\PhpInsights\Domain\LinkFormatter\NullFileLinkFormatter;
Expand Down Expand Up @@ -285,6 +286,7 @@ private function issues(
string $dir
): self {
$previousCategory = null;
$detailsComparator = new DetailsComparator();

foreach ($metrics as $metricClass) {
foreach ($insightCollection->allFrom(new $metricClass()) as $insight) {
Expand Down Expand Up @@ -318,6 +320,7 @@ private function issues(
}

$details = $insight->getDetails();
usort($details, $detailsComparator);
$totalDetails = count($details);

if (! $this->style->getOutput()->isVerbose()) {
Expand Down
7 changes: 6 additions & 1 deletion src/Application/Console/Formatters/GithubAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use NunoMaduro\PhpInsights\Domain\Contracts\HasDetails;
use NunoMaduro\PhpInsights\Domain\Contracts\Insight;
use NunoMaduro\PhpInsights\Domain\Details;
use NunoMaduro\PhpInsights\Domain\DetailsComparator;
use NunoMaduro\PhpInsights\Domain\Insights\InsightCollection;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -51,6 +52,7 @@ public function format(InsightCollection $insightCollection, string $dir, array
// Call The Console Formatter to get summary and recap,
// not issues by passing an empty array for metrics.
$this->decorated->format($insightCollection, $dir, []);
$detailsComparator = new DetailsComparator();

$errors = [];

Expand All @@ -59,8 +61,11 @@ public function format(InsightCollection $insightCollection, string $dir, array
continue;
}

$details = $insight->getDetails();
usort($details, $detailsComparator);

/** @var Details $detail */
foreach ($insight->getDetails() as $detail) {
foreach ($details as $detail) {
if (! $detail->hasFile()) {
continue;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Application/Console/Formatters/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use InvalidArgumentException;
use NunoMaduro\PhpInsights\Application\Console\Contracts\Formatter;
use NunoMaduro\PhpInsights\Domain\Contracts\HasDetails;
use NunoMaduro\PhpInsights\Domain\DetailsComparator;
use NunoMaduro\PhpInsights\Domain\Insights\Insight;
use NunoMaduro\PhpInsights\Domain\Insights\InsightCollection;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -75,6 +76,7 @@ private function issues(
array $metrics
): array {
$data = [];
$detailsComparator = new DetailsComparator();

foreach ($metrics as $metricClass) {
$category = explode('\\', $metricClass);
Expand All @@ -100,8 +102,11 @@ private function issues(
continue;
}

$details = $insight->getDetails();
usort($details, $detailsComparator);

/** @var \NunoMaduro\PhpInsights\Domain\Details $detail */
foreach ($insight->getDetails() as $detail) {
foreach ($details as $detail) {
$current[] = array_filter([
'title' => $insight->getTitle(),
'insightClass' => $insight->getInsightClass(),
Expand Down
49 changes: 49 additions & 0 deletions src/Domain/DetailsComparator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace NunoMaduro\PhpInsights\Domain;

/**
* @internal
*/
final class DetailsComparator
{
public function __invoke(Details $first, Details $second): int
{
$comparisons = [
$this->fileComparison($first, $second),
$this->lineComparison($first, $second),
$this->functionComparison($first, $second),
$this->messageComparison($first, $second),
];

foreach ($comparisons as $comparison) {
if ($comparison !== 0) {
return $comparison;
}
}

return 0;
}

private function fileComparison(Details $first, Details $second): int
{
return ($first->hasFile() ? $first->getFile() : null) <=> ($second->hasFile() ? $second->getFile() : null);
}

private function lineComparison(Details $first, Details $second): int
{
return ($first->hasLine() ? $first->getLine() : null) <=> ($second->hasLine() ? $second->getLine() : null);
}

private function functionComparison(Details $first, Details $second): int
{
return ($first->hasFunction() ? $first->getFunction() : null) <=> ($second->hasFunction() ? $second->getFunction() : null);
}

private function messageComparison(Details $first, Details $second): int
{
return ($first->hasMessage() ? $first->getMessage() : null) <=> ($second->hasMessage() ? $second->getMessage() : null);
}
}
72 changes: 72 additions & 0 deletions tests/Domain/DetailsComparatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Tests\Domain;

use NunoMaduro\PhpInsights\Domain\Details;
use NunoMaduro\PhpInsights\Domain\DetailsComparator;
use Tests\TestCase;

final class DetailsComparatorTest extends TestCase
{
/** @var \NunoMaduro\PhpInsights\Domain\DetailsComparator */
private $comparator;

protected function setUp(): void
{
parent::setUp();
$this->comparator = new DetailsComparator();
}

public function testComparisonWithEmptyDetails(): void
{
self::assertEquals(0, ($this->comparator)(new Details(), new Details()));
}

public function testComparisonWithDifferentFileNames(): void
{
$first = (new Details())->setFile('src/First.php');
$second = (new Details())->setFile('src/Second.php');

self::assertEquals(-1, ($this->comparator)($first, $second));
self::assertEquals(1, ($this->comparator)($second, $first));
}

public function testComparisonWithSameFileName(): void
{
$first = (new Details())
->setFile('src/Foo.php')
->setLine(10);

$second = (new Details())
->setFile('src/Foo.php')
->setLine(20);

self::assertEquals(-1, ($this->comparator)($first, $second));
self::assertEquals(1, ($this->comparator)($second, $first));
}

public function testSortWithComparator(): void
{
$a = $this->makeDetails('app/App/Admin/Filters/ByRuleTemplate.php', 25, 'Unused parameter $request.');
$b = $this->makeDetails('app/App/Dashboard/Controllers/UserController.php', 54, 'Unused parameter $user');
$c = $this->makeDetails('app/Support/Rules/HexColor.php', 9, 'Unused parameter $attribute.');
$d = $this->makeDetails('app/Support/Rules/DomainName.php', 27, 'Unused parameter $values.');
$e = $this->makeDetails('app/Support/Rules/DomainName.php', 17, 'Unused parameter $attribute.');
$f = $this->makeDetails('app/Domain/User/Policies/UserPolicy.php', 14, 'Unused parameter $admin.');

$array = [$a, $b, $c, $d, $e, $f];
usort($array, $this->comparator);

self::assertEquals([$a, $b, $f, $e, $d, $c], $array);
}

private function makeDetails(string $file, int $line, string $message): Details
{
return (new Details())
->setFile($file)
->setLine($line)
->setMessage($message);
}
}