symfonyphpcsphp-cs-fixersymfony-finder

Exception when directory does not exist


The issue is that if watching multiple directories via the Finder class. If one does not exist, the Finder will throw an exception and the fixer will die.

$finder = Finder::create()
    ->in([
        __DIR__ . '/web/app/mu-plugins/ys-*',
        __DIR__ . '/web/app/plugins/ys-*'
    ])
    ->name('*.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

enter image description here

Is there a possibility of adding an option to "skip if directory does not exist" so it doesn't kill the fixer if one of the directories does not exist?


Solution

  • I would use an array_filter beforehand with a simple glob() function to check if the directories/files actually do exist. Glob will return an array of files/folders if any are found.

    If directories/files do exist, pass them to the Finder:

    $directories = [
        __DIR__ . '/web/app/mu-plugins/ys-*',
        __DIR__ . '/web/app/plugins/ys-*'
    ];
    
    $checkDirs = array_filter($directories, static function ($dir){
        return !empty(glob($dir));
    });
    
    $finder = Finder::create()
    ->in($checkDirs)
    ->name('*.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);