Skip to content

Commit

Permalink
fix exclude ForbiddenGlobals sniff (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
50bhan authored Mar 8, 2022
1 parent fb676aa commit bbcc738
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Domain/Insights/ForbiddenGlobals.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function getDetails(): array
$details = [];

foreach ($this->collector->getGlobalVariableAccesses() as $file => $global) {
if ($this->shouldSkipFile($file)) {
$filePath = current(explode(':', $file));
if ($this->shouldSkipFile($filePath)) {
continue;
}

Expand Down
39 changes: 35 additions & 4 deletions tests/Domain/Insights/ForbiddenGlobalsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace Tests\Domain\Insights;

use NunoMaduro\PhpInsights\Application\Console\Formatters\PathShortener;
use PHPUnit\Framework\TestCase;
use NunoMaduro\PhpInsights\Domain\Insights\ForbiddenGlobals;
use NunoMaduro\PhpInsights\Domain\Analyser;
use NunoMaduro\PhpInsights\Domain\Metrics\Code\Classes;
use Tests\TestCase;

final class ForbiddenGlobalsTest extends TestCase
{
public function testFileHasNoGlobals() : void
public function testFileHasNoGlobals(): void
{
$files = [
__DIR__ . '/Fixtures/LittleToComplexClass.php',
Expand All @@ -27,7 +28,7 @@ public function testFileHasNoGlobals() : void
self::assertEmpty($insight->getDetails());
}

public function testHasOneGlobalUsage() : void
public function testHasOneGlobalUsage(): void
{
$files = [
__DIR__ . '/Fixtures/FileWithGlobals.php',
Expand All @@ -50,7 +51,7 @@ public function testHasOneGlobalUsage() : void
self::assertEquals('FileWithGlobals.php:3', $file);
}

public function testHasMultipleGlobalsUsage() : void
public function testHasMultipleGlobalsUsage(): void
{
$files = [
__DIR__ . '/Fixtures/FileWithMultipleGlobals.php',
Expand All @@ -72,4 +73,34 @@ public function testHasMultipleGlobalsUsage() : void
$file = PathShortener::fileName($detail, $commonPath);
self::assertEquals('FileWithMultipleGlobals.php:3', $file);
}

public function testSkipFile(): void
{
$file = __DIR__ . '/Fixtures/FileWithMultipleGlobals.php';

$collection = $this->runAnalyserOnConfig(
[
'add' => [
Classes::class => [
ForbiddenGlobals::class,
],
],
'config' => [
ForbiddenGlobals::class => [
'exclude' => [$file],
],
],
],
[$file]
);

$errors = 0;
foreach ($collection->allFrom(new Classes()) as $insight) {
if ($insight->getInsightClass() === ForbiddenGlobals::class && $insight->hasIssue()) {
$errors++;
}
}

self::assertEquals(0, $errors);
}
}

0 comments on commit bbcc738

Please sign in to comment.