1515
1616class 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 );
0 commit comments