Tinker   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 8
dl 0
loc 105
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A execute() 0 14 1
A createShell() 0 27 4
A removeComments() 0 14 2
A ignoreCommentsAndPhpTags() 0 19 5
A cleanOutput() 0 6 1
1
<?php
2
3
namespace Spatie\WebTinker;
4
5
use Psy\Shell;
6
use Psy\Configuration;
7
use Psy\ExecutionLoopClosure;
8
use Illuminate\Support\Collection;
9
use Illuminate\Foundation\Application;
10
use Illuminate\Database\Eloquent\Model;
11
use Laravel\Tinker\ClassAliasAutoloader;
12
use Spatie\WebTinker\OutputModifiers\OutputModifier;
13
use Symfony\Component\Console\Output\BufferedOutput;
14
15
class Tinker
16
{
17
    /** @var \Symfony\Component\Console\Output\BufferedOutput */
18
    protected $output;
19
20
    /** @var \Psy\Shell */
21
    protected $shell;
22
23
    /** @var \Spatie\WebTinker\OutputModifiers\OutputModifier */
24
    protected $outputModifier;
25
26
    public function __construct(OutputModifier $outputModifier)
27
    {
28
        $this->output = new BufferedOutput();
29
30
        $this->shell = $this->createShell($this->output);
31
32
        $this->outputModifier = $outputModifier;
33
    }
34
35
    public function execute(string $phpCode): string
36
    {
37
        $phpCode = $this->removeComments($phpCode);
38
39
        $this->shell->addInput($phpCode);
40
41
        $closure = new ExecutionLoopClosure($this->shell);
42
43
        $closure->execute();
44
45
        $output = $this->cleanOutput($this->output->fetch());
46
47
        return $this->outputModifier->modify($output);
48
    }
49
50
    protected function createShell(BufferedOutput $output): Shell
51
    {
52
        $config = new Configuration([
53
            'updateCheck' => 'never',
54
            'configFile' => config('web-tinker.config_file') !== null ? base_path().'/'.config('web-tinker.config_file') : null,
55
        ]);
56
57
        $config->setHistoryFile(defined('PHP_WINDOWS_VERSION_BUILD') ? 'nul' : '/dev/null');
58
59
        $config->getPresenter()->addCasters([
60
            Collection::class => 'Laravel\Tinker\TinkerCaster::castCollection',
61
            Model::class => 'Laravel\Tinker\TinkerCaster::castModel',
62
            Application::class => 'Laravel\Tinker\TinkerCaster::castApplication',
63
        ]);
64
65
        $shell = new Shell($config);
66
67
        $shell->setOutput($output);
68
69
        $composerClassMap = base_path('vendor/composer/autoload_classmap.php');
70
71
        if (file_exists($composerClassMap)) {
72
            ClassAliasAutoloader::register($shell, $composerClassMap);
73
        }
74
75
        return $shell;
76
    }
77
78
    public function removeComments(string $code): string
79
    {
80
        $tokens = collect(token_get_all("<?php\n".$code.'?>'));
81
82
        return $tokens->reduce(function ($carry, $token) {
83
            if (is_string($token)) {
84
                return $carry.$token;
85
            }
86
87
            $text = $this->ignoreCommentsAndPhpTags($token);
88
89
            return $carry.$text;
90
        }, '');
91
    }
92
93
    protected function ignoreCommentsAndPhpTags(array $token)
94
    {
95
        [$id, $text] = $token;
0 ignored issues
show
Bug introduced by Razvan Aurariu
The variable $id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by Razvan Aurariu
The variable $text does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
96
97
        if ($id === T_COMMENT) {
98
            return '';
99
        }
100
        if ($id === T_DOC_COMMENT) {
101
            return '';
102
        }
103
        if ($id === T_OPEN_TAG) {
104
            return '';
105
        }
106
        if ($id === T_CLOSE_TAG) {
107
            return '';
108
        }
109
110
        return $text;
111
    }
112
113
    protected function cleanOutput(string $output): string
114
    {
115
        $output = preg_replace('/(?s)(<aside.*?<\/aside>)|Exit:  Ctrl\+D/ms', '$2', $output);
116
117
        return trim($output);
118
    }
119
}
120