From a125a50b896611b4938b2f3d85fafa167857d3ac Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 8 Apr 2026 13:05:50 +0000 Subject: [PATCH] fix: align configSearch file priority with PHP_CodeSniffer's own auto-detection order The confFileNames array had .phpcs.xml.dist before phpcs.xml, but PHP_CodeSniffer itself searches in this order: .phpcs.xml > phpcs.xml > .phpcs.xml.dist > phpcs.xml.dist This only matters when a project has two different config files in the same directory (e.g. both phpcs.xml and .phpcs.xml.dist), but the extension was silently choosing the wrong one. Fix: reorder to match PHPCS Config.php: https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/src/Config.php Also adds 3 new unit tests that document and lock in the correct priority. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- extension.js | 4 +++- test/unit.test.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/extension.js b/extension.js index 6457e71..9c78ece 100644 --- a/extension.js +++ b/extension.js @@ -111,8 +111,10 @@ class PHPCBF { const workspaceRoot = folder ? folder.uri.fsPath : null; const filePath = document.fileName; if (this.configSearch && workspaceRoot !== null && filePath !== undefined) { + // Priority matches PHP_CodeSniffer's own auto-detection order: + // https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/src/Config.php const confFileNames = [ - '.phpcs.xml', '.phpcs.xml.dist', 'phpcs.xml', 'phpcs.xml.dist', + '.phpcs.xml', 'phpcs.xml', '.phpcs.xml.dist', 'phpcs.xml.dist', 'phpcs.ruleset.xml', 'ruleset.xml', ]; diff --git a/test/unit.test.js b/test/unit.test.js index 0fb759e..88a25e8 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -103,4 +103,39 @@ describe("findFiles", () => { const result = findFiles(path.join(tmpRoot, "single"), ".", "phpcs.xml"); assert.equal(result, expected); }); + + // Verify that the PHPCS auto-detection priority is respected: + // .phpcs.xml > phpcs.xml > .phpcs.xml.dist > phpcs.xml.dist + test("PHPCS priority: .phpcs.xml beats phpcs.xml when both exist", () => { + const expected = mkFile("priority1", "src", ".phpcs.xml"); + mkFile("priority1", "src", "phpcs.xml"); + const result = findFiles( + path.join(tmpRoot, "priority1"), + "src", + [".phpcs.xml", "phpcs.xml", ".phpcs.xml.dist", "phpcs.xml.dist"] + ); + assert.equal(result, expected); + }); + + test("PHPCS priority: phpcs.xml beats .phpcs.xml.dist when both exist", () => { + const expected = mkFile("priority2", "src", "phpcs.xml"); + mkFile("priority2", "src", ".phpcs.xml.dist"); + const result = findFiles( + path.join(tmpRoot, "priority2"), + "src", + [".phpcs.xml", "phpcs.xml", ".phpcs.xml.dist", "phpcs.xml.dist"] + ); + assert.equal(result, expected); + }); + + test("PHPCS priority: .phpcs.xml.dist beats phpcs.xml.dist when both exist", () => { + const expected = mkFile("priority3", "src", ".phpcs.xml.dist"); + mkFile("priority3", "src", "phpcs.xml.dist"); + const result = findFiles( + path.join(tmpRoot, "priority3"), + "src", + [".phpcs.xml", "phpcs.xml", ".phpcs.xml.dist", "phpcs.xml.dist"] + ); + assert.equal(result, expected); + }); });