GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#21)
by Freek
01:35 queued 37s
created

Tinker   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 103
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 25 3
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->getPresenter()->addCasters([
58
            Collection::class => 'Laravel\Tinker\TinkerCaster::castCollection',
59
            Model::class => 'Laravel\Tinker\TinkerCaster::castModel',
60
            Application::class => 'Laravel\Tinker\TinkerCaster::castApplication',
61
        ]);
62
63
        $shell = new Shell($config);
64
65
        $shell->setOutput($output);
66
67
        $composerClassMap = base_path('vendor/composer/autoload_classmap.php');
68
69
        if (file_exists($composerClassMap)) {
70
            ClassAliasAutoloader::register($shell, $composerClassMap);
71
        }
72
73
        return $shell;
74
    }
75
76
    public function removeComments(string $code): string
77
    {
78
        $tokens = collect(token_get_all("<?php\n".$code.'?>'));
79
80
        return $tokens->reduce(function ($carry, $token) {
81
            if (is_string($token)) {
82
                return $carry.$token;
83
            }
84
85
            $text = $this->ignoreCommentsAndPhpTags($token);
86
87
            return $carry.$text;
88
        }, '');
89
    }
90
91
    protected function ignoreCommentsAndPhpTags(array $token)
92
    {
93
        [$id, $text] = $token;
0 ignored issues
show
Bug introduced by
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
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...
94
95
        if ($id === T_COMMENT) {
96
            return '';
97
        }
98
        if ($id === T_DOC_COMMENT) {
99
            return '';
100
        }
101
        if ($id === T_OPEN_TAG) {
102
            return '';
103
        }
104
        if ($id === T_CLOSE_TAG) {
105
            return '';
106
        }
107
108
        return $text;
109
    }
110
111
    protected function cleanOutput(string $output): string
112
    {
113
        $output = preg_replace('/(?s)(<aside.*?<\/aside>)|Exit:  Ctrl\+D/ms', '$2', $output);
114
115
        return trim($output);
116
    }
117
}
118