<commit_before><?php declare(strict_types=1);

namespace Symplify\Monorepo\Filesystem;

use Nette\Utils\FileSystem as NetteFileSystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

final class Filesystem
{
    /**
     * @var string[]
     */
    private const EXCLUDED_LOCAL_DIRS = ['packages', 'vendor', '.idea'];

    public function findMergedPackageFiles(string $directory): Finder
    {
        return Finder::create()
            ->files()
            ->in($directory)
            ->exclude(self::EXCLUDED_LOCAL_DIRS)
            // include .gitignore, .travis etc
            ->ignoreDotFiles(false);
    }

    /**
     * - find new files with finder
     * - copy to new directory
     */
    public function copyFinderFilesToDirectory(Finder $finder, string $directory): void
    {
        foreach ($finder->getIterator() as $fileInfo) {
            NetteFileSystem::copy($fileInfo->getRealPath(), $directory . '/' . $fileInfo->getRelativePathname());
        }
    }

    public function deleteDirectory(string $directory): void
    {
        $finder = Finder::create()
            ->in($directory)
            // include .gitignore, .travis.yml etc
            ->ignoreDotFiles(false);

        $this->deleteFilesInFinder($finder);

        NetteFileSystem::delete($directory);
    }

    public function deleteFilesInFinder(Finder $finder): void
    {
        $finder
            // sort from deepest to top to allow removal in same direction
            ->sort(function (SplFileInfo $firstFileInfo, SplFileInfo $secondFileInfo): bool {
                return strlen($firstFileInfo->getRealPath()) < strlen($secondFileInfo->getRealPath());
            });

        foreach ($finder->getIterator() as $fileInfo) {
            if ($fileInfo->isFile()) {
                unlink($fileInfo->getRealPath());
            }
        }

        foreach ($finder->getIterator() as $fileInfo) {
            if ($fileInfo->isDir()) {
                rmdir($fileInfo->getRealPath());
            }
        }<commit_msg>make Filesystem simpler<commit_after><?php declare(strict_types=1);

namespace Symplify\Monorepo\Filesystem;

use Nette\Utils\FileSystem as NetteFileSystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

final class Filesystem
{
    /**
     * @var string[]
     */
    private const EXCLUDED_LOCAL_DIRS = ['packages', 'vendor', '.idea'];

    public function findMergedPackageFiles(string $directory): Finder
    {
        return Finder::create()
            ->files()
            ->in($directory)
            ->exclude(self::EXCLUDED_LOCAL_DIRS)
            // include .gitignore, .travis etc
            ->ignoreDotFiles(false);
    }

    /**
     * - find new files with finder(
     * - copy to new directory
     */
    public function copyFinderFilesToDirectory(Finder $finder, string $directory): void
    {
        foreach ($finder->getIterator() as $fileInfo) {
            NetteFileSystem::copy($fileInfo->getRealPath(), $directory . '/' . $fileInfo->getRelativePathname());
        }
    }

    public function deleteDirectory(string $directory): void
    {
        NetteFileSystem::delete($directory);
    }

    public function deleteFilesInFinder(Finder $finder): void
    {
        $finder
            // sort from deepest to top to allow removal in same direction
            ->sort(function (SplFileInfo $firstFileInfo, SplFileInfo $secondFileInfo): bool {
                return strlen($firstFileInfo->getRealPath()) < strlen($secondFileInfo->getRealPath());
            });

        foreach ($finder->getIterator() as $fileInfo) {
            if ($fileInfo->isFile()) {
                unlink($fileInfo->getRealPath());
            }
        }

        foreach ($finder->getIterator() as $fileInfo) {
            if ($fileInfo->isDir()) {
                rmdir($fileInfo->getRealPath());
            }
        }
{'fraction_non_alphanumeric': 0.12659520163348648, 'fraction_numerical': 0.0005104645227156713, 'mean_word_length': 2.332482993197279, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5621013', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1063, 'n_words': 225}