Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
->exclude('vendor')
->exclude('.git')
->exclude('coverage')
->notPath('lib/PHPCfg/Types/InternalArgInfo.php')
->in(__DIR__);

return (new PhpCsFixer\Config())
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ targets=$(shell for file in `find . -name '*Test.php' -type f -printf "%P\n"`; d


.PHONY: build
build: cs-fix test
build: cs-fix testbuild

.PHONY: cs-fix
cs-fix:
vendor/bin/php-cs-fixer fix

.PHONY: testbuild
testbuild:
XDEBUG_MODE=coverage vendor/bin/phpunit --display-deprecations

.PHONY: test
test:
XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage --display-deprecations
Expand Down
15 changes: 14 additions & 1 deletion bin/php-cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
#!/usr/bin/env php
<?php
require_once(__DIR__ . '/../vendor/autoload.php');

$startFolder = realpath(__DIR__ . '/../vendor/');

$n = 0;
while (!empty($startFolder) && !file_exists($startFolder . "/autoload.php")) {
$startFolder = dirname($startFolder);
$n++;
}
if ($n > 5) {
echo "Too deep nesting, could not find autoloader, exiting\n";
exit(1);
}

require_once($startFolder . "/autoload.php");

// Init App with name and version
$app = new Ahc\Cli\Application('PHP-CFG', 'v0.0.1');
Expand Down
2 changes: 1 addition & 1 deletion lib/PHPCfg/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use RuntimeException;

class Assertion
abstract class Assertion
{
public const MODE_NONE = 0;

Expand Down
2 changes: 2 additions & 0 deletions lib/PHPCfg/Op.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

abstract class Op
{
public ?Op\Type $scope = null;

protected array $attributes = [];

protected array $writeVariables = [];
Expand Down
8 changes: 3 additions & 5 deletions lib/PHPCfg/Operand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@

namespace PHPCfg;

use PHPTypes\Type;

abstract class Operand
{
public ?Type $type = null;
public ?Types\Type $type = null;

public array $assertions = [];

Expand Down Expand Up @@ -68,7 +66,7 @@ public function addAssertion(self $op, Assertion $assert, $mode = Assertion::MOD
foreach ($this->assertions as $key => $orig) {
if ($orig['var'] === $op) {
// Merge them
$this->assertions[$key]['assertion'] = new Assertion(
$this->assertions[$key]['assertion'] = new Assertion\TypeAssertion(
[$orig['assertion'], $assert],
$mode,
);
Expand All @@ -86,7 +84,7 @@ public function addAssertion(self $op, Assertion $assert, $mode = Assertion::MOD
}
if ($orig['var']->original->name->value === $op->original->name->value) {
// merge
$this->assertions[$key]['assertion'] = new Assertion(
$this->assertions[$key]['assertion'] = new Assertion\TypeAssertion(
[$orig['assertion'], $assert],
$mode,
);
Expand Down
3 changes: 2 additions & 1 deletion lib/PHPCfg/ParserHandler/Batch/Unary.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace PHPCfg\ParserHandler\Batch;

use PHPCfg\Assertion\NegatedAssertion;
use PHPCfg\Op;
use PHPCfg\Operand;
use PHPCfg\ParserHandler;
Expand Down Expand Up @@ -63,7 +64,7 @@ public function handleExpr(Node\Expr $expr): Operand
if ($expr instanceof Node\Expr\BooleanNot) {
// process type assertions
foreach ($cond->assertions as $assertion) {
$result->addAssertion($assertion['var'], new Assertion\NegatedAssertion([$assertion['assertion']]));
$result->addAssertion($assertion['var'], new NegatedAssertion([$assertion['assertion']]));
}
}

Expand Down
40 changes: 38 additions & 2 deletions lib/PHPCfg/ParserHandler/Stmt/Class_.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPCfg\ParserHandler;
use PHPCfg\ParserHandler\Stmt;
use PhpParser\Node;
use SplObjectStorage;

class Class_ extends ParserHandler implements Stmt
{
Expand All @@ -22,15 +23,50 @@ public function handleStmt(Node\Stmt $node): void
$old = $this->parser->currentClass;
$this->parser->currentClass = $name;

$this->addOp(new Op\Stmt\Class_(
$class = new Op\Stmt\Class_(
$name,
$node->flags,
$node->extends ? $this->parser->parseTypeNode($node->extends) : null,
$this->parser->parseTypeList(...$node->implements),
$this->parser->parseNodes($node->stmts, $this->createBlock()),
$this->parser->parseAttributeGroups(...$node->attrGroups),
$this->mapAttributes($node),
));
);

$this->addScope($class, $name);
$this->addOp($class);
$this->parser->currentClass = $old;
}

public static function addScope(Op\Stmt\ClassLike $class, Op\Type $name): void
{
$toprocess = new SplObjectStorage();
$processed = new SplObjectStorage();
$toprocess->attach($class->stmts);
while ($toprocess->count() > 0) {
$block = $toprocess->current();
$toprocess->detach($block);
$processed->attach($block);
foreach ($block->children as $op) {
$op->scope = $name;
if ($op instanceof Op\CallableOp) {
if ($op->func->cfg && !$processed->contains($op->func->cfg)) {
$toprocess->attach($op->func->cfg);
}
}
foreach ($op->getSubBlocks() as $sub) {
if (is_array($sub)) {
foreach ($sub as $s) {
if ($s && !$processed->contains($s)) {
$toprocess->attach($s);
}
}
} elseif ($sub && !$processed->contains($sub)) {
$toprocess->attach($sub);
}
}
}
$toprocess->rewind();
}
}
}
7 changes: 5 additions & 2 deletions lib/PHPCfg/ParserHandler/Stmt/Interface_.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ public function handleStmt(Node\Stmt $node): void
$name = $this->parser->parseTypeNode($node->namespacedName);
$old = $this->parser->currentClass;
$this->parser->currentClass = $name;
$this->addOp(new Op\Stmt\Interface_(
$interface = new Op\Stmt\Interface_(
$name,
$this->parser->parseTypeList(...$node->extends),
$this->parser->parseNodes($node->stmts, $this->createBlock()),
$this->parser->parseAttributeGroups(...$node->attrGroups),
$this->mapAttributes($node),
));
);
$this->addOp($interface);
Class_::addScope($interface, $name);

$this->parser->currentClass = $old;
}

Expand Down
6 changes: 4 additions & 2 deletions lib/PHPCfg/ParserHandler/Stmt/Trait_.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ public function handleStmt(Node\Stmt $node): void
$name = $this->parser->parseTypeNode($node->namespacedName);
$old = $this->parser->currentClass;
$this->parser->currentClass = $name;
$this->addOp(new Op\Stmt\Trait_(
$trait = new Op\Stmt\Trait_(
$name,
$this->parser->parseNodes($node->stmts, $this->createBlock()),
$this->parser->parseAttributeGroups(...$node->attrGroups),
$this->mapAttributes($node),
));
);
Class_::addScope($trait, $name);
$this->addOp($trait);
$this->parser->currentClass = $old;
}
}
Loading