CodeTesterController::form()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 22
nc 2
nop 0
dl 0
loc 26
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
namespace App\Admin\Controllers;
4
5
use App\Http\Controllers\Controller;
6
use Encore\Admin\Layout\Content;
7
use App\Models\Eloquent\OJ;
8
use App\Models\Eloquent\Problem;
9
use App\Models\Eloquent\Compiler;
10
use Encore\Admin\Widgets\Form;
11
use Encore\Admin\Widgets\Box;
12
use Encore\Admin\Widgets\Table;
13
use App\Babel\Babel;
14
use Arr;
15
16
class CodeTesterController extends Controller
17
{
18
    /**
19
     * Show the Testing Page.
20
     *
21
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Admin\Controllers\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
22
     */
23
    public function tester(Content $content)
24
    {
25
        $content=$content->header(__('admin.tester.tester.header'));
0 ignored issues
show
Bug introduced by
It seems like __('admin.tester.tester.header') can also be of type array and array; however, parameter $header of Encore\Admin\Layout\Content::header() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        $content=$content->header(/** @scrutinizer ignore-type */ __('admin.tester.tester.header'));
Loading history...
26
        $content=$content->description(__('admin.tester.tester.description'));
0 ignored issues
show
Bug introduced by
It seems like __('admin.tester.tester.description') can also be of type array and array; however, parameter $description of Encore\Admin\Layout\Content::description() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        $content=$content->description(/** @scrutinizer ignore-type */ __('admin.tester.tester.description'));
Loading history...
27
        if (request()->isMethod('post')) {
28
            $content=$content->body($this->run());
29
        }
30
        return $content->body($this->form());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $content->body($this->form()) returns the type Encore\Admin\Layout\Content which is incompatible with the documented return type App\Admin\Controllers\Response.
Loading history...
31
    }
32
33
    /**
34
     * Make a form builder.
35
     *
36
     * @return Form
37
     */
38
    protected function form()
39
    {
40
        $OJ=OJ::where(["ocode"=>"noj"])->get();
41
        $box=new Box(__('admin.tester.tester.title'));
0 ignored issues
show
Bug introduced by
It seems like __('admin.tester.tester.title') can also be of type array and array; however, parameter $title of Encore\Admin\Widgets\Box::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        $box=new Box(/** @scrutinizer ignore-type */ __('admin.tester.tester.title'));
Loading history...
42
        if (blank($OJ)) {
43
            $box->style('danger');
44
            $box->content(__('admin.tester.help.installfirst'));
0 ignored issues
show
Bug introduced by
It seems like __('admin.tester.help.installfirst') can also be of type array and array; however, parameter $content of Encore\Admin\Widgets\Box::content() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
            $box->content(/** @scrutinizer ignore-type */ __('admin.tester.help.installfirst'));
Loading history...
45
            return $box;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $box returns the type Encore\Admin\Widgets\Box which is incompatible with the documented return type Encore\Admin\Widgets\Form.
Loading history...
46
        }
47
        $oid=$OJ->first()->oid;
48
        $box->style('success');
49
        $form=new Form();
50
        $form->select('oid', __('admin.tester.oj'))->options($OJ->pluck('name', 'oid'))->help(__('admin.tester.help.onlinejudge'))->rules('required');
0 ignored issues
show
Bug introduced by
It seems like __('admin.tester.help.onlinejudge') can also be of type array and array; however, parameter $text of Encore\Admin\Form\Field::help() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $form->select('oid', __('admin.tester.oj'))->options($OJ->pluck('name', 'oid'))->help(/** @scrutinizer ignore-type */ __('admin.tester.help.onlinejudge'))->rules('required');
Loading history...
51
        $form->select('pid', __('admin.tester.pid'))->options(Problem::where(["OJ"=>$oid])->get()->sortBy('readable_name')->pluck('readable_name', 'pid'))->rules('required');
52
        $form->select('coid', __('admin.tester.coid'))->options(Compiler::where(["oid"=>$oid])->get()->pluck('display_name', 'coid'))->rules('required');
53
        $form->textarea('solution', __('admin.tester.solution'))->rows(20)->rules('required');
54
        $form->action(route('admin.codetester.tester'));
55
        $form->fill([
56
            'oid'=>request()->oid,
57
            'pid'=>request()->pid,
58
            'coid'=>request()->coid,
59
            'solution'=>request()->solution,
60
        ]);
61
        $form->method('POST');
62
        $box->content($form);
0 ignored issues
show
Bug introduced by
$form of type Encore\Admin\Widgets\Form is incompatible with the type string expected by parameter $content of Encore\Admin\Widgets\Box::content(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
        $box->content(/** @scrutinizer ignore-type */ $form);
Loading history...
63
        return $box;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $box returns the type Encore\Admin\Widgets\Box which is incompatible with the documented return type Encore\Admin\Widgets\Form.
Loading history...
64
    }
65
66
    /**
67
     * Running Test.
68
     *
69
     * @return Response
70
     */
71
    protected function run()
72
    {
73
        $babel=new Babel();
74
        request()->validate([
75
            'oid' => 'required|integer',
76
            'pid' => 'required|integer',
77
            'coid' => 'required|integer',
78
            'solution' => 'required',
79
        ]);
80
        $runner=$babel->testrun([
81
            'name' => 'noj',
82
            'pid' => request()->pid,
83
            'coid' => request()->coid,
84
            'solution' => request()->solution,
85
        ]);
86
        $verdict=$runner->verdict;
87
        $boxRun=new Box(__('admin.tester.tester.run'));
0 ignored issues
show
Bug introduced by
It seems like __('admin.tester.tester.run') can also be of type array and array; however, parameter $title of Encore\Admin\Widgets\Box::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        $boxRun=new Box(/** @scrutinizer ignore-type */ __('admin.tester.tester.run'));
Loading history...
88
        $boxRun->style('info');
89
        $verdictData=[];
90
        foreach ($verdict['data'] as $v) {
91
            $verdictData[]=[
92
                $v["test_case"],
93
                $v["cpu_time"],
94
                $v["real_time"],
95
                $v["memory"],
96
                $v["signal"],
97
                $v["exit_code"],
98
                $v["error"],
99
                $v["result"],
100
            ];
101
        }
102
        $table=new Table(['Test Case', 'CPU Time(ms)', 'Real Time(ms)', 'Memory(byte)', 'Signal', 'Exit Code', 'Error', 'Result'], $verdictData);
103
        $output="<p>Verdict: {$verdict['verdict']}</p>";
104
        if (!blank($verdict['compile_info'])) {
105
            $output.="<p>Compiler Info:</p><pre>".htmlspecialchars($verdict['compile_info'])."</pre>";
106
        }
107
        $output.=$table->render();
108
        $boxRun->content($output);
109
        return $boxRun;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $boxRun returns the type Encore\Admin\Widgets\Box which is incompatible with the documented return type App\Admin\Controllers\Response.
Loading history...
110
    }
111
}
112