Skip to content

Directory exclusions do not work as expected when a single file name is passed to phpcs #1731

Closed
@toolstack

Description

@toolstack

GlotPress has been using phpcs in conjunction with TravisCI, we've created a phpcs.xml that includes several directory exclusions like:

<exclude-pattern>/tests/*</exclude-pattern>

However these do not function as expected when TravisCI runs against a PR.

Looking through the phpcs code, the shouldIgnorePath() method in /Filters/Filter.php, includes this code:

        if (is_dir($path) === true) {
            $ignorePatterns = $this->ignoreDirPatterns;
        } else {
            $ignorePatterns = $this->ignoreFilePatterns;
        }

Which checks to see if the path passed in to the function is a directory or a file and picks which exclusions to check (directory or file).

However when a file name is passed in to phpcs, like: (which is what happens on a PR with TravisCI)

phpcs --standard=phpcs.xml tests/phpunit/testcases/test_links.php

the directory exclusions are never checked as only the full path and filename are passed in to shouldIgnorePath().

As this individual file is not a directory, only the file ignore patterns are checked against and the filename.

This is easily observable by instead of using a directory pattern of /tests/*, just using /tests in the exclusion-pattern, which will get added as a file pattern.

A fix appears to be to change the above code to:

        if (is_dir($path) === true) {
            $ignorePatterns = $this->ignoreDirPatterns;
        } else {
            $ignorePatterns = array_merge($this->ignoreFilePatterns, $this->ignoreDirPatterns);
        }

Which ensures that when a file is checked, both file and directory patterns are used.

I can create a PR for the above if that makes sense.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions