Skip to content

Commit 92f25af

Browse files
committed
wip
1 parent 1327512 commit 92f25af

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

app/Commands/AutocompleteCommand.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88

99
class AutocompleteCommand extends Command
1010
{
11-
protected $signature = 'autocomplete {code} {--debug} {--from-file=}';
11+
use ResolvesCode;
12+
13+
protected $signature = 'autocomplete {code} {--from-file} {--debug} {--local-file=}';
1214

1315
protected $description = 'Parse the given PHP code and return the autocomplete results';
1416

1517
public function handle(): void
1618
{
17-
$code = $this->argument('code');
18-
19-
if ($this->option('from-file')) {
20-
$code = file_get_contents(__DIR__ . '/../../tests/snippets/parse/' . $this->option('from-file') . '.php');
21-
}
19+
$code = $this->resolveCode('autocomplete');
2220

2321
$walker = new Walker($code, (bool) $this->option('debug'));
2422
$result = $walker->walk();
@@ -38,7 +36,7 @@ protected function log($autocompleting, $result, $code)
3836
File::ensureDirectoryExists(storage_path($dir));
3937
$now = now()->format('Y-m-d-H-i-s');
4038

41-
if (!$this->option('from-file')) {
39+
if (!$this->option('local-file')) {
4240
File::put(storage_path("{$dir}/{$now}-01-code.php"), $code);
4341
}
4442

app/Commands/DetectCommand.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88

99
class DetectCommand extends Command
1010
{
11-
protected $signature = 'detect {code} {--debug} {--from-file=}';
11+
use ResolvesCode;
12+
13+
protected $signature = 'detect {code} {--from-file} {--debug} {--local-file=}';
1214

1315
protected $description = 'Detect things we care about in the current code';
1416

1517
public function handle(): void
1618
{
17-
$code = $this->argument('code');
18-
19-
if ($this->option('from-file')) {
20-
$code = file_get_contents(__DIR__ . '/../../tests/snippets/detect/' . $this->option('from-file') . '.php');
21-
}
19+
$code = $this->resolveCode('detect');
2220

2321
$walker = new DetectWalker($code, (bool) $this->option('debug'));
2422
$result = $walker->walk();
@@ -38,7 +36,7 @@ protected function log($result, $code)
3836

3937
File::put(storage_path("{$dir}/result-{$now}.json"), $result->toJson(JSON_PRETTY_PRINT));
4038

41-
if (!$this->option('from-file')) {
39+
if (!$this->option('local-file')) {
4240
File::put(storage_path("{$dir}/result-{$now}.php"), $code);
4341
}
4442
}

app/Commands/ResolvesCode.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Commands;
4+
5+
trait ResolvesCode
6+
{
7+
protected function resolveCode($path): string
8+
{
9+
if ($this->option('local-file')) {
10+
return file_get_contents(__DIR__ . '/../../tests/snippets/' . $path . '/' . $this->option('from-file') . '.php');
11+
}
12+
13+
$code = $this->argument('code');
14+
15+
if ($this->option('from-file')) {
16+
return file_get_contents($code);
17+
}
18+
19+
return $code;
20+
}
21+
}

0 commit comments

Comments
 (0)