Skip to content
This repository was archived by the owner on Aug 10, 2025. It is now read-only.

Commit a0e8e86

Browse files
authored
Merge pull request #4 from luminarix/feature/readability-changes
Code readability changes, modernization
2 parents 98af0d3 + a9b3526 commit a0e8e86

File tree

3 files changed

+21
-43
lines changed

3 files changed

+21
-43
lines changed

src/Http/Middleware/Authorize.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ public function handle($request, $next)
1515

1616
protected function allowedToUseTinker(): bool
1717
{
18-
if (!config('web-tinker.enabled')) {
19-
return false;
20-
}
21-
22-
return Gate::check('viewWebTinker');
18+
return config('web-tinker.enabled') && Gate::check('viewWebTinker');
2319
}
2420
}

src/LaravelWebTinker.php

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515

1616
class LaravelWebTinker
1717
{
18-
/** @var BufferedOutput */
19-
protected $output;
18+
protected BufferedOutput $output;
2019

21-
/** @var Shell */
22-
protected $shell;
20+
protected Shell $shell;
2321

24-
/** @var OutputModifier */
25-
protected $outputModifier;
22+
protected OutputModifier $outputModifier;
2623

2724
public function __construct(OutputModifier $outputModifier)
2825
{
@@ -36,11 +33,9 @@ public function __construct(OutputModifier $outputModifier)
3633
public function execute(string $phpCode): string
3734
{
3835
$phpCode = $this->removeComments($phpCode);
39-
4036
$this->shell->addInput($phpCode);
4137

4238
$closure = new ExecutionLoopClosure($this->shell);
43-
4439
$runtime = Benchmark::measure(fn () => $closure->execute());
4540
$output = $this->cleanOutput($this->output->fetch());
4641

@@ -49,24 +44,22 @@ public function execute(string $phpCode): string
4944

5045
public function removeComments(string $code): string
5146
{
52-
$tokens = collect(token_get_all("<?php\n" . $code . '?>'));
53-
54-
return $tokens->reduce(function ($carry, $token) {
55-
if (is_string($token)) {
56-
return $carry . $token;
57-
}
58-
59-
$text = $this->ignoreCommentsAndPhpTags($token);
60-
61-
return $carry . $text;
62-
}, '');
47+
return collect(token_get_all("<?php\n" . $code . '?>'))
48+
->reduce(
49+
callback: function ($carry, $token) {
50+
return is_string($token)
51+
? $carry . $token
52+
: $carry . $this->ignoreCommentsAndPhpTags($token);
53+
},
54+
initial: ''
55+
);
6356
}
6457

6558
protected function createShell(BufferedOutput $output): Shell
6659
{
6760
$config = new Configuration([
6861
'updateCheck' => 'never',
69-
'configFile' => config('web-tinker.config_file') !== null ? base_path() . '/' . config('web-tinker.config_file') : null,
62+
'configFile' => config('web-tinker.config_file') ? base_path(config('web-tinker.config_file')) : null,
7063
]);
7164

7265
$config->setHistoryFile(defined('PHP_WINDOWS_VERSION_BUILD') ? 'null' : '/dev/null');
@@ -78,11 +71,9 @@ protected function createShell(BufferedOutput $output): Shell
7871
]);
7972

8073
$shell = new Shell($config);
81-
8274
$shell->setOutput($output);
8375

8476
$composerClassMap = base_path('vendor/composer/autoload_classmap.php');
85-
8677
if (file_exists($composerClassMap)) {
8778
ClassAliasAutoloader::register($shell, $composerClassMap, config('tinker.alias', []), config('tinker.dont_alias', []));
8879
}
@@ -94,26 +85,12 @@ protected function ignoreCommentsAndPhpTags(array $token)
9485
{
9586
[$id, $text] = $token;
9687

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;
88+
return in_array($id, [T_COMMENT, T_DOC_COMMENT, T_OPEN_TAG, T_CLOSE_TAG]) ? '' : $text;
11189
}
11290

11391
protected function cleanOutput(string $output): string
11492
{
11593
$output = preg_replace('/(?s)(<aside.*?<\/aside>)|Exit: {2}Ctrl\+D/ms', '$2', $output);
116-
11794
$output = preg_replace('/(?s)(<whisper.*?<\/whisper>)|INFO {2}Ctrl\+D\./ms', '$2', $output);
11895

11996
return trim($output);

src/OutputModifiers/PrefixDateTime.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ class PrefixDateTime implements OutputModifier
66
{
77
public function modify(string $output, float $runtime): string
88
{
9-
return '<span style="color:rgba(255,255,255,0.2);font-style:italic">' . now()->format('Y-m-d H:i:s') . ' (runtime: ' . number_format($runtime / 1_000, 3) . 's)</span><br>' . $output;
9+
$timestamp = now()->format('Y-m-d H:i:s');
10+
$formattedRuntime = number_format($runtime / 1_000, 3);
11+
12+
return <<<HTML
13+
<span style="color:rgba(255,255,255,0.2);font-style:italic">{$timestamp} (runtime: {$formattedRuntime}s)</span><br>{$output}
14+
HTML;
1015
}
1116
}

0 commit comments

Comments
 (0)