-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathVariableParser.php
58 lines (46 loc) · 1.42 KB
/
VariableParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace App\Parsers;
use App\Contexts\AbstractContext;
use App\Contexts\Variable as VariableContext;
use App\Parser\Settings;
use Microsoft\PhpParser\Node\Expression\Variable;
use Microsoft\PhpParser\PositionUtilities;
class VariableParser extends AbstractParser
{
/**
* @var VariableContext
*/
protected AbstractContext $context;
/**
* Check if the node has a object operator and
* is a last element in the string
*/
private function hasObjectOperator(Variable $node): bool
{
$name = $node->getName();
return preg_match('/\$' . $name . '->;$/s', $node->getFileContents());
}
public function parse(Variable $node)
{
if ($this->hasObjectOperator($node)) {
$this->context->autocompleting = true;
}
$this->context->name = $node->getName();
if (Settings::$capturePosition) {
$range = PositionUtilities::getRangeFromPosition(
$node->getStartPosition(),
mb_strlen($node->getText()),
$node->getRoot()->getFullText(),
);
if (Settings::$calculatePosition !== null) {
$range = Settings::adjustPosition($range);
}
$this->context->setPosition($range);
}
return $this->context;
}
public function initNewContext(): ?AbstractContext
{
return new VariableContext;
}
}