Skip to content

Commit a8b28a5

Browse files
committed
Bring in PHP-Types into PHP-CFG, work on getting up to speed and running for library
1 parent 413c556 commit a8b28a5

34 files changed

+10986
-53
lines changed

.php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
->exclude('vendor')
1313
->exclude('.git')
1414
->exclude('coverage')
15+
->notPath('lib/PHPCfg/Types/InternalArgInfo.php')
1516
->in(__DIR__);
1617

1718
return (new PhpCsFixer\Config())

bin/php-cfg

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
#!/usr/bin/env php
22
<?php
3-
require_once(__DIR__ . '/../vendor/autoload.php');
3+
4+
$startFolder = realpath(__DIR__ . '/../vendor/');
5+
6+
$n = 0;
7+
while (!empty($startFolder) && !file_exists($startFolder . "/autoload.php")) {
8+
$startFolder = dirname($startFolder);
9+
$n++;
10+
}
11+
if ($n > 5) {
12+
echo "Too deep nesting, could not find autoloader, exiting\n";
13+
exit(1);
14+
}
15+
16+
require_once($startFolder . "/autoload.php");
417

518
// Init App with name and version
619
$app = new Ahc\Cli\Application('PHP-CFG', 'v0.0.1');

lib/PHPCfg/Assertion.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use RuntimeException;
1515

16-
class Assertion
16+
abstract class Assertion
1717
{
1818
public const MODE_NONE = 0;
1919

lib/PHPCfg/AssertionTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,35 @@ public function testEmptyAssertion()
2323
$this->expectException(RuntimeException::class);
2424
$this->expectExceptionMessage("Empty value supplied for Assertion");
2525

26-
new Assertion([]);
26+
new Assertion\TypeAssertion([]);
2727
}
2828

2929
public function testInvalidAssertion()
3030
{
3131
$this->expectException(RuntimeException::class);
3232
$this->expectExceptionMessage("Invalid array key supplied for Assertion");
3333

34-
new Assertion([123]);
34+
new Assertion\TypeAssertion([123]);
3535
}
3636

3737
public function testInvalidModeArray()
3838
{
3939
$this->expectException(RuntimeException::class);
4040
$this->expectExceptionMessage("Invalid mode supplied for Assertion");
4141

42-
new Assertion([new Assertion(new class extends Operand {})], 99);
42+
new Assertion\TypeAssertion([new Assertion\TypeAssertion(new class extends Operand {})], 99);
4343
}
4444

4545
public function testValidModeArray()
4646
{
47-
$assert = new Assertion([new Assertion(new class extends Operand {})], Assertion::MODE_INTERSECTION);
47+
$assert = new Assertion\TypeAssertion([new Assertion\TypeAssertion(new class extends Operand {})], Assertion::MODE_INTERSECTION);
4848
$this->assertEquals(Assertion::MODE_INTERSECTION, $assert->mode);
4949
}
5050

5151
public function testGetKind()
5252
{
53-
$assert = new Assertion([new Assertion(new class extends Operand {})], Assertion::MODE_INTERSECTION);
54-
$this->assertEquals('', $assert->getKind());
53+
$assert = new Assertion\TypeAssertion([new Assertion\TypeAssertion(new class extends Operand {})], Assertion::MODE_INTERSECTION);
54+
$this->assertEquals('type', $assert->getKind());
5555
}
5656

5757
public function testInvalidMode()

lib/PHPCfg/Op.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
abstract class Op
1515
{
16+
public ?Op\Type $scope = null;
17+
1618
protected array $attributes = [];
1719

1820
protected array $writeVariables = [];

lib/PHPCfg/Operand.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111

1212
namespace PHPCfg;
1313

14-
use PHPTypes\Type;
15-
1614
abstract class Operand
1715
{
18-
public ?Type $type = null;
16+
public ?Types\Type $type = null;
1917

2018
public array $assertions = [];
2119

@@ -68,7 +66,7 @@ public function addAssertion(self $op, Assertion $assert, $mode = Assertion::MOD
6866
foreach ($this->assertions as $key => $orig) {
6967
if ($orig['var'] === $op) {
7068
// Merge them
71-
$this->assertions[$key]['assertion'] = new Assertion(
69+
$this->assertions[$key]['assertion'] = new Assertion\TypeAssertion(
7270
[$orig['assertion'], $assert],
7371
$mode,
7472
);
@@ -86,7 +84,7 @@ public function addAssertion(self $op, Assertion $assert, $mode = Assertion::MOD
8684
}
8785
if ($orig['var']->original->name->value === $op->original->name->value) {
8886
// merge
89-
$this->assertions[$key]['assertion'] = new Assertion(
87+
$this->assertions[$key]['assertion'] = new Assertion\TypeAssertion(
9088
[$orig['assertion'], $assert],
9189
$mode,
9290
);

lib/PHPCfg/ParserHandler/Batch/Unary.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace PHPCfg\ParserHandler\Batch;
1111

1212
use PHPCfg\Op;
13+
use PHPCfg\Assertion\NegatedAssertion;
1314
use PHPCfg\Operand;
1415
use PHPCfg\ParserHandler;
1516
use PHPCfg\ParserHandler\Batch;
@@ -63,7 +64,7 @@ public function handleExpr(Node\Expr $expr): Operand
6364
if ($expr instanceof Node\Expr\BooleanNot) {
6465
// process type assertions
6566
foreach ($cond->assertions as $assertion) {
66-
$result->addAssertion($assertion['var'], new Assertion\NegatedAssertion([$assertion['assertion']]));
67+
$result->addAssertion($assertion['var'], new NegatedAssertion([$assertion['assertion']]));
6768
}
6869
}
6970

lib/PHPCfg/ParserHandler/Stmt/Class_.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
namespace PHPCfg\ParserHandler\Stmt;
1111

12+
use PHPCfg\Block;
1213
use PHPCfg\Op;
14+
use PHPCfg\Operand;
1315
use PHPCfg\ParserHandler;
1416
use PHPCfg\ParserHandler\Stmt;
1517
use PhpParser\Node;
@@ -22,15 +24,50 @@ public function handleStmt(Node\Stmt $node): void
2224
$old = $this->parser->currentClass;
2325
$this->parser->currentClass = $name;
2426

25-
$this->addOp(new Op\Stmt\Class_(
27+
$class = new Op\Stmt\Class_(
2628
$name,
2729
$node->flags,
2830
$node->extends ? $this->parser->parseTypeNode($node->extends) : null,
2931
$this->parser->parseTypeList(...$node->implements),
3032
$this->parser->parseNodes($node->stmts, $this->createBlock()),
3133
$this->parser->parseAttributeGroups(...$node->attrGroups),
3234
$this->mapAttributes($node),
33-
));
35+
);
36+
37+
$this->addScope($class, $name);
38+
$this->addOp($class);
3439
$this->parser->currentClass = $old;
3540
}
41+
42+
public static function addScope(Op\Stmt\ClassLike $class, Op\Type $name): void
43+
{
44+
$toprocess = new \SplObjectStorage;
45+
$processed = new \SplObjectStorage;
46+
$toprocess->attach($class->stmts);
47+
while ($toprocess->count() > 0) {
48+
$block = $toprocess->current();
49+
$toprocess->detach($block);
50+
$processed->attach($block);
51+
foreach ($block->children as $op) {
52+
$op->scope = $name;
53+
if ($op instanceof Op\CallableOp) {
54+
if ($op->func->cfg && !$processed->contains($op->func->cfg)) {
55+
$toprocess->attach($op->func->cfg);
56+
}
57+
}
58+
foreach ($op->getSubBlocks() as $sub) {
59+
if (is_array($sub)) {
60+
foreach ($sub as $s) {
61+
if ($s && !$processed->contains($s)) {
62+
$toprocess->attach($s);
63+
}
64+
}
65+
} elseif ($sub && !$processed->contains($sub)) {
66+
$toprocess->attach($sub);
67+
}
68+
}
69+
}
70+
$toprocess->rewind();
71+
}
72+
}
3673
}

lib/PHPCfg/ParserHandler/Stmt/Interface_.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ public function handleStmt(Node\Stmt $node): void
2222
$name = $this->parser->parseTypeNode($node->namespacedName);
2323
$old = $this->parser->currentClass;
2424
$this->parser->currentClass = $name;
25-
$this->addOp(new Op\Stmt\Interface_(
25+
$interface = new Op\Stmt\Interface_(
2626
$name,
2727
$this->parser->parseTypeList(...$node->extends),
2828
$this->parser->parseNodes($node->stmts, $this->createBlock()),
2929
$this->parser->parseAttributeGroups(...$node->attrGroups),
3030
$this->mapAttributes($node),
31-
));
31+
);
32+
$this->addOp($interface);
33+
Class_::addScope($interface, $name);
34+
3235
$this->parser->currentClass = $old;
3336
}
3437

lib/PHPCfg/ParserHandler/Stmt/Trait_.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ public function handleStmt(Node\Stmt $node): void
2121
$name = $this->parser->parseTypeNode($node->namespacedName);
2222
$old = $this->parser->currentClass;
2323
$this->parser->currentClass = $name;
24-
$this->addOp(new Op\Stmt\Trait_(
24+
$trait = new Op\Stmt\Trait_(
2525
$name,
2626
$this->parser->parseNodes($node->stmts, $this->createBlock()),
2727
$this->parser->parseAttributeGroups(...$node->attrGroups),
2828
$this->mapAttributes($node),
29-
));
29+
);
30+
Class_::addScope($trait, $name);
31+
$this->addOp($trait);
3032
$this->parser->currentClass = $old;
3133
}
3234
}

0 commit comments

Comments
 (0)