Skip to content

Exclude the files under a directory that an exclude pattern matches - #7175

Merged
staabm merged 2 commits into
phpstan:2.3.xfrom
SanderMuller:exclude-directory-patterns
Nov 26, 2026
Merged

staabm merged 2 commits into
phpstan:2.3.xfrom
SanderMuller:exclude-directory-patterns

Conversation

@SanderMuller

Copy link
Copy Markdown
Contributor

An exclude path that names a directory excludes the files under it. Since 2.0 that is true only for a literal path. A pattern has to match the whole file path, so themes/*/node_modules/ and themes/*/node_modules exclude nothing under those directories. Only themes/*/node_modules/* works.

This change makes a pattern behave like a literal path. When a pattern matches a directory, it now also excludes the files under that directory. FileExcluder adds <pattern>/* for each pattern that does not end with *. A pattern that ends with a separator can only name a directory, so it gets only that form.

themes/*/node_modules still does not exclude themes/foo/node_modules-old/, so this does not bring back the implicit wildcard that phpstan/phpstan#10299 removed.

A config from phpstan/phpstan#9554 uses this form:

excludePaths:
	analyseAndScan:
		- ./wp-content/themes/*/node_modules/

I rebuilt that layout with a file in themes/a/node_modules/pkg/. 2.3.x analyses that file, and this PR does not.

ignoreErrors uses FileExcluder for path and paths too, so a pattern there now also matches the files under a directory it names. That is again how a literal directory path already works. Before, such an entry matched only a file with exactly that name.

The new FileExcluderTest cases with an /*-less directory pattern fail without the change. I could not run the Windows cases, because I have no Windows machine.

🤖 Generated with Claude Code

@staabm
staabm force-pushed the exclude-directory-patterns branch from 69bcc77 to 4394c03 Compare November 26, 2026 06:05
@staabm
staabm requested a balanced review from Copilot November 26, 2026 06:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@staabm

staabm commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

I think this looks good.

remarks:

  • Each pattern not ending in * now costs two fnmatch calls per file.
  • do we have test coverage for a case where a pattern like /project/src/*Test matches the file /project/src/FooTest ?

@SanderMuller

Copy link
Copy Markdown
Contributor Author

Thanks. Both points are in 9743e72.

The second call now runs only when the path contains the literal text that the pattern ends with, followed by a separator. A file under a directory that the pattern matches always contains that text. On Windows the check is off, because there the match ignores case.

On 20,000 paths, best of 15, with four patterns from the WordPress config in phpstan/phpstan#9554 and wp-content/*/tests/*Fixtures:

2.3.x 4394c03 9743e72
the four with their trailing /, as in the issue 35 ms 39 ms 16 ms
the four without the trailing / 35 ms 61 ms 40 ms

A pattern that ends with / now needs only the guarded call, so that config is faster than on 2.3.x. Without the /, the first form's own call is still there. A fuzzer compared the guarded version with the same logic without the guard on 378,560 paths and patterns, bracket expressions included, with no difference.

I added /project/src/*Test against the file /project/src/FooTest, and also against /project/src/FooTests/Bar.php, which must stay included. There are cases with ? and a bracket expression as well.

@staabm
staabm force-pushed the exclude-directory-patterns branch from 9743e72 to f796e6d Compare November 26, 2026 12:45

@staabm staabm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. I think we should wait until CI stablizes a bit before merging.

SanderMuller and others added 2 commits November 26, 2026 15:03
A literal exclude path that names a directory excludes the files under
it. A pattern had to match the whole file path, so themes/*/node_modules/
and themes/*/node_modules excluded nothing under those directories. Each
pattern that does not end with * now also gets the form <pattern>/*.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A file under a directory that the pattern matches contains the literal
text the pattern ends with, followed by a separator. str_contains() on
that text rules out most paths before the second fnmatch() call. The
check is off on Windows, which matches case-insensitively.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@staabm
staabm force-pushed the exclude-directory-patterns branch from f796e6d to 8359339 Compare November 26, 2026 14:03
@staabm
staabm merged commit bb705f0 into phpstan:2.3.x Nov 26, 2026
891 of 917 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants