Skip to content

Commit

Permalink
Add option to configure followLinks of finder (#1703)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
sabbelasichon and actions-user committed Jan 24, 2022
1 parent e414c51 commit 084ac39
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,9 @@ final class Option
* @var string
*/
final public const PARALLEL_SYSTEM_ERROR_COUNT_LIMIT = 'parallel-system-error-count-limit';

/**
* @var string
*/
final public const FOLLOW_SYMLINKS = 'follow-symlinks';
}
19 changes: 17 additions & 2 deletions src/FileSystem/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Rector\Core\FileSystem;

use Rector\Caching\UnchangedFilesFilter;
use Rector\Core\Configuration\Option;
use Rector\Core\Util\StringUtils;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\Skipper\SkipCriteriaResolver\SkippedPathsResolver;
use Symplify\SmartFileSystem\FileSystemFilter;
use Symplify\SmartFileSystem\Finder\FinderSanitizer;
Expand Down Expand Up @@ -35,7 +37,8 @@ public function __construct(
private readonly FinderSanitizer $finderSanitizer,
private readonly FileSystemFilter $fileSystemFilter,
private readonly SkippedPathsResolver $skippedPathsResolver,
private readonly UnchangedFilesFilter $unchangedFilesFilter
private readonly UnchangedFilesFilter $unchangedFilesFilter,
private readonly ParameterProvider $parameterProvider
) {
}

Expand Down Expand Up @@ -74,13 +77,16 @@ private function findInDirectories(array $directories, array $suffixes): array
}

$finder = Finder::create()
->followLinks()
->files()
// skip empty files
->size('> 0')
->in($directories)
->sortByName();

if ($this->hasFollowLinks()) {
$finder->followLinks();
}

if ($suffixes !== []) {
$suffixesPattern = $this->normalizeSuffixesToPattern($suffixes);
$finder->name($suffixesPattern);
Expand Down Expand Up @@ -156,4 +162,13 @@ private function normalizeForFnmatch(string $path): string

return $path;
}

private function hasFollowLinks(): bool
{
if (! $this->parameterProvider->hasParameter(Option::FOLLOW_SYMLINKS)) {
return true;
}

return $this->parameterProvider->provideBoolParameter(Option::FOLLOW_SYMLINKS);
}
}
16 changes: 16 additions & 0 deletions tests/FileSystem/FilesFinder/FilesFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ protected function setUp(): void
$this->filesFinder = $this->getService(FilesFinder::class);
}

public function testWithoutFollowingSymlinks(): void
{
$this->bootFromConfigFiles([__DIR__ . '/config/disable_follow_symlinks.php']);
$filesFinder = $this->getService(FilesFinder::class);
$foundFiles = $filesFinder->findInDirectoriesAndFiles([__DIR__ . '/SourceWithSymlinks'], ['txt']);
$this->assertCount(1, $foundFiles);
}

public function testWithFollowingSymlinks(): void
{
$this->bootFromConfigFiles([__DIR__ . '/config/enable_follow_symlinks.php']);
$filesFinder = $this->getService(FilesFinder::class);
$foundFiles = $filesFinder->findInDirectoriesAndFiles([__DIR__ . '/SourceWithSymlinks'], ['txt']);
$this->assertCount(2, $foundFiles);
}

/**
* @dataProvider provideData()
*/
Expand Down
1 change: 1 addition & 0 deletions tests/FileSystem/FilesFinder/Source/folder1/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Test
1 change: 1 addition & 0 deletions tests/FileSystem/FilesFinder/SourceWithSymlinks/folder2
11 changes: 11 additions & 0 deletions tests/FileSystem/FilesFinder/config/disable_follow_symlinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::FOLLOW_SYMLINKS, false);
};
11 changes: 11 additions & 0 deletions tests/FileSystem/FilesFinder/config/enable_follow_symlinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::FOLLOW_SYMLINKS, true);
};

0 comments on commit 084ac39

Please sign in to comment.