TerminalController::renderException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Console\Http\Controllers\Adminarea;
6
7
use Exception;
8
use Illuminate\Http\Request;
9
use Cortex\Console\Services\Terminal;
10
use Cortex\Foundation\Http\Controllers\AuthorizedController;
11
12
class TerminalController extends AuthorizedController
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    protected $resource = 'run-terminal';
18
19
    /**
20
     * Show terminal index.
21
     *
22
     * @param \Illuminate\Http\Request          $request
23
     * @param \Cortex\Console\Services\Terminal $terminal
24
     *
25
     * @throws \Exception
26
     *
27
     * @return \Illuminate\View\View
28
     */
29
    public function index(Request $request, Terminal $terminal)
30
    {
31
        $token = null;
32
33
        if ($request->hasSession() === true) {
34
            $token = $request->session()->token();
0 ignored issues
show
Bug introduced by
The method token() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
        }
36
37
        $terminal->call('list --ansi');
38
        $options = json_encode([
39
            'username' => 'LARAVEL',
40
            'hostname' => php_uname('n'),
41
            'os' => PHP_OS,
42
            'csrfToken' => $token,
43
            'helpInfo' => $terminal->output(),
44
            'basePath' => app()->basePath(),
45
            'environment' => app()->environment(),
46
            'version' => app()->version(),
47
            'endpoint' => route('adminarea.console.terminal.execute'),
48
            'interpreters' => [
49
                'mysql' => 'mysql',
50
                'artisan tinker' => 'tinker',
51
                'tinker' => 'tinker',
52
            ],
53
            'confirmToProceed' => [
54
                'artisan' => [
55
                    'migrate',
56
                    'migrate:install',
57
                    'migrate:refresh',
58
                    'migrate:reset',
59
                    'migrate:rollback',
60
                    'db:seed',
61
                ],
62
            ],
63
        ]);
64
65
        return view('cortex/console::adminarea.pages.terminal', compact('options'));
66
    }
67
68
    /**
69
     * Process the form for store/update of the given resource.
70
     *
71
     * @param \Illuminate\Http\Request $request
72
     *
73
     * @throws \Exception
74
     *
75
     * @return \Illuminate\Http\JsonResponse
76
     */
77
    public function execute(Terminal $terminal, Request $request)
78
    {
79
        $error = $terminal->call($request->get('command'));
80
81
        return response()->json([
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
82
            'jsonrpc' => $request->get('jsonrpc'),
83
            'id' => $request->get('id'),
84
            'result' => $terminal->output(),
85
            'error' => $error,
86
        ]);
87
    }
88
89
    /**
90
     * Render exception.
91
     *
92
     * @param \Exception $exception
93
     *
94
     * @return \Illuminate\View\View
95
     */
96
    protected function renderException(Exception $exception)
97
    {
98
        return view('cortex/console::adminarea.pages.error', ['message' => $exception->getMessage()]);
99
    }
100
}
101