From 5ffbc5d15da8752dba2b6a8786ea69a1035f72e6 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Thu, 16 Aug 2018 23:06:30 -0700 Subject: [PATCH] Implemented support for the "exclude-pattern" parameter As of https://github.com/squizlabs/PHP_CodeSniffer/issues/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. --- composer.json | 2 +- src/Filter.php | 35 ------------------- src/Iterator.php | 18 +++++----- tests/ApplicationTest.php | 4 +++ tests/fixtures/exclude-pattern/changeset.diff | 18 ++++++++++ tests/fixtures/exclude-pattern/output.txt | 10 ++++++ .../tree/excluded/excluded.php | 5 +++ tests/fixtures/exclude-pattern/tree/main.php | 5 +++ tests/fixtures/exclude-pattern/tree/phpcs.xml | 6 ++++ 9 files changed, 59 insertions(+), 44 deletions(-) delete mode 100644 src/Filter.php create mode 100644 tests/fixtures/exclude-pattern/changeset.diff create mode 100644 tests/fixtures/exclude-pattern/output.txt create mode 100644 tests/fixtures/exclude-pattern/tree/excluded/excluded.php create mode 100644 tests/fixtures/exclude-pattern/tree/main.php create mode 100644 tests/fixtures/exclude-pattern/tree/phpcs.xml diff --git a/composer.json b/composer.json index c5cec02..fb51fec 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Filter.php b/src/Filter.php deleted file mode 100644 index eadb914..0000000 --- a/src/Filter.php +++ /dev/null @@ -1,35 +0,0 @@ -setAccessible(true); - - $this->callback = function () use ($it, $filter, $re) { - return $re->invoke($filter, $it->current()); - }; - } - - public function accept() : bool - { - return ($this->callback)(); - } -} diff --git a/src/Iterator.php b/src/Iterator.php index eefbfd7..43a5267 100644 --- a/src/Iterator.php +++ b/src/Iterator.php @@ -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; @@ -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( diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index 286350f..404bfb9 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -46,6 +46,10 @@ public static function useCaseProvider() 'excluded-rule-cache', 0, ], + 'exclude-pattern' => [ + 'exclude-pattern', + 1, + ], ]; } } diff --git a/tests/fixtures/exclude-pattern/changeset.diff b/tests/fixtures/exclude-pattern/changeset.diff new file mode 100644 index 0000000..78901f5 --- /dev/null +++ b/tests/fixtures/exclude-pattern/changeset.diff @@ -0,0 +1,18 @@ +diff --git a/main.php b/main.php +`--- /dev/null ++++ b/main.php +@@ -0,0 +1,5 @@ ++ + + + excluded/* + +