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
Push — master ( 906385...fb30d7 )
by Freek
18s queued 10s
created

Tinker   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 0
loc 100
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 4 1
A __construct() 0 6 1
A run() 0 12 1
A createShell() 0 24 2
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 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
            $text = $this->ignoreCommentsAndPhpTags($token);
82
83
            return $carry.$text;
84
        }, '');
85
    }
86
87
    protected function ignoreCommentsAndPhpTags(array $token)
88
    {
89
        [$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...
90
91
        if ($id === T_COMMENT) {
92
            return '';
93
        }
94
        if ($id === T_DOC_COMMENT) {
95
            return '';
96
        }
97
        if ($id === T_OPEN_TAG) {
98
            return '';
99
        }
100
        if ($id === T_CLOSE_TAG) {
101
            return '';
102
        }
103
104
        return $text;
105
    }
106
107
    protected function cleanOutput(string $output): string
108
    {
109
        $output = preg_replace('/(?s)(<aside.*?<\/aside>)|Exit:  Ctrl\+D/ms', '$2', $output);
110
111
        return trim($output);
112
    }
113
}
114