Completed
Push — master ( 0f3b35...4c86ee )
by Roberto
04:55
created

TinkerServerCommand::getCasters()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
namespace Redmoon\TinkerServer\Console;
4
5
use Clue\React\Stdio\Stdio;
6
use Illuminate\Console\Command;
7
use Psy\Configuration;
8
use Psy\Shell;
9
use React\EventLoop\Factory;
10
use Symfony\Component\Console\Output\BufferedOutput;
11
12
class TinkerServerCommand extends Command
13
{
14
    protected $signature = 'tinker-server';
15
16
    /** @var BufferedOutput */
17
    protected $shellOutput;
18
19
    public function handle()
20
    {
21
        $shellOutput = new BufferedOutput();
0 ignored issues
show
Unused Code introduced by
$shellOutput is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
22
        $loop = Factory::create();
23
        $stdio = new Stdio($loop);
24
25
        $shell = $this->getPsyShell();
26
27
        $stdio->getReadline()->setPrompt('>> ');
28
29
        $stdio->on('data', function ($line) use ($stdio, $shell) {
30
            $line = rtrim($line, "\r\n");
31
            $shell->addCode($line);
32
            $closure = new ExecutionClosure($shell);
33
            $closure->execute();
34
            $stdio->write($this->shellOutput->fetch());
35
        });
36
37
        $loop->run();
38
    }
39
40
    /**
41
     * Get an instance of PsyShell
42
     *
43
     * @return Psy\Shell
44
     */
45
    protected function getPsyShell() : Shell
46
    {
47
        $config = new Configuration([
48
            'updateCheck' => 'never'
49
        ]);
50
51
        $config->getPresenter()->addCasters(
52
            $this->getCasters()
53
        );
54
55
        $shell = new Shell($config);
56
        $shell->setOutput($this->shellOutput);
57
58
        return $shell;
59
    }
60
61
    /**
62
     * Get an array of Laravel tailored casters.
63
     *
64
     * @return array
65
     */
66
    protected function getCasters() : array
67
    {
68
        $casters = [
69
            'Illuminate\Support\Collection' => 'Laravel\Tinker\TinkerCaster::castCollection',
70
        ];
71
72
        if (class_exists('Illuminate\Database\Eloquent\Model')) {
73
            $casters['Illuminate\Database\Eloquent\Model'] = 'Laravel\Tinker\TinkerCaster::castModel';
74
        }
75
76
        if (class_exists('Illuminate\Foundation\Application')) {
77
            $casters['Illuminate\Foundation\Application'] = 'Laravel\Tinker\TinkerCaster::castApplication';
78
        }
79
80
        return $casters;
81
    }
82
}
83