<commit_before><?php
/**
 * Synchronizer Library
 * Copyright (C) 2014 IceFlame.net
 *
 * Permission to use, copy, modify, and/or distribute this software for
 * any purpose with or without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all copies.
 *
 * @package  FlameCore\Synchronizer
 * @version  0.1-dev
 * @link     http://www.flamecore.org
 * @license  http://opensource.org/licenses/ISC ISC License
 */

namespace FlameCore\Synchronizer\Files;

use FlameCore\Synchronizer\Files\Source\FilesSourceInterface;
use FlameCore\Synchronizer\Files\Target\FilesTargetInterface;

/**
 * The FilesComparer class
 *
 * @author   <NAME> <<EMAIL>>
 */
class FilesComparer
{
    /**
     * @var array
     */
    protected $outdatedFiles = array();

    /**
     * @var array
     */
    protected $missingDirs = array();

    /**
     * @var array
     */
    protected $missingFiles = array();

    /**
     * @var array
     */
    protected $obsoleteDirs = array();

    /**
     * @var array
     */
    protected $obsoleteFiles = array();

    /**
     * @param \FlameCore\Synchronizer\Files\Source\FilesSourceInterface $source
     * @param \FlameCore\Synchronizer\Files\Target\FilesTargetInterface $target
     * @param array|bool $exclude
     */
    public function __construct(FilesSourceInterface $source, FilesTargetInterface $target, $exclude = false)
    {
        $sourceFiles = $source->getFilesList($exclude);
        $targetFiles = $target->getFilesList();

        ksort($sourceFiles);
        ksort($targetFiles);

        foreach ($sourceFiles as $dir => $files) {
            if (!isset($targetFiles[$dir])) {
                $this->missingDirs[] = $dir;
            }

            foreach ($files as $file => $pathname) {
                if (isset($targetFiles[$dir][$file])) {
                    if ($source->getFileHash($pathname) != $target->getFileHash($pathname)) {
                        $this->outdatedFiles[] = $pathname;
                    }
                } else {
                    $this->missingFiles[] = $pathname;
                }
            }
        }

        foreach ($targetFiles as $dir => $files) {
            foreach ($files as $file => $pathname) {
                if (!isset($sourceFiles[$dir][$file])) {
                    $this->obsoleteFiles[] = $pathname;
                }
            }

            if (!isset($sourceFiles[$dir])) {
                $this->obsoleteDirs[] = $dir;
            }
        }
    }

    /**
     * @return array
     */
    public function getOutdatedFiles()
    {
        return $this->outdatedFiles;
    }

    /**
     * @return array
     */
    public function getMissingDirs()
    {
        return $this->missingDirs;
    }

    /**
     * @return array
     */
    public function getMissingFiles()
    {
        return $this->missingFiles;
    }

    /**
     * @return array
     */
    public function getObsoleteDirs()
    {
        return $this->obsoleteDirs;
    }

    /**
     * @return array
     */
    public function getObsoleteFiles()
    {
        return $this->obsoleteFiles;
    }
}<commit_msg>Improved class FilesComparer<commit_after><?php
/**
 * Synchronizer Library
 * Copyright (C) 2014 IceFlame.net
 *
 * Permission to use, copy, modify, and/or distribute this software for
 * any purpose with or without fee is hereby granted, provided that the
 * above copyright notice and this permission notice appear in all copies.
 *
 * @package  FlameCore\Synchronizer
 * @version  0.1-dev
 * @link     http://www.flamecore.org
 * @license  http://opensource.org/licenses/ISC ISC License
 */

namespace FlameCore\Synchronizer\Files;

use FlameCore\Synchronizer\Files\Source\FilesSourceInterface;
use FlameCore\Synchronizer\Files\Target\FilesTargetInterface;

/**
 * The FilesComparer class
 *
 * @author   <NAME> <<EMAIL>>
 */
class FilesComparer
{
    /**
     * @var \FlameCore\Synchronizer\Files\Source\FilesSourceInterface
     */
    protected $source;

    /**
     * @var \FlameCore\Synchronizer\Files\Target\FilesTargetInterface
     */
    protected $target;

    /**
     * @var array
     */
    protected $outdatedFiles = array();

    /**
     * @var array
     */
    protected $missingDirs = array();

    /**
     * @var array
     */
    protected $missingFiles = array();

    /**
     * @var array
     */
    protected $obsoleteDirs = array();

    /**
     * @var array
     */
    protected $obsoleteFiles = array();

    /**
     * @param \FlameCore\Synchronizer\Files\Source\FilesSourceInterface $source
     * @param \FlameCore\Synchronizer\Files\Target\FilesTargetInterface $target
     * @param array|bool $exclude
     */
    public function __construct(FilesSourceInterface $source, FilesTargetInterface $target, $exclude = false)
    {
        $this->source = $source;
        $this->target = $target;

        $sourceFiles = $source->getFilesList($exclude);
        $targetFiles = $target->getFilesList();

        ksort($sourceFiles);
        ksort($targetFiles);

        $this->scan($sourceFiles, $targetFiles);
    }

    /**
     * @return array
     */
    public function getOutdatedFiles()
    {
        return $this->outdatedFiles;
    }

    /**
     * @return array
     */
    public function getMissingDirs()
    {
        return $this->missingDirs;
    }

    /**
     * @return array
     */
    public function getMissingFiles()
    {
        return $this->missingFiles;
    }

    /**
     * @return array
     */
    public function getObsoleteDirs()
    {
        return $this->obsoleteDirs;
    }

    /**
     * @return array
     */
    public function getObsoleteFiles()
    {
        return $this->obsoleteFiles;
    }

    /**
     * @param array $sourceFiles
     * @param array $targetFiles
     */
    protected function scan(array $sourceFiles, array $targetFiles)
    {
        foreach ($sourceFiles as $dir => $files) {
            if (!isset($targetFiles[$dir])) {
                $this->missingDirs[] = $dir;
            }

            foreach ($files as $file => $pathname) {
                if (isset($targetFiles[$dir][$file])) {
                    if ($this->compare($pathname)) {
                        $this->outdatedFiles[] = $pathname;
                    }
                } else {
                    $this->missingFiles[] = $pathname;
                }
            }
        }

        foreach ($targetFiles as $dir => $files) {
            foreach ($files as $file => $pathname) {
                if (!isset($sourceFiles[$dir][$file])) {
                    $this->obsoleteFiles[] = $pathname;
                }
            }

            if (!isset($sourceFiles[$dir])) {
                $this->obsoleteDirs[] = $dir;
            }
        }
    }

    /**
     * @param string $file
     * @return bool
     */
    protected function compare($file)
    {
        return $this->source->getFileHash($file) !== $this->target->getFileHash($file);
    }
}
{'fraction_non_alphanumeric': 0.13302817908739809, 'fraction_numerical': 0.0017164926333857818, 'mean_word_length': 1.9328859060402686, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7281094', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1971, 'n_words': 416}