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 (#11)
by
unknown
06:32
created

Tinker::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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 = token_get_all("<?php\n".$code."\n?>");
75
        $cleanCode = "";
76
        foreach ($tokens as $token) {
77
            if (is_string($token)) {
78
                // simple 1-character token
79
                $cleanCode .= $token;
80
            } else {
81
                // token array
82
                list($id, $text) = $token;
83
84
                switch ($id) {
85
                    case T_COMMENT:
86
                    case T_DOC_COMMENT:
87
                    case T_OPEN_TAG:
88
                    case T_CLOSE_TAG:
89
                        // no action on comments and php tags
90
                        break;
91
                    default:
92
                        // anything else -> output "as is"
93
                        $cleanCode .= $text;
94
                        break;
95
                }
96
            }
97
        }
98
        return $cleanCode;
99
    }
100
101
    protected function cleanOutput(string $output): string
102
    {
103
        $output = preg_replace('/(?s)(<aside.*?<\/aside>)|Exit:  Ctrl\+D/ms', '$2', $output);
104
105
        return trim($output);
106
    }
107
}
108