<commit_before><?php
namespace pulledbits\ActiveRecord\SQL;

class EntityType
{
    private $schema;
    private $entityTypeIdentifier;

    private $identifier = [];
    private $requiredAttributeIdentifiers = [];
    private $references = [];

    public function __construct(\pulledbits\ActiveRecord\Schema $schema, $tableIdentifier)
    {
        $this->schema = $schema;
        $this->entityTypeIdentifier = $tableIdentifier;

        $indexes = $schema->listIndexesForTable($tableIdentifier)->fetchAll();
        foreach ($indexes as $index) {
            if ($index['Key_name'] === 'PRIMARY') {
                $this->identifier[] = $index['Column_name'];
            }
        }

        $columns = $schema->listColumnsForTable($tableIdentifier)->fetchAll();
        foreach ($columns as $column) {
            if ($column['Extra'] === 'auto_increment') {
                continue;
            } elseif ($column['Null'] === 'NO') {
                $this->requiredAttributeIdentifiers[] = $column['Field'];
            }
        }

        $foreignKeys = $schema->listForeignKeys($tableIdentifier)->fetchAll();
        foreach ($foreignKeys as $foreignKey) {
            $this->addForeignKeyConstraint($foreignKey['CONSTRAINT_NAME'], $foreignKey['COLUMN_NAME'], $foreignKey['REFERENCED_TABLE_NAME'], $foreignKey['REFERENCED_COLUMN_NAME']);
        }
    }

    public function primaryKey(array $values) {
        $sliced = [];
        foreach ($values as $key => $value) {
            if (in_array($key, $this->identifier, true)) {
                $sliced[$key] = $value;
            }
        }
        return $sliced;
    }

    public function calculateMissingValues(array $values) : array {
        $missing = [];
        foreach ($this->requiredAttributeIdentifiers as $requiredColumnIdentifier) {
            if (array_key_exists($requiredColumnIdentifier, $values) === false) {
                $missing[] = $requiredColumnIdentifier;
                break;
            } elseif ($values[$requiredColumnIdentifier] === null) {
                $missing[] = $requiredColumnIdentifier;
                break;
            }
        }
        return $missing;
    }

    private function addForeignKeyConstraint(string $constraintName, string $columnName, string $referencedTableName, string $referencedColumnName) {
        $fkIdentifier = join('', array_map('ucfirst', explode('_', $constraintName)));
        if (array_key_exists($fkIdentifier, $this->references)) {
            $this->references[$fkIdentifier]['conditions'][$referencedColumnName] = $columnName;
        } else {
            $this->references[$fkIdentifier] = [
                'entityTypeIdentifier' => $referencedTableName,
                'conditions' => [$referencedColumnName => $columnName]
            ];
        }
    }

    public function update(array $values, array $conditions) : int
    {
        return $this->schema->update($this->entityTypeIdentifier, $values, $conditions);
    }


    public function delete(array $conditions) : int
    {
        return $this->schema->delete($this->entityTypeIdentifier, $conditions);
    }

    public function create(array $values) : int
    {
        $missing = $this->calculateMissingValues($values);
        if (count($missing) === 0) {
            return $this->schema->create($this->entityTypeIdentifier, $values);
        }

        trigger_error('Required values are missing: ' . join(', ', $missing), E_USER_ERROR);
    }

    private function mergeValuesIntoConditions(array $referenceConditions, array $values, array $conditions) {
        foreach ($referenceConditions as $referencedColumnIdentifier => $localColumnIdentifier) {<commit_msg>improve code coverage by negating if-statement<commit_after><?php
namespace pulledbits\ActiveRecord\SQL;

class EntityType
{
    private $schema;
    private $entityTypeIdentifier;

    private $identifier = [];
    private $requiredAttributeIdentifiers = [];
    private $references = [];

    public function __construct(\pulledbits\ActiveRecord\Schema $schema, $tableIdentifier)
    {
        $this->schema = $schema;
        $this->entityTypeIdentifier = $tableIdentifier;

        $indexes = $schema->listIndexesForTable($tableIdentifier)->fetchAll();
        foreach ($indexes as $index) {
            if ($index['Key_name'] === 'PRIMARY') {
                $this->identifier[] = $index['Column_name'];
            }
        }

        $columns = $schema->listColumnsForTable($tableIdentifier)->fetchAll();
        foreach ($columns as $column) {
            if ($column['Extra'] === 'auto_increment') {
                continue;
            } elseif ($column['Null'] === 'NO') {
                $this->requiredAttributeIdentifiers[] = $column['Field'];
            }
        }

        $foreignKeys = $schema->listForeignKeys($tableIdentifier)->fetchAll();
        foreach ($foreignKeys as $foreignKey) {
            $this->addForeignKeyConstraint($foreignKey['CONSTRAINT_NAME'], $foreignKey['COLUMN_NAME'], $foreignKey['REFERENCED_TABLE_NAME'], $foreignKey['REFERENCED_COLUMN_NAME']);
        }
    }

    public function primaryKey(array $values) {
        $sliced = [];
        foreach ($values as $key => $value) {
            if (in_array($key, $this->identifier, true)) {
                $sliced[$key] = $value;
            }
        }
        return $sliced;
    }

    public function calculateMissingValues(array $values) : array {
        $missing = [];
        foreach ($this->requiredAttributeIdentifiers as $requiredColumnIdentifier) {
            if (array_key_exists($requiredColumnIdentifier, $values) === false) {
                $missing[] = $requiredColumnIdentifier;
                break;
            } elseif ($values[$requiredColumnIdentifier] === null) {
                $missing[] = $requiredColumnIdentifier;
                break;
            }
        }
        return $missing;
    }

    private function addForeignKeyConstraint(string $constraintName, string $columnName, string $referencedTableName, string $referencedColumnName) {
        $fkIdentifier = join('', array_map('ucfirst', explode('_', $constraintName)));
        if (array_key_exists($fkIdentifier, $this->references)) {
            $this->references[$fkIdentifier]['conditions'][$referencedColumnName] = $columnName;
        } else {
            $this->references[$fkIdentifier] = [
                'entityTypeIdentifier' => $referencedTableName,
                'conditions' => [$referencedColumnName => $columnName]
            ];
        }
    }

    public function update(array $values, array $conditions) : int
    {
        return $this->schema->update($this->entityTypeIdentifier, $values, $conditions);
    }


    public function delete(array $conditions) : int
    {
        return $this->schema->delete($this->entityTypeIdentifier, $conditions);
    }

    public function create(array $values) : int
    {
        $missing = $this->calculateMissingValues($values);
        if (count($missing) > 0) {
            trigger_error('Required values are missing: ' . join(', ', $missing), E_USER_ERROR);
        }
        return $this->schema->create($this->entityTypeIdentifier, $values);
    }

    private function mergeValuesIntoConditions(array $referenceConditions, array $values, array $conditions) {
        foreach ($referenceConditions as $referencedColumnIdentifier => $localColumnIdentifier) {
{'fraction_non_alphanumeric': 0.13418918918918918, 'fraction_numerical': 0.0002702702702702703, 'mean_word_length': 2.7153614457831323, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 66, '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': '1422662', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1898, 'n_words': 369}