<commit_before><?php

namespace project\controllers\actions;

use project\models\Project;
use VcsCommon\BaseCommit;
use VcsCommon\BaseDiff;
use VcsCommon\BaseRepository;
use VcsCommon\exception\CommonException;
use Yii;
use yii\base\Action;
use yii\base\InvalidParamException;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;

/**
 * View file diffs (or raw file) by specific commit id in modal window.
 * Use this action only by AJAX access.
 */
class FileViewAction extends Action
{
    /**
     * Diff type
     */
    const MODE_DIFF = 'diff';

    /**
     * Raw type
     */
    const MODE_RAW = 'raw';

    /**
     * @var Project project model
     */
    public $project;

    /**
     * @var BaseRepository repository model
     */
    public $repository;

    /**
     * @var string commit identifier
     */
    public $commitId;

    /**
     * @var string relative file path
     */
    public $filePath;

    /**
     * @var string file view type: diff or raw
     */
    public $mode;

    /**
     * Validate project model before run action
     *
     * @throws InvalidParamException
     */
    public function init()
    {
        if (!Yii::$app->request->isAjax) {
            // only AJAX access
            throw new ForbiddenHttpException();
        }
        if (!$this->project instanceof Project) {
            throw new InvalidParamException('Repository property must be an instance of \project\models\Project');
        }
        if (!$this->repository instanceof BaseRepository) {
            throw new InvalidParamException('Repository property must be an instance of \VcsCommon\BaseRepository');
        }
        if (!is_string($this->commitId) || !preg_match('#[a-f0-9]+#i', $this->commitId)) {
            throw new InvalidParamException('Invalid commit identifier');
        }
        if (!is_string($this->filePath)) {
            throw new InvalidParamException('Invalid file path');
        }
        Yii::$app->response->format = Response::FORMAT_JSON;
    }

    /**
     * Render file view.
     * If has CommonException - it's may by only not found commit, or not found file in project path.
     *
     * @return array
     * @throws NotFoundHttpException
     */
    public function run()
    {
        /* @var $commit BaseCommit */
        $commit = null;

        /* @var $fileDiff BaseDiff[] */
        $fileDiff = null;
        /* @var $fileContents string */
        $fileContents = '';
        /* @var $raw string */
        $raw = null;

        try {
            // get commit model by commit identifier
            $commit = $this->repository->getCommit($this->commitId);

            if ($this->mode === self::MODE_DIFF) {
                $fileDiff = $commit->getDiff($this->filePath);
            }
            elseif ($this->mode === self::MODE_RAW) {<commit_msg>fix default variables in FileViewAction.php<commit_after><?php

namespace project\controllers\actions;

use project\models\Project;
use VcsCommon\BaseCommit;
use VcsCommon\BaseDiff;
use VcsCommon\BaseRepository;
use VcsCommon\exception\CommonException;
use Yii;
use yii\base\Action;
use yii\base\InvalidParamException;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;

/**
 * View file diffs (or raw file) by specific commit id in modal window.
 * Use this action only by AJAX access.
 */
class FileViewAction extends Action
{
    /**
     * Diff type
     */
    const MODE_DIFF = 'diff';

    /**
     * Raw type
     */
    const MODE_RAW = 'raw';

    /**
     * @var Project project model
     */
    public $project;

    /**
     * @var BaseRepository repository model
     */
    public $repository;

    /**
     * @var string commit identifier
     */
    public $commitId;

    /**
     * @var string relative file path
     */
    public $filePath;

    /**
     * @var string file view type: diff or raw
     */
    public $mode;

    /**
     * Validate project model before run action
     *
     * @throws InvalidParamException
     */
    public function init()
    {
        if (!Yii::$app->request->isAjax) {
            // only AJAX access
            throw new ForbiddenHttpException();
        }
        if (!$this->project instanceof Project) {
            throw new InvalidParamException('Repository property must be an instance of \project\models\Project');
        }
        if (!$this->repository instanceof BaseRepository) {
            throw new InvalidParamException('Repository property must be an instance of \VcsCommon\BaseRepository');
        }
        if (!is_string($this->commitId) || !preg_match('#[a-f0-9]+#i', $this->commitId)) {
            throw new InvalidParamException('Invalid commit identifier');
        }
        if (!is_string($this->filePath)) {
            throw new InvalidParamException('Invalid file path');
        }
        Yii::$app->response->format = Response::FORMAT_JSON;
    }

    /**
     * Render file view.
     * If has CommonException - it's may by only not found commit, or not found file in project path.
     *
     * @return array
     * @throws NotFoundHttpException
     */
    public function run()
    {
        /* @var $commit BaseCommit */
        $commit = null;
        /* @var $fileDiff BaseDiff[] */
        $fileDiff = [];
        /* @var $fileContents string */
        $fileContents = '';

        try {
            // get commit model by commit identifier
            $commit = $this->repository->getCommit($this->commitId);

            if ($this->mode === self::MODE_DIFF) {
                $fileDiff = $commit->getDiff($this->filePath);
            }
            elseif ($this->mode === self::MODE_RAW) {
{'fraction_non_alphanumeric': 0.11958618271085393, 'fraction_numerical': 0.0007013852358407856, 'mean_word_length': 2.177715877437326, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '2889226', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1585, 'n_words': 442}