Skip to content

Commit a0e9aaa

Browse files
committed
Refactoring more STMT and batch away
1 parent b406142 commit a0e9aaa

28 files changed

+928
-497
lines changed

lib/PHPCfg/Parser.php

Lines changed: 24 additions & 497 deletions
Large diffs are not rendered by default.

lib/PHPCfg/ParserHandler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public function handleStmt(Node\Stmt $stmt): void
3232
throw new LogicException("Stmt " . $stmt->getType() . " not Implemented Yet");
3333
}
3434

35+
public function isBatch(): bool
36+
{
37+
return false;
38+
}
39+
3540
public function getName(): string
3641
{
3742
$name = str_replace([__CLASS__ . '\\', '_'], '', get_class($this));

lib/PHPCfg/ParserHandler/Batch/AssignOp.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class AssignOp extends ParserHandler
3434
'Expr_AssignOp_ShiftRight' => Op\Expr\BinaryOp\ShiftRight::class,
3535
];
3636

37+
public function isBatch(): bool
38+
{
39+
return true;
40+
}
41+
3742
public function supports(Node $expr): bool
3843
{
3944
return isset(self::MAP[$expr->getType()]);

lib/PHPCfg/ParserHandler/Batch/BinaryOp.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class BinaryOp extends ParserHandler
4949
'Expr_BinaryOp_Spaceship' => Op\Expr\BinaryOp\Spaceship::class,
5050
];
5151

52+
public function isBatch(): bool
53+
{
54+
return true;
55+
}
56+
5257
public function supports(Node $expr): bool
5358
{
5459
return isset(self::MAP[$expr->getType()]);

lib/PHPCfg/ParserHandler/Batch/IncDec.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class IncDec extends ParserHandler
2525
'Expr_PreInc' => Op\Expr\BinaryOp\Plus::class,
2626
];
2727

28+
public function isBatch(): bool
29+
{
30+
return true;
31+
}
32+
2833
public function supports(Node $expr): bool
2934
{
3035
return isset(self::MAP[$expr->getType()]);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHP-CFG, a Control flow graph implementation for PHP
5+
*
6+
* @copyright 2015 Anthony Ferrara. All rights reserved
7+
* @license MIT See LICENSE at the root of the project for more info
8+
*/
9+
10+
namespace PHPCfg\ParserHandler\Batch;
11+
12+
use PHPCfg\ParserHandler;
13+
use PhpParser\Node;
14+
15+
class Nop extends ParserHandler
16+
{
17+
private const MAP = [
18+
// ignore use statements, since names are already resolved
19+
'Stmt_GroupUse' => true,
20+
'Stmt_Nop' => true,
21+
'Stmt_Use' => true,
22+
];
23+
24+
public function isBatch(): bool
25+
{
26+
return true;
27+
}
28+
29+
public function supports(Node $expr): bool
30+
{
31+
return isset(self::MAP[$expr->getType()]);
32+
}
33+
34+
public function handleStmt(Node\Stmt $node): void {}
35+
}

lib/PHPCfg/ParserHandler/Batch/Unary.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class Unary extends ParserHandler
3535
'Expr_UnaryPlus' => Op\Expr\UnaryPlus::class,
3636
];
3737

38+
public function isBatch(): bool
39+
{
40+
return true;
41+
}
42+
3843
public function supports(Node $expr): bool
3944
{
4045
return isset(self::MAP[$expr->getType()]);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHP-CFG, a Control flow graph implementation for PHP
5+
*
6+
* @copyright 2015 Anthony Ferrara. All rights reserved
7+
* @license MIT See LICENSE at the root of the project for more info
8+
*/
9+
10+
namespace PHPCfg\ParserHandler\Stmt;
11+
12+
use PHPCfg\Op;
13+
use PHPCfg\ParserHandler;
14+
use PhpParser\Node\Stmt;
15+
use RuntimeException;
16+
17+
class ClassConst extends ParserHandler
18+
{
19+
public function handleStmt(Stmt $node): void
20+
{
21+
if (! $this->parser->currentClass instanceof Op\Type\Literal) {
22+
throw new RuntimeException('Unknown current class');
23+
}
24+
foreach ($node->consts as $const) {
25+
$tmp = $this->block();
26+
$valueBlock = $this->block($this->createBlock());
27+
$value = $this->parser->parseExprNode($const->value);
28+
$this->block($tmp);
29+
30+
$this->addOp(new Op\Terminal\Const_(
31+
$this->parser->parseExprNode($const->name),
32+
$value,
33+
$valueBlock,
34+
$this->mapAttributes($node),
35+
));
36+
}
37+
}
38+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHP-CFG, a Control flow graph implementation for PHP
5+
*
6+
* @copyright 2015 Anthony Ferrara. All rights reserved
7+
* @license MIT See LICENSE at the root of the project for more info
8+
*/
9+
10+
namespace PHPCfg\ParserHandler\Stmt;
11+
12+
use PHPCfg\Func;
13+
use PHPCfg\Op;
14+
use PHPCfg\ParserHandler;
15+
use PhpParser\Modifiers;
16+
use PhpParser\Node\Stmt;
17+
use RuntimeException;
18+
19+
class ClassMethod extends ParserHandler
20+
{
21+
public function handleStmt(Stmt $node): void
22+
{
23+
if (! $this->parser->currentClass instanceof Op\Type\Literal) {
24+
throw new RuntimeException('Unknown current class');
25+
}
26+
27+
$this->parser->script->functions[] = $func = new Func(
28+
$node->name->toString(),
29+
$node->flags | ($node->byRef ? Func::FLAG_RETURNS_REF : 0),
30+
$this->parser->parseTypeNode($node->returnType),
31+
$this->parser->currentClass,
32+
);
33+
34+
if ($node->stmts !== null) {
35+
$this->parser->parseFunc($func, $node->params, $node->stmts, null);
36+
} else {
37+
$func->params = $this->parser->parseParameterList($func, $node->params);
38+
$func->cfg = null;
39+
}
40+
41+
$visibility = $node->flags & Modifiers::VISIBILITY_MASK;
42+
$static = $node->flags & Modifiers::STATIC;
43+
$final = $node->flags & Modifiers::FINAL;
44+
$abstract = $node->flags & Modifiers::ABSTRACT;
45+
46+
$this->addOp($class_method = new Op\Stmt\ClassMethod(
47+
$func,
48+
$visibility,
49+
(bool) $static,
50+
(bool) $final,
51+
(bool) $abstract,
52+
$this->parser->parseAttributeGroups($node->attrGroups),
53+
$this->mapAttributes($node),
54+
));
55+
$func->callableOp = $class_method;
56+
}
57+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHP-CFG, a Control flow graph implementation for PHP
5+
*
6+
* @copyright 2015 Anthony Ferrara. All rights reserved
7+
* @license MIT See LICENSE at the root of the project for more info
8+
*/
9+
10+
namespace PHPCfg\ParserHandler\Stmt;
11+
12+
use PHPCfg\ParserHandler;
13+
14+
class Declare_ extends ParserHandler {}

0 commit comments

Comments
 (0)