<commit_before><?php namespace Watson\Industrie;

use Watson\Industrie\DefinitionRepository;
use Watson\Industrie\Exceptions\ClassNotFoundException;
use Watson\Industrie\Exceptions\DefinitionNotFoundException;
use Watson\Industrie\Exceptions\IncompatibleClassException;
use Watson\Industrie\Generators\FakerGenerator;

class Builder {

    /**
     * Model definition repository.
     *
     * @var \Watson\Industrie\DefinitionRepository
     */
    protected $definitions;

    /**
     * The number of times we are building the class.
     *
     * @param int
     */
    protected $times = 1;

    /**
     * Construct the builder.
     *
     * @param  array  $definitions
     * @return void
     */
    public function __construct(DefinitionRepository $definitions)
    {
        $this->definitions = $definitions;
    }

    /**
     * Get the definition repository.
     *
     * @return \Watson\Industrie\DefinitionRepository
     */
    public function getDefinitions()
    {
        return $this->definitions;
    }

    /**
     * Get the number of times we are building the class.
     *
     * @return int
     */
    public function getTimes()
    {
        return $this->times;
    }

    /**
     * Set the number of times we are building the class.
     *
     * @param  int  $times
     * @return this
     */
    public function setTimes($times = 2)
    {
        $this->times = $times;

        return $this;
    }

    /**
     * Wrapper to set the number of times we are building the class.
     *
     * @param  int  $times
     * @return this
     */
    public function times($times = 2)
    {
        return $this->setTimes($times);
    }

    /**
     * Get the attribtues for a class.
     *
     * @param  string  $class
     * @param  array   $overrides
     * @return array
     */
    public function attributesFor($class, $overrides = [])
    {
        $definition = $this->getDefinitions()->getDefinition($class);

        return array_merge(
            call_user_func($definition, new FakerGenerator($this)),
            $overrides
        );
    }

    /**
     * Build classes with attributes set.
     *
     * @param  string  $class
     * @param  array   $overrides
     * @return mixed
     */
    public function build($class, $overrides = [])
    {
        $instance = $this->getInstance($class);

        foreach ($this->attributesFor($class, $overrides) as $key => $value)
        {
            $instance->setAttribute($key, $value);
        }

        return $instance;
    }

    /**
     * Build and persist classes with attributes set.
     *
     * @param  string  $class
     * @param  array   $overrides
     * @return mixed
     */
    public function create($class, $overrides = [])
    {
        $instances = [];

        for ($i = 0; $i < $this->getTimes(); $i++)
        {
            $instance = $this->build($class, $overrides);
            $instance->save();

            $instances[] = $instance;
        }

        return count($instances) > 1 ? $instances : $instances[0];
    }

    /*
     * Get an fresh instance of the class or class name provided.
     *
     * @param  stirng  $class
     * @return mixed
     */
    public function getInstance($class)
    {
        if ( ! class_exists($class))
        {
            throw new ClassNotFoundException("The {$class} class was not found.");
        }

        if ( ! is_subclass_of($class, 'Illuminate\Database\Eloquent\Model'))
        {
            throw new IncompatibleClassException("The {$class} class is not an Eloquent model.");
        }

        return new $class;
    }

}<commit_msg>Extract class logic to method<commit_after><?php namespace Watson\Industrie;

use Watson\Industrie\DefinitionRepository;
use Watson\Industrie\Exceptions\ClassNotFoundException;
use Watson\Industrie\Exceptions\DefinitionNotFoundException;
use Watson\Industrie\Exceptions\IncompatibleClassException;
use Watson\Industrie\Generators\FakerGenerator;

class Builder {

    /**
     * Model definition repository.
     *
     * @var \Watson\Industrie\DefinitionRepository
     */
    protected $definitions;

    /**
     * The number of times we are building the class.
     *
     * @param int
     */
    protected $times = 1;

    /**
     * Construct the builder.
     *
     * @param  array  $definitions
     * @return void
     */
    public function __construct(DefinitionRepository $definitions)
    {
        $this->definitions = $definitions;
    }

    /**
     * Get the definition repository.
     *
     * @return \Watson\Industrie\DefinitionRepository
     */
    public function getDefinitions()
    {
        return $this->definitions;
    }

    /**
     * Get the number of times we are building the class.
     *
     * @return int
     */
    public function getTimes()
    {
        return $this->times;
    }

    /**
     * Set the number of times we are building the class.
     *
     * @param  int  $times
     * @return this
     */
    public function setTimes($times = 2)
    {
        $this->times = $times;

        return $this;
    }

    /**
     * Wrapper to set the number of times we are building the class.
     *
     * @param  int  $times
     * @return this
     */
    public function times($times = 2)
    {
        return $this->setTimes($times);
    }

    /**
     * Get the attribtues for a class.
     *
     * @param  string  $class
     * @param  array   $overrides
     * @return array
     */
    public function attributesFor($class, $overrides = [])
    {
        $definition = $this->getDefinitions()->getDefinition($class);

        return array_merge(
            call_user_func($definition, new FakerGenerator($this)),
            $overrides
        );
    }

    /**
     * Build classes with attributes set.
     *
     * @param  string  $class
     * @param  array   $overrides
     * @return mixed
     */
    public function build($class, $overrides = [])
    {
        return $this->buildInstance($class, $overrides);
    }

    /**
     * Build and persist classes with attributes set.
     *
     * @param  string  $class
     * @param  array   $overrides
     * @return mixed
     */
    public function create($class, $overrides = [])
    {
        $instances = [];

        for ($i = 0; $i < $this->getTimes(); $i++)
        {
            $instances[] = $this->buildInstance($class, $overrides, true);
        }

        return count($instances) > 1 ? $instances : $instances[0];
    }

    /*
     * Get an fresh instance of the class or class name provided.
     *
     * @param  stirng  $class
     * @return mixed
     */
    protected function getInstance($class)
    {
        if ( ! class_exists($class))
        {
            throw new ClassNotFoundException("The {$class} class was not found.");
        }

        if ( ! is_subclass_of($class, 'Illuminate\Database\Eloquent\Model'))
        {
            throw new IncompatibleClassException("The {$class} class is not an Eloquent model.");
        }

        return new $class;
    }

    /**
     * Build an instance of the given class with attributes.
     *
     * @param  string  $class
     * @param  array   $overrides
     * @param  bool    $persist
     * @return mixed
     */
    protected function buildInstance($class, $overrides = [], $persist = false)
    {
        $instance = $this->getInstance($class);

        foreach ($this->attributesFor($class, $overrides) as $key => $value)
        {
            $instance->setAttribute($key, $value);
        }

        if ($persist)
        {
            $instance->save();
        }

        return $instance;
    }

}
{'fraction_non_alphanumeric': 0.1177319858249114, 'fraction_numerical': 0.0015750098438115237, 'mean_word_length': 1.9307692307692308, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 34, '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': '3549359', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2180, 'n_words': 556}