Skip to content

Commit d952706

Browse files
committed
detect now uses parse
1 parent f5df1b8 commit d952706

File tree

5 files changed

+78
-7
lines changed

5 files changed

+78
-7
lines changed

app/Contexts/AbstractContext.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Contexts;
44

55
use Illuminate\Support\Arr;
6+
use Microsoft\PhpParser\Range;
67

78
abstract class AbstractContext
89
{
@@ -16,6 +17,10 @@ abstract class AbstractContext
1617

1718
protected ?AbstractContext $parent = null;
1819

20+
public array $start = [];
21+
22+
public array $end = [];
23+
1924
abstract public function type(): string;
2025

2126
public function castToArray(): array
@@ -138,6 +143,8 @@ public function toArray(): array
138143
$this->autocompleting ? ['autocompleting' => true] : [],
139144
$this->castToArray(),
140145
($this->label !== '') ? ['label' => $this->label] : [],
146+
(count($this->start) > 0) ? ['start' => $this->start] : [],
147+
(count($this->end) > 0) ? ['end' => $this->end] : [],
141148
($this->hasChildren)
142149
? ['children' => array_map(fn ($child) => $child->toArray(), $this->children)]
143150
: [],
@@ -148,4 +155,17 @@ public function toJson($flags = 0)
148155
{
149156
return json_encode($this->toArray(), $flags);
150157
}
158+
159+
public function setPosition(Range $range)
160+
{
161+
$this->start = [
162+
'line' => $range->start->line,
163+
'column' => $range->start->character,
164+
];
165+
166+
$this->end = [
167+
'line' => $range->end->line,
168+
'column' => $range->end->character,
169+
];
170+
}
151171
}

app/Parser/DetectWalker.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace App\Parser;
44

5+
use App\Contexts\AbstractContext;
56
use App\Support\Debugs;
7+
use Illuminate\Support\Arr;
68
use Illuminate\Support\Str;
79
use Microsoft\PhpParser\MissingToken;
810
use Microsoft\PhpParser\Node;
@@ -53,8 +55,32 @@ public function __construct(protected string $document, $debug = false)
5355
$this->context = new DetectContext;
5456
}
5557

58+
protected function handleContext(Node $node, AbstractContext $context)
59+
{
60+
$nodesToDetect = [
61+
CallExpression::class,
62+
ObjectCreationExpression::class,
63+
];
64+
65+
foreach ($nodesToDetect as $nodeClass) {
66+
if ($node instanceof $nodeClass) {
67+
$this->items[] = $context;
68+
}
69+
}
70+
}
71+
5672
public function walk(?Node $node = null)
5773
{
74+
SourceFile::$sourceFile = $this->sourceFile;
75+
Settings::$capturePosition = true;
76+
77+
Parse::parse(
78+
node: $this->sourceFile,
79+
callback: $this->handleContext(...),
80+
);
81+
82+
return collect($this->items)->map(fn ($item) => Arr::except($item->toArray(), 'children'));
83+
5884
$node = $this->nextNodeToWalk ?? $node ?? $this->sourceFile;
5985
$this->nextNodeToWalk = null;
6086

@@ -85,11 +111,11 @@ protected function parsePotentialBlade(InlineHtml $node)
85111
$this->parseBladeDirective($node);
86112
}
87113

88-
// if ($node instanceof EchoNode) {
89-
// $walker = new static('<?php ' . $node->innerContent);
90-
// TODO: Parse this and re-calc the offsets
91-
// var_dump($walker->walk());
92-
// }
114+
if ($node instanceof EchoNode) {
115+
$walker = new static('<?php ' . $node->innerContent);
116+
// TODO: Parse this and re-calc the offsets
117+
dd($node, $walker->walk());
118+
}
93119

94120
$this->debug('potential blade node', $node::class);
95121
}

app/Parser/Parse.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Parse
1010
{
1111
public static $debug = false;
1212

13-
public static function parse(Node $node, $depth = 0, ?AbstractContext $currentContext = null)
13+
public static function parse(Node $node, $depth = 0, ?AbstractContext $currentContext = null, ?callable $callback = null)
1414
{
1515
if ($currentContext === null) {
1616
self::debugBreak();
@@ -44,10 +44,14 @@ public static function parse(Node $node, $depth = 0, ?AbstractContext $currentCo
4444
self::debugBreak();
4545

4646
$context = $parser->parseNode($node);
47+
48+
if ($callback) {
49+
$callback($node, $context);
50+
}
4751
}
4852

4953
foreach ($node->getChildNodes() as $child) {
50-
self::parse($child, $depth + 1, $context);
54+
self::parse($child, $depth + 1, $context, $callback);
5155
}
5256

5357
return $context;

app/Parser/Settings.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App\Parser;
4+
5+
class Settings
6+
{
7+
public static bool $capturePosition = false;
8+
}

app/Parsers/StringLiteralParser.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
use App\Contexts\AbstractContext;
66
use App\Contexts\StringValue;
7+
use App\Parser\Settings;
8+
use App\Parser\SourceFile;
79
use Microsoft\PhpParser\Node\StringLiteral;
10+
use Microsoft\PhpParser\PositionUtilities;
811

912
class StringLiteralParser extends AbstractParser
1013
{
@@ -17,6 +20,16 @@ public function parse(StringLiteral $node)
1720
{
1821
$this->context->value = $node->getStringContentsText();
1922

23+
if (Settings::$capturePosition) {
24+
$this->context->setPosition(
25+
PositionUtilities::getRangeFromPosition(
26+
$node->getStartPosition(),
27+
mb_strlen($node->getStringContentsText()),
28+
SourceFile::fullText(),
29+
),
30+
);
31+
}
32+
2033
return $this->context;
2134
}
2235

0 commit comments

Comments
 (0)