<commit_before><?php
namespace CsvMigrations;

use Cake\Core\Configure;
use Cake\Utility\Inflector;

trait CsvMigrationsTableTrait
{
    /**
     * Field parameters
     *
     * @var array
     */
    protected $_fieldParams = ['name', 'type', 'limit', 'required', 'non-searchable'];

    /**
     * Method that retrieves fields from csv file and returns them in associate array format.
     *
     * @return array
     */
    public function getFieldsDefinitions()
    {
        $path = Configure::readOrFail('CsvMigrations.migrations.path') . $this->alias() . DS;

        $csvFiles = $this->_getCsvFile($path);
        $csvData = $this->_getCsvData($csvFiles);

        $result = [];
        if (!empty($csvData)) {
            foreach ($csvData as $module => $fields) {
                foreach ($fields as $row) {
                    $field = array_combine($this->_fieldParams, $row);
                    $result[$field['name']] = $field;
                }
            }
        }

        return $result;
    }

    /**
     * Method that retrieves csv file path from specified directory.
     *
     * @param  string $path directory to search in
     * @return array        csv file path
     */
    protected function _getCsvFile($path)
    {
        $result = [];
        if (file_exists($path)) {
            $di = new \DirectoryIterator($path);
            foreach ($di as $fileInfo) {
                if ($fileInfo->isFile() && 'csv' === $fileInfo->getExtension()) {
                    $result[$this->alias()][] = $fileInfo->getPathname();
                }
            }
        }

        return $result;
    }

    /**
     * Method that sets current model table associations.
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    protected function _setAssociationsFromCsv(array $config)
    {
        $path = Configure::readOrFail('CsvMigrations.migrations.path');
        $csvFiles = $this->_getCsvFiles($path);
        $csvData = $this->_getCsvData($csvFiles);

        if (!empty($csvData)) {
            return;
        }

        $count = count($this->_fieldParams);

        foreach ($csvData as $module => $fields) {
            foreach ($fields as $row) {
                /*
                Skip if row is incomplete
                 */
                if ($count !== count($row)) {
                    continue;
                }

                $assocModule = $this->_getAssociatedModuleName($row[1]);
                /*
                Skip if not associated module name was found
                 */<commit_msg>skip if empty, not the opposite (task #1705)<commit_after><?php
namespace CsvMigrations;

use Cake\Core\Configure;
use Cake\Utility\Inflector;

trait CsvMigrationsTableTrait
{
    /**
     * Field parameters
     *
     * @var array
     */
    protected $_fieldParams = ['name', 'type', 'limit', 'required', 'non-searchable'];

    /**
     * Method that retrieves fields from csv file and returns them in associate array format.
     *
     * @return array
     */
    public function getFieldsDefinitions()
    {
        $path = Configure::readOrFail('CsvMigrations.migrations.path') . $this->alias() . DS;

        $csvFiles = $this->_getCsvFile($path);
        $csvData = $this->_getCsvData($csvFiles);

        $result = [];
        if (!empty($csvData)) {
            foreach ($csvData as $module => $fields) {
                foreach ($fields as $row) {
                    $field = array_combine($this->_fieldParams, $row);
                    $result[$field['name']] = $field;
                }
            }
        }

        return $result;
    }

    /**
     * Method that retrieves csv file path from specified directory.
     *
     * @param  string $path directory to search in
     * @return array        csv file path
     */
    protected function _getCsvFile($path)
    {
        $result = [];
        if (file_exists($path)) {
            $di = new \DirectoryIterator($path);
            foreach ($di as $fileInfo) {
                if ($fileInfo->isFile() && 'csv' === $fileInfo->getExtension()) {
                    $result[$this->alias()][] = $fileInfo->getPathname();
                }
            }
        }

        return $result;
    }

    /**
     * Method that sets current model table associations.
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    protected function _setAssociationsFromCsv(array $config)
    {
        $path = Configure::readOrFail('CsvMigrations.migrations.path');
        $csvFiles = $this->_getCsvFiles($path);
        $csvData = $this->_getCsvData($csvFiles);

        if (empty($csvData)) {
            return;
        }

        $count = count($this->_fieldParams);

        foreach ($csvData as $module => $fields) {
            foreach ($fields as $row) {
                /*
                Skip if row is incomplete
                 */
                if ($count !== count($row)) {
                    continue;
                }

                $assocModule = $this->_getAssociatedModuleName($row[1]);
                /*
                Skip if not associated module name was found
                 */
{'fraction_non_alphanumeric': 0.1288897426046869, 'fraction_numerical': 0.001152516327314637, 'mean_word_length': 1.695134575569358, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '2467379', 'n_tokens_mistral': 1451, 'n_tokens_neox': 1405, 'n_words': 343}