Completed
Pull Request — master (#11)
by
unknown
01:37
created

src/Tinker.php (2 issues)

usage of undeclared variables.

Bug Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 Symfony\Component\Console\Output\BufferedOutput;
13
14
class Tinker
15
{
16
    /** @var \Symfony\Component\Console\Output\BufferedOutput */
17
    protected $output;
18
19
    /** @var \Psy\Shell */
20
    protected $shell;
21
22
    public static function execute(string $phpCode): string
23
    {
24
        return(new static())->run($phpCode);
25
    }
26
27
    public function __construct()
28
    {
29
        $this->output = new BufferedOutput();
30
31
        $this->shell = $this->createShell($this->output);
32
    }
33
34
    public function run(string $phpCode): string
35
    {
36
        $phpCode = $this->removeComments($phpCode);
37
38
        $this->shell->addInput($phpCode);
39
40
        $closure = new ExecutionLoopClosure($this->shell);
41
42
        $closure->execute();
43
44
        return $this->cleanOutput($this->output->fetch());
45
    }
46
47
    protected function createShell(BufferedOutput $output): Shell
48
    {
49
        $config = new Configuration([
50
            'updateCheck' => 'never',
51
        ]);
52
53
        $config->getPresenter()->addCasters([
54
            Collection::class => 'Laravel\Tinker\TinkerCaster::castCollection',
55
            Model::class => 'Laravel\Tinker\TinkerCaster::castModel',
56
            Application::class => 'Laravel\Tinker\TinkerCaster::castApplication',
57
        ]);
58
59
        $shell = new Shell($config);
60
61
        $shell->setOutput($output);
62
63
        $composerClassMap = base_path('vendor/composer/autoload_classmap.php');
64
65
        if (file_exists($composerClassMap)) {
66
            ClassAliasAutoloader::register($shell, $composerClassMap);
67
        }
68
69
        return $shell;
70
    }
71
72
    public function removeComments(string $code): string
73
    {
74
        $tokens = collect(token_get_all("<?php\n".$code.'?>'));
75
76
        return $tokens->reduce(function ($carry, $token) {
77
            if (is_string($token)) {
78
                return $carry . $token;
79
            }
80
81
            // Destructure token array
82
            [$id, $text] = $token;
0 ignored issues
show
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...
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...
83
84
            // Ingore comments and php tags
85
            if ($id === T_COMMENT) {
86
                return $carry;
87
            }
88
            if ($id === T_DOC_COMMENT) {
89
                return $carry;
90
            }
91
            if ($id === T_OPEN_TAG) {
92
                return $carry;
93
            }
94
            if ($id === T_CLOSE_TAG) {
95
                return $carry;
96
            }
97
98
            return $carry . $text;
99
        }, '');
100
    }
101
102
    protected function cleanOutput(string $output): string
103
    {
104
        $output = preg_replace('/(?s)(<aside.*?<\/aside>)|Exit:  Ctrl\+D/ms', '$2', $output);
105
106
        return trim($output);
107
    }
108
}
109