Skip to content
This repository has been archived by the owner on May 25, 2020. It is now read-only.

Commit

Permalink
Merge pull request #5 from morozov/exclude-pattern
Browse files Browse the repository at this point in the history
Implemented support for the "exclude-pattern" parameter
  • Loading branch information
morozov authored Mar 2, 2019
2 parents b7bd346 + 4264c0e commit 30116d6
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 48 deletions.
35 changes: 0 additions & 35 deletions src/Filter.php

This file was deleted.

33 changes: 24 additions & 9 deletions src/Iterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace DiffSniffer;

use EmptyIterator;
use IteratorIterator;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Files\DummyFile;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Filters\Filter as BaseFilter;
use PHP_CodeSniffer\Filters\Filter;
use PHP_CodeSniffer\Ruleset;
use RecursiveArrayIterator;
use Traversable;
use const DIRECTORY_SEPARATOR;
use function iterator_to_array;
use function str_replace;

/**
* Changeset iterator
Expand Down Expand Up @@ -47,15 +49,28 @@ public function __construct(Traversable $files, Changeset $changeSet, Ruleset $r

public function getIterator() : Traversable
{
$it = new IteratorIterator($this->files);
$it = new Filter($it, new BaseFilter(
new RecursiveArrayIterator(
new EmptyIterator()
),
'',
// PHP_CodeSniffer expects file paths to contain the native directory separator on Windows when matching them
// against the exclude pattern but Git and GitHub REST API will return forward slashes regardless of the OS
if (DIRECTORY_SEPARATOR === '\\') {
$it = (function () : iterable {
foreach ($this->files as $file) {
yield str_replace('/', DIRECTORY_SEPARATOR, $file);
}
})();
} else {
$it = new IteratorIterator($this->files);
}

$it = new RecursiveArrayIterator(
iterator_to_array($it)
);

$it = new Filter(
$it,
"\0",
$this->config,
$this->ruleSet
));
);

foreach ($it as $path) {
yield $this->createFile(
Expand Down
11 changes: 8 additions & 3 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DiffSniffer\Command;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use const DIRECTORY_SEPARATOR;

class ApplicationTest extends TestCase
{
Expand All @@ -16,9 +17,9 @@ class ApplicationTest extends TestCase
*/
public function testUseCase(string $useCase, int $expectedExitCode)
{
$dir = __DIR__ . '/fixtures/' . $useCase;
$dir = __DIR__ . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . $useCase;
$changeset = new FixtureChangeset($dir);
chdir($dir . '/tree');
chdir($dir . DIRECTORY_SEPARATOR . 'tree');

/** @var Command|MockObject $command */
$command = $this->createMock(Command::class);
Expand All @@ -27,7 +28,7 @@ public function testUseCase(string $useCase, int $expectedExitCode)
->willReturn($changeset);
$app = new Application();

$expectedOutput = file_get_contents($dir . '/output.txt');
$expectedOutput = file_get_contents($dir . DIRECTORY_SEPARATOR . 'output.txt');
$expectedOutput = str_replace("\n", PHP_EOL, $expectedOutput);

$this->expectOutputString($expectedOutput);
Expand All @@ -47,6 +48,10 @@ public static function useCaseProvider()
'excluded-rule-cache',
0,
],
'exclude-pattern' => [
'exclude-pattern',
1,
],
];
}
}
3 changes: 2 additions & 1 deletion tests/FixtureChangeset.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace DiffSniffer\Tests;

use DiffSniffer\Changeset;
use const DIRECTORY_SEPARATOR;

/**
* Fixture changeset
Expand All @@ -29,7 +30,7 @@ public function __construct(string $dir)
*/
public function getDiff() : string
{
return file_get_contents($this->dir . '/changeset.diff');
return file_get_contents($this->dir . DIRECTORY_SEPARATOR . 'changeset.diff');
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/exclude-pattern/changeset.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
diff --git a/main.php b/main.php
`--- /dev/null
+++ b/main.php
@@ -0,0 +1,5 @@
+<?php
+
+if ( ! isset($foo)) {
+ $foo = 'bar';
+}
diff --git a/excluded/excluded.php b/excluded/excluded.php
`--- /dev/null
+++ b/excluded/excluded.php
@@ -0,0 +1,5 @@
+<?php
+
+if ( ! isset($foo)) {
+ $foo = 'bar';
+}
10 changes: 10 additions & 0 deletions tests/fixtures/exclude-pattern/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

FILE: main.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
3 | ERROR | [x] Expected 0 spaces after opening bracket; 1 found
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

5 changes: 5 additions & 0 deletions tests/fixtures/exclude-pattern/tree/excluded/excluded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

if ( ! isset($foo)) {
$foo = 'bar';
}
5 changes: 5 additions & 0 deletions tests/fixtures/exclude-pattern/tree/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

if ( ! isset($foo)) {
$foo = 'bar';
}
5 changes: 5 additions & 0 deletions tests/fixtures/exclude-pattern/tree/phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<ruleset>
<exclude-pattern>excluded/*</exclude-pattern>
<rule ref="PSR2"/>
</ruleset>

0 comments on commit 30116d6

Please sign in to comment.