Skip to content

trycatch support #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
45 changes: 45 additions & 0 deletions lib/PHPCfg/Op/Stmt/Catch_.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/**
* This file is part of PHP-CFG, a Control flow graph implementation for PHP
*
* @copyright 2015 Anthony Ferrara. All rights reserved
* @license MIT See LICENSE at the root of the project for more info
*/

namespace PHPCfg\Op\Stmt;

use PHPCfg\Op\Stmt;
use PhpCfg\Block;
use PHPCfg\Operand;

class Catch_ extends Stmt
{
public Operand $var;

public array $types;

public Block $body;

protected array $writeVariables = ['var'];

public function __construct(Operand $var, array $types, Block $body, array $attributes = [])
{
parent::__construct($attributes);
$this->var = $this->addWriteRef($var);
$this->types = $types;
$this->body = $body;
}

public function getVariableNames(): array
{
return ['var'];
}

public function getSubBlocks(): array
{
return ['body'];
}
}
37 changes: 37 additions & 0 deletions lib/PHPCfg/Op/Stmt/Try_.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/**
* This file is part of PHP-CFG, a Control flow graph implementation for PHP
*
* @copyright 2015 Anthony Ferrara. All rights reserved
* @license MIT See LICENSE at the root of the project for more info
*/

namespace PHPCfg\Op\Stmt;

use PHPCfg\Op\Stmt;
use PhpCfg\Block;

class Try_ extends Stmt
{
public Block $body;

public Block $finally;

public Block $catch;

public function __construct(Block $body, Block $catch, Block $finally, array $attributes = [])
{
parent::__construct($attributes);
$this->body = $body;
$this->catch = $catch;
$this->finally = $finally;
}

public function getSubBlocks(): array
{
return ['body', 'catch', 'finally'];
}
}
30 changes: 29 additions & 1 deletion lib/PHPCfg/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace PHPCfg;

use PHPCfg\Op\Stmt\Catch_;
use PHPCfg\Op\Stmt\Jump;
use PHPCfg\Op\Stmt\JumpIf;
use PHPCfg\Op\Stmt\TraitUse;
use PHPCfg\Op\Stmt\Try_;
use PHPCfg\Op\Terminal\Return_;
use PHPCfg\Op\TraitUseAdaptation\Alias;
use PHPCfg\Op\TraitUseAdaptation\Precedence;
Expand Down Expand Up @@ -722,9 +724,35 @@ protected function parseStmt_TraitUse(Stmt\TraitUse $node)
$this->block->children[] = new TraitUse($traits, $adaptations, $this->mapAttributes($node));
}

protected function parseStmt_Catch(Stmt\Catch_ $node)
{
$body = new Block($this->block);
$this->parseNodes($node->stmts, $body);

$var = $this->writeVariable($this->parseExprNode($node->var));

$parsedTypes = [];
foreach ($node->types as $type) {
$parsedTypes[] = $this->parseTypeNode($type);
}

$this->block->children[] = new Catch_($var, $parsedTypes, $body, $this->mapAttributes($node));
}

protected function parseStmt_TryCatch(Stmt\TryCatch $node)
{
// TODO: implement this!!!
$body = new Block($this->block);
$this->parseNodes($node->stmts, $body);

$catch = new Block($this->block);
$this->parseNodes($node->catches, $catch);

$finally = new Block($this->block);
if ($node->finally != null) {
$this->parseNodes($node->finally->stmts, $finally);
}

$this->block->children[] = new Try_($body, $catch, $finally, $this->mapAttributes($node));
}

protected function parseStmt_Unset(Stmt\Unset_ $node)
Expand Down
7 changes: 7 additions & 0 deletions lib/PHPCfg/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ protected function renderOp(Op $op)
}
}
}

if ($op instanceof Op\Stmt\Catch_) {
foreach ($op->types as $index => $type) {
$result .= "\n type[$index]: " . $this->indent($this->renderType($type));
}
}

if ($op instanceof Op\Stmt\ClassMethod) {
$result .= $this->renderAttrGroups($op->attrGroups);
$result .= "\n flags: " . $this->indent($this->renderFlags($op));
Expand Down
44 changes: 44 additions & 0 deletions test/code/try.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

try {
throw new MyException();
} catch (MyException | MyOtherException $e) {
var_dump(get_class($e));
}
-----
Block#1
Stmt_Try
body: Block#2
catch: Block#3
finally: Block#4
Terminal_Return

Block#2
Parent: Block#1
Expr_New
class: LITERAL('MyException')
result: Var#1
Terminal_Throw
expr: Var#1

Block#3
Parent: Block#1
Stmt_Catch
type[0]: LITERAL('MyException')
type[1]: LITERAL('MyOtherException')
var: Var#2<$e>
body: Block#5

Block#4
Parent: Block#1

Block#5
Parent: Block#3
Expr_FuncCall
name: LITERAL('get_class')
args[0]: Var#2<$e>
result: Var#3
Expr_FuncCall
name: LITERAL('var_dump')
args[0]: Var#3
result: Var#4
42 changes: 42 additions & 0 deletions test/code/try_finally.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

try {
throw new Exception('foo');
} catch (Exception $e) {
return $e;
} finally {
return 'finally';
}
-----
Block#1
Stmt_Try
body: Block#2
catch: Block#3
finally: Block#4
Terminal_Return

Block#2
Parent: Block#1
Expr_New
class: LITERAL('Exception')
args[0]: LITERAL('foo')
result: Var#1
Terminal_Throw
expr: Var#1

Block#3
Parent: Block#1
Stmt_Catch
type[0]: LITERAL('Exception')
var: Var#2<$e>
body: Block#5

Block#4
Parent: Block#1
Terminal_Return
expr: LITERAL('finally')

Block#5
Parent: Block#3
Terminal_Return
expr: Var#2<$e>