Skip to content

Walk up directory tree to find php-cs-fixer config#227

Closed
benmatselby wants to merge 1 commit intomainfrom
project-path
Closed

Walk up directory tree to find php-cs-fixer config#227
benmatselby wants to merge 1 commit intomainfrom
project-path

Conversation

@benmatselby
Copy link
Owner

@benmatselby benmatselby commented Feb 26, 2026

Resolves #226

Summary

The Fixer command previously set its working directory to the target file's immediate parent directory. This meant php-cs-fixer could miss project-level config files (e.g. .php-cs-fixer.php) when editing files in nested subdirectories. This PR adds a find_config_dir method that walks up the directory tree from the target file looking for a recognised php-cs-fixer config file, and uses that directory as the working directory. If no config file is found, it falls back to the previous behaviour.

Changes

  • phpcs.py: Added CONFIG_FILES class constant listing the four config file names php-cs-fixer recognises (.php-cs-fixer.php, .php-cs-fixer.dist.php, .php_cs, .php_cs.dist)
  • phpcs.py: Added Fixer.find_config_dir() method that walks up from a starting directory to the filesystem root, checking for any of the config files at each level
  • phpcs.py: Updated Fixer.execute() to use find_config_dir() to determine the working directory instead of always using the target file's parent directory
  • tests/test_phpcs.py: Updated existing test_fixer_sets_working_dir test to mock os.path.isfile (renamed to test_fixer_sets_working_dir_to_file_dir_when_no_config)
  • tests/test_phpcs.py: Added test_fixer_sets_working_dir_to_config_dir integration-style test
  • tests/test_phpcs.py: Added TestFixerFindConfigDir test class with 6 tests covering: no config found, config in start dir, config in parent dir, dist config, legacy config, and priority ordering

Instead of using the target file's directory as the working directory,
walk up the directory tree to find a php-cs-fixer config file
(.php-cs-fixer.php, .php-cs-fixer.dist.php, .php_cs, .php_cs.dist).
This ensures php-cs-fixer picks up project-level configuration even
when editing files in nested subdirectories.
@benmatselby
Copy link
Owner Author

Might come back to this at a later date

@benmatselby benmatselby deleted the project-path branch February 28, 2026 10:55
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.

Since last update (7.2.0) phpcs create .php-cs-fixer.dist file everywhere

1 participant