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

Commit

Permalink
Implemented support for the "exclude-pattern" parameter
Browse files Browse the repository at this point in the history
As of squizlabs/PHP_CodeSniffer#1488, Filter::accept() doesn't strictly check the real path of the files, so intstead of using reflection and calling its protected methods, we can use the filter itself.

Closes #4.
  • Loading branch information
morozov committed Aug 28, 2018
1 parent 544801d commit 5ffbc5d
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 44 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"require": {
"jean85/pretty-package-versions": "^1.0.3",
"morozov/bootstrap": "^1.1",
"squizlabs/php_codesniffer": "^3.1",
"squizlabs/php_codesniffer": "^3.3",
"php": "^7.1"
},
"require-dev": {
Expand Down
35 changes: 0 additions & 35 deletions src/Filter.php

This file was deleted.

18 changes: 10 additions & 8 deletions src/Iterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace DiffSniffer;

use EmptyIterator;
use function iterator_to_array;
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;
Expand Down Expand Up @@ -48,14 +48,16 @@ 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()
),
'',
$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
4 changes: 4 additions & 0 deletions tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public static function useCaseProvider()
'excluded-rule-cache',
0,
],
'exclude-pattern' => [
'exclude-pattern',
1,
],
];
}
}
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';
}
6 changes: 6 additions & 0 deletions tests/fixtures/exclude-pattern/tree/phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<ruleset>
<rule ref="PSR2">
<exclude-pattern>excluded/*</exclude-pattern>
</rule>
</ruleset>

0 comments on commit 5ffbc5d

Please sign in to comment.