Skip to content
Merged
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
26 changes: 25 additions & 1 deletion lib/PHPCfg/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,47 @@ class Block
/** @var Block[] */
public $parents = [];

public ?CatchTarget $catchTarget;

/** @var Op\Phi[] */
public $phi = [];

public $dead = false;

public function __construct(?self $parent = null)
public function __construct(?self $parent = null, ?CatchTarget $catchTarget = null)
{
if ($parent) {
$this->parents[] = $parent;
}
$this->catchTarget = $catchTarget;
if ($parent && !$catchTarget) {
$this->catchTarget = $parent->catchTarget;
}

$this->setCatchTargetParents();
}

public function create()
{
return new static();
}

public function setCatchTarget(?CatchTarget $catchTarget)
{
$this->catchTarget = $catchTarget;
$this->setCatchTargetParents();
}

public function setCatchTargetParents()
{
if ($this->catchTarget) {
$this->catchTarget->finally->addParent($this);
foreach ($this->catchTarget->catches as $catch) {
$catch["block"]->addParent($this);
}
}
}

public function addParent(self $parent)
{
if (! in_array($parent, $this->parents, true)) {
Expand Down
30 changes: 30 additions & 0 deletions lib/PHPCfg/CatchTarget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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;

class CatchTarget
{
public array $catches = [];
public Block $finally;

public function __construct(Block $finally) {
$this->finally = $finally;
}

public function addCatch(Op $type, Operand $var, Block $block) {
$this->catches[] = [
"type" => $type,
"var" => $var,
"block" => $block,
];
}
}
2 changes: 1 addition & 1 deletion lib/PHPCfg/Op/Expr/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ public function getVariableNames(): array

public function getSubBlocks(): array
{
return ['defaultBlock'];
return ['defaultBlock' => $this->defaultBlock];
}
}
1 change: 0 additions & 1 deletion lib/PHPCfg/Op/Phi.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,4 @@ public function getVariableNames(): array
{
return ['vars', 'result'];
}

}
2 changes: 1 addition & 1 deletion lib/PHPCfg/Op/Stmt/ClassLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public function __construct(Operand $name, Block $stmts, array $attributes = [])

public function getSubBlocks(): array
{
return ['stmts'];
return ['stmts' => $this->stmts];
}
}
2 changes: 1 addition & 1 deletion lib/PHPCfg/Op/Stmt/Jump.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public function __construct(Block $target, array $attributes = [])

public function getSubBlocks(): array
{
return ['target'];
return ['target' => $this->target];
}
}
2 changes: 1 addition & 1 deletion lib/PHPCfg/Op/Stmt/JumpIf.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public function getVariableNames(): array

public function getSubBlocks(): array
{
return ['if', 'else'];
return ['if' => $this->if, 'else' => $this->else];
}
}
4 changes: 2 additions & 2 deletions lib/PHPCfg/Op/Stmt/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Property extends Stmt

public ?Block $defaultBlock = null;

public Op\Type $declaredType ;
public Op\Type $declaredType;

public function __construct(Operand $name, int $visiblity, bool $static, bool $readonly, array $attrGroups, ?Op\Type $declaredType = null, ?Operand $defaultVar = null, ?Block $defaultBlock = null, array $attributes = [])
{
Expand Down Expand Up @@ -82,6 +82,6 @@ public function getVariableNames(): array

public function getSubBlocks(): array
{
return ['defaultBlock'];
return ['defaultBlock' => $this->defaultBlock];
}
}
2 changes: 1 addition & 1 deletion lib/PHPCfg/Op/Stmt/Switch_.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public function getVariableNames(): array

public function getSubBlocks(): array
{
return ['targets', 'default'];
return ['targets' => $this->targets, 'default' => $this->default];
}
}
52 changes: 52 additions & 0 deletions lib/PHPCfg/Op/Stmt/Try_.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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 array $catch;
public array $catchTypes;
public array $catchVars;
public Block $finally;

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

$this->catch = [];
$this->catchTypes = [];
$this->catchVars = [];
foreach ($catches as $catch) {
$this->catch[] = $catch['block'];
$this->catchTypes[] = $catch['type'];
$this->catchVars[] = $catch['var'];
}

$this->finally = $finally;
}

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

public function getSubBlocks(): array
{
return ['body' => $this->body, 'catch' => $this->catch, 'finally' => $this->finally];
}
}
2 changes: 1 addition & 1 deletion lib/PHPCfg/Op/Terminal/Const_.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public function getVariableNames(): array

public function getSubBlocks(): array
{
return ['valueBlock'];
return ['valueBlock' => $this->valueBlock];
}
}
2 changes: 1 addition & 1 deletion lib/PHPCfg/Op/Terminal/StaticVar.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public function getVariableNames(): array

public function getSubBlocks(): array
{
return ['defaultBlock'];
return ['defaultBlock' => $this->defaultBlock];
}
}
Loading