PHP-CS-Fixer: how to ignore folders without permission

285 views Asked by At

I would like to format the code of my PHP project running a composer script called format. Searching in internet I found PHP-CS-Fixer. It looks like it works the same way prettier does for JavaScript. But even make the config to ignore folders and files without permission, It still trying to read these entries.

Following the documentation of the project, I installed via composer using:

composer require --dev friendsofphp/php-cs-fixer

And created a config file (.php-cs-fixer.php) like documented in this repository:

<?php

$finder = (new PhpCsFixer\Finder())
    ->exclude(['.git', '.docker', 'vendor'])
    ->ignoreUnreadableDirs()
    ->in(__DIR__);

return (new PhpCsFixer\Config())
    ->setRules([
        '@PER-CS' => true,
        '@PHP82Migration' => true,
    ])
    ->setFinder($finder);

I put a script in composer.json in scripts section:

...
"scripts": {
    "format": "PHP_CS_FIXER_ALLOW_XDEBUG=true vendor/bin/php-cs-fixer fix ."
  } 
...

Then, I run:

composer format

So, the errors that return are:

In RecursiveDirectoryIterator.php line 110:
RecursiveDirectoryIterator::__construct(/var/www/.docker/mysql/dbdata/sys): Failed to open directory: Permission denied  

In RecursiveDirectoryIterator.php line 43:
RecursiveDirectoryIterator::__construct(/var/www/.docker/mysql/dbdata/sys): Failed to open directory: Permission denied 

Reading the source code of PHP-CS-Fixer and Finder class of Symfony Framework, the class that the formatter use to iterate over files and directories, I noticed via XDebug that the flag $ignoreUnreadableDirs don't change to true passing the config via ->ignoreUnreadableDirs() and If I change it directly in class to true, the formatter works.

To avoid change in place the code in Symfony class and lost it every time I run in composer install, I tried to inject a Custom Finder creating a custom Class changing that flag to true, but PHP-CS-Fixer still using the Finder of Symfony and continue to trigger the described errors.

Should I open a issue in PHP-CS-Fixer to inform that this flag is not working as intended or is there another through config to resolve this?

PS.: The version of fixer I use at the moment is v3.41.1.

0

There are 0 answers