Skip to content

Commit 6a8fd4f

Browse files
committed
wippiest wip
1 parent 3e79655 commit 6a8fd4f

35 files changed

+652
-309
lines changed

app/Contexts/Argument.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Contexts;
4+
5+
class Argument extends BaseContext
6+
{
7+
public function type(): string
8+
{
9+
return 'argument';
10+
}
11+
12+
public function castToArray(): array
13+
{
14+
return [];
15+
}
16+
}

app/Contexts/Arguments.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Contexts;
4+
5+
class Arguments extends BaseContext
6+
{
7+
public function type(): string
8+
{
9+
return 'arguments';
10+
}
11+
12+
public function castToArray(): array
13+
{
14+
return [];
15+
}
16+
}

app/Contexts/BaseContext.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace App\Contexts;
4+
5+
abstract class BaseContext
6+
{
7+
public $children = [];
8+
9+
protected array $freshObject;
10+
11+
protected bool $hasChildren = true;
12+
13+
abstract public function castToArray(): array;
14+
15+
abstract public function type(): string;
16+
17+
public function __construct(protected $label = '')
18+
{
19+
if (method_exists($this, 'init')) {
20+
call_user_func([$this, 'init']);
21+
}
22+
23+
$this->freshObject = $this->freshArray();
24+
}
25+
26+
protected function freshArray()
27+
{
28+
return $this->toArray();
29+
}
30+
31+
public function initNew(BaseContext $newContext)
32+
{
33+
// if ($this->pristine()) {
34+
// return $this;
35+
// }
36+
37+
$this->children[] = $newContext;
38+
39+
return $newContext;
40+
}
41+
42+
public function pristine(): bool
43+
{
44+
return $this->freshObject === $this->freshArray();
45+
}
46+
47+
public function touched(): bool
48+
{
49+
return !$this->pristine();
50+
}
51+
52+
public function toArray(): array
53+
{
54+
return array_merge(
55+
$this->castToArray(),
56+
[
57+
'type' => $this->type(),
58+
],
59+
($this->label !== '') ? ['label' => $this->label] : [],
60+
($this->hasChildren) ? ['children' => array_map(fn($child) => $child->toArray(), $this->children)] : [],
61+
);
62+
}
63+
64+
public function toJson($flags = 0)
65+
{
66+
return json_encode($this->toArray(), $flags);
67+
}
68+
}

app/Contexts/ClassDefinition.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Contexts;
4+
5+
class ClassDefinition extends BaseContext
6+
{
7+
public ?string $name = null;
8+
9+
public ?string $extends = null;
10+
11+
public array $implements = [];
12+
13+
public function type(): string
14+
{
15+
return 'classDefinition';
16+
}
17+
18+
public function castToArray(): array
19+
{
20+
return [
21+
'name' => $this->name,
22+
'extends' => $this->extends,
23+
'implements' => $this->implements,
24+
];
25+
}
26+
}

app/Contexts/Generic.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Contexts;
4+
5+
class Generic extends BaseContext
6+
{
7+
8+
public function type(): string
9+
{
10+
return 'generic';
11+
}
12+
13+
public function castToArray(): array
14+
{
15+
return [];
16+
}
17+
}

app/Contexts/MethodCall.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Contexts;
4+
5+
class MethodCall extends BaseContext
6+
{
7+
public ?string $name = null;
8+
9+
public ?string $class = null;
10+
11+
public Arguments $arguments;
12+
13+
public function init()
14+
{
15+
$this->arguments = new Arguments();
16+
}
17+
18+
public function type(): string
19+
{
20+
return 'methodCall';
21+
}
22+
23+
public function castToArray(): array
24+
{
25+
return [
26+
'name' => $this->name,
27+
'class' => $this->class,
28+
'arguments' => $this->arguments->toArray(),
29+
];
30+
}
31+
}

app/Contexts/MethodDefinition.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Contexts;
4+
5+
class MethodDefinition extends BaseContext
6+
{
7+
public ?string $name = null;
8+
9+
public function type(): string
10+
{
11+
return 'methodDefinition';
12+
}
13+
14+
public function castToArray(): array
15+
{
16+
return [
17+
'name' => $this->name,
18+
];
19+
}
20+
}

app/Contexts/Parameters.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Contexts;
4+
5+
class Parameters extends BaseContext
6+
{
7+
public function type(): string
8+
{
9+
return 'parameters';
10+
}
11+
12+
public function castToArray(): array
13+
{
14+
return [];
15+
}
16+
}

app/Contexts/StringValue.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Contexts;
4+
5+
class StringValue extends BaseContext
6+
{
7+
public ?string $value = null;
8+
9+
protected bool $hasChildren = false;
10+
11+
public function type(): string
12+
{
13+
return 'string';
14+
}
15+
16+
public function castToArray(): array
17+
{
18+
return [
19+
'value' => $this->value,
20+
];
21+
}
22+
}

app/Parser/Parse.php

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,50 @@
22

33
namespace App\Parser;
44

5+
use App\Contexts\BaseContext;
6+
use App\Contexts\Generic;
57
use Microsoft\PhpParser\Node;
68
use Microsoft\PhpParser\Node\SourceFileNode;
79

810
class Parse
911
{
10-
public static function parse(Node $node, $depth = 0, $currentContext = null)
12+
public static function parse(Node $node, $depth = 0, ?BaseContext $currentContext = null)
1113
{
14+
// echo str_repeat(' ', $depth) . $node::class . ' `' . substr(str_replace("\n", ' ', $node->getText()), 0, 25) . '`' . PHP_EOL;
15+
16+
// foreach ($node->getChildNodes() as $child) {
17+
// self::parse($child, $depth + 1);
18+
// }
19+
20+
// return;
21+
1222
echo str_repeat(' ', $depth) . $node::class . PHP_EOL;
1323

1424
$class = basename(str_replace('\\', '/', $node::class));
15-
$parser = 'App\\Parser\\Parsers\\' . $class . 'Parser';
25+
$parserClass = 'App\\Parsers\\' . $class . 'Parser';
1626

17-
$context = $currentContext ?? new Context();
27+
$context = $currentContext ?? new Generic();
1828

19-
if (class_exists($parser)) {
20-
echo str_repeat(' ', $depth) . '- Parsing: ' . $parser . PHP_EOL;
29+
if (class_exists($parserClass)) {
30+
echo str_repeat(' ', $depth) . '- Parsing: ' . $parserClass . PHP_EOL;
2131
echo PHP_EOL;
2232

23-
$parser = app()->make($parser);
33+
/** @var \App\Parsers\AbstractParser */
34+
$parser = app()->make($parserClass);
35+
$parser->context($context)->depth($depth + 1);
2436

25-
if ($parser->shouldInitNewContext()) {
26-
// echo '------------ INIT NEW CONTEXT ------------ ' . PHP_EOL;
27-
// echo '------------ INIT NEW CONTEXT ------------ ' . PHP_EOL;
28-
// echo '------------ INIT NEW CONTEXT ------------ ' . PHP_EOL;
29-
// echo '------------ INIT NEW CONTEXT ------------ ' . PHP_EOL;
30-
$context = $context->initNew();
37+
if ($newContext = $parser->initNewContext()) {
38+
$context = $context->initNew($newContext);
39+
$parser->context($context);
3140
}
3241

33-
// echo $context->toJson(JSON_PRETTY_PRINT) . PHP_EOL;
34-
35-
$parser->context($context)->depth($depth + 1)->parseNode($node);
42+
$context = $parser->parseNode($node);
3643
}
3744

3845
foreach ($node->getChildNodes() as $child) {
3946
self::parse($child, $depth + 1, $context);
4047
}
4148

4249
return $context;
43-
44-
echo str_repeat(' ', $depth) . $node::class . PHP_EOL;
45-
echo str_repeat(' ', $depth) . '[code] ' . substr(str_replace("\n", ' ', $node->getText()), 0, 25) . '...' . PHP_EOL;
46-
47-
if (!class_exists($parser)) {
48-
echo PHP_EOL;
49-
return $currentContext;
50-
}
51-
52-
echo str_repeat(' ', $depth) . '- Parsing: ' . $parser . PHP_EOL;
53-
echo PHP_EOL;
54-
55-
$parser = app()->make($parser);
56-
57-
$context = $currentContext ?? new Context();
58-
59-
if ($parser->shouldInitNewContext()) {
60-
echo '------------ INIT NEW CONTEXT ------------ ' . PHP_EOL;
61-
echo '------------ INIT NEW CONTEXT ------------ ' . PHP_EOL;
62-
echo '------------ INIT NEW CONTEXT ------------ ' . PHP_EOL;
63-
echo '------------ INIT NEW CONTEXT ------------ ' . PHP_EOL;
64-
$context = $context->initNew();
65-
}
66-
67-
echo $context->toJson(JSON_PRETTY_PRINT) . PHP_EOL;
68-
69-
$result = $parser->context($context)->depth($depth + 1)->parse($node);
70-
71-
if ($node instanceof SourceFileNode) {
72-
dd($context, 'sou rce file');
73-
}
74-
75-
return $result;
7650
}
7751
}

0 commit comments

Comments
 (0)