<commit_before>
namespace Ice\Auth\Driver\Model\Users;

use Ice\Text;
use Ice\Mvc\Model;
use Ice\Validation;

/**
 * Model user's Tokens.
 *
 * @package     Ice/Auth
 * @category    Model
 * @author      I<NAME>
 * @copyright   (c) 2014-2018 Ice Team
 * @license     http://iceframework.org/license
 */
class Tokens extends Model
{

    protected from = "user_tokens";

    /**
     * User class name.
     */
    protected userClass = "Ice\\Auth\\Driver\\Model\\Users";

    /**
     * Initialize token's relations, remove expired tokens.
     *
     * @return void
     */
    public function initialize()
    {
        var auth;

        let auth = this->getDi()->get("auth");

        this->belongsTo("user_id", auth->getOption("users", this->userClass), this->getIdKey(), [
            "alias": "User",
            "foreignKey": true
        ]);

        // Do garbage collection
        if mt_rand(1, 100) === 1 {
            this->deleteExpired();
        }

        // This object has expired
        if this->has("expires") && this->get("expires") < time() {
            this->remove();
        }
    }

    /**
     * Generate a new unique token and create the token.
     *
     * @param array fields Fields to save or valid fields
     * @param object extra Extra validation
     * @return mixed
     */
    public function create(var fields = [], <Validation> extra = null)
    {
        let this->{"token"} = this->generate();

        return parent::create(fields);
    }

    /**
     * Deletes all expired tokens.
     *
     * @return bool status
     */
    public function deleteExpired()
    {
        return this->remove([
            "expires": ["<": time()]
        ]);
    }

    /**
     * Generate a new unique token and update the token.
     *
     * @param array fields Fields to save or valid fields
     * @param object extra Extra validation
     * @return mixed
     */
    public function update(var fields = [], <Validation> extra = null)
    {
        let this->{"token"} = this->generate();

        return parent::update(fields);
    }

    /**
     * Generate a new unique token.
     *
     * @return string
     * @uses Text::random()
     */
    protected function generate() -> string
    {
        var token;

        do {
            let token = bin2hex(openssl_random_pseudo_bytes(16));
        } while static::findOne(["token": token]);

        return token;
    }
}
<commit_msg>Update tokens.zep<commit_after>
namespace Ice\Auth\Driver\Model\Users;

use Ice\Text;
use Ice\Mvc\Model;
use Ice\Validation;

/**
 * Model user's Tokens.
 *
 * @package     Ice/Auth
 * @category    Model
 * @author      I<NAME>
 * @copyright   (c) 2014-2018 Ice Team
 * @license     http://iceframework.org/license
 */
class Tokens extends Model
{

    protected from = "user_tokens";

    /**
     * User class name.
     */
    protected userClass = "Ice\\Auth\\Driver\\Model\\Users";

    /**
     * Initialize token's relations, remove expired tokens.
     *
     * @return void
     */
    public function initialize()
    {
        var auth, expire;

        let auth = this->di->get("auth");

        this->belongsTo("user_id", auth->getOption("users", this->userClass), this->getIdKey(), [
            "alias": "User",
            "foreignKey": true
        ]);

        // Do garbage collection
        if mt_rand(1, 100) === 1 {
            this->deleteExpired();
        }

        let expire = this->get("expires");

        // This object has expired
        if expire > 0 && expire < time() {
            this->remove();
        }
    }

    /**
     * Generate a new unique token and create the token.
     *
     * @param array fields Fields to save or valid fields
     * @param object extra Extra validation
     * @return mixed
     */
    public function create(var fields = [], <Validation> extra = null)
    {
        let this->{"token"} = this->generate();

        return parent::create(fields);
    }

    /**
     * Deletes all expired tokens.
     *
     * @return bool status
     */
    public function deleteExpired()
    {
        return this->remove([
            "expires": ["<": time()]
        ]);
    }

    /**
     * Generate a new unique token and update the token.
     *
     * @param array fields Fields to save or valid fields
     * @param object extra Extra validation
     * @return mixed
     */
    public function update(var fields = [], <Validation> extra = null)
    {
        let this->{"token"} = this->generate();

        return parent::update(fields);
    }

    /**
     * Generate a new unique token.
     *
     * @return string
     * @uses Text::random()
     */
    protected function generate() -> string
    {
        var token;

        do {
            let token = bin2hex(openssl_random_pseudo_bytes(16));
        } while static::findOne(["token": token]);

        return token;
    }
}

{'fraction_non_alphanumeric': 0.13536760188408764, 'fraction_numerical': 0.006758140487405284, 'mean_word_length': 1.8831168831168832, 'pattern_counts': {'":': 10, '<': 13, '<?xml version=': 0, '>': 41, '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': '4877045', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1365, 'n_words': 388}