Skip to content

Commit

Permalink
attributes are not expr
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-therond committed May 24, 2024
1 parent a6b9dd8 commit 1557195
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
* @license MIT See LICENSE at the root of the project for more info
*/

namespace PHPCfg\Op\Expr;
namespace PHPCfg\Op\Attributes;

use PHPCfg\Op\Expr;
use PHPCfg\Op;
use PhpCfg\Operand;

class Attribute extends Expr
class Attribute extends Op
{
public Operand $name;

Expand All @@ -26,9 +26,4 @@ public function __construct(Operand $name, array $args, array $attributes = [])
$this->name = $this->addReadRef($name);
$this->args = $args;
}

public function getVariableNames(): array
{
return ['name', 'args', 'result'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
* @license MIT See LICENSE at the root of the project for more info
*/

namespace PHPCfg\Op\Expr;
namespace PHPCfg\Op\Attributes;

use PHPCfg\Op\Expr;
use PHPCfg\Op;

class AttributeGroup extends Expr
class AttributeGroup extends Op
{
public array $attrs;

Expand All @@ -22,9 +22,4 @@ public function __construct(array $attrs, array $attributes = [])
parent::__construct($attributes);
$this->attrs = $attrs;
}

public function getVariableNames(): array
{
return ['attrs', 'result'];
}
}
2 changes: 1 addition & 1 deletion lib/PHPCfg/Op/Expr/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct(

public function getVariableNames(): array
{
return ['name', 'attrGroups', 'defaultVar', 'result'];
return ['name', 'defaultVar', 'result'];
}

public function getSubBlocks(): array
Expand Down
2 changes: 1 addition & 1 deletion lib/PHPCfg/Op/Stmt/Class_.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public function __construct(Operand $name, int $flags, ?Operand $extends, array

public function getVariableNames(): array
{
return ['name', 'attrGroups', 'extends', 'implements'];
return ['name', 'extends', 'implements'];
}
}
5 changes: 0 additions & 5 deletions lib/PHPCfg/Op/Stmt/Function_.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,4 @@ public function getFunc(): Func
{
return $this->func;
}

public function getVariableNames(): array
{
return ['attrGroups'];
}
}
2 changes: 1 addition & 1 deletion lib/PHPCfg/Op/Stmt/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function isReadonly() : bool

public function getVariableNames(): array
{
return ['name', 'attrGroups', 'defaultVar'];
return ['name', 'defaultVar'];
}

public function getSubBlocks(): array
Expand Down
23 changes: 15 additions & 8 deletions lib/PHPCfg/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ protected function parseNode(Node $node)

return;
}

$type = $node->getType();
if (method_exists($this, 'parse'.$type)) {
$this->{'parse'.$type}($node);
Expand Down Expand Up @@ -232,7 +233,7 @@ protected function parseStmt_Class(Stmt\Class_ $node)
$this->parseExprNode($node->extends),
$this->parseExprList($node->implements),
$this->parseNodes($node->stmts, new Block()),
$this->parseExprList($node->attrGroups),
$this->parseAttributeGroups($node->attrGroups),
$this->mapAttributes($node)
);
$this->currentClass = $old;
Expand Down Expand Up @@ -289,7 +290,7 @@ protected function parseStmt_ClassMethod(Stmt\ClassMethod $node)
(bool) $static,
(bool) $final,
(bool) $abstract,
$this->parseExprList($node->attrGroups),
$this->parseAttributeGroups($node->attrGroups),
$this->mapAttributes($node)
);
$func->callableOp = $class_method;
Expand Down Expand Up @@ -424,7 +425,7 @@ protected function parseStmt_Function(Stmt\Function_ $node)
null,
);
$this->parseFunc($func, $node->params, $node->stmts, null);
$this->block->children[] = $function = new Op\Stmt\Function_($func, $this->parseExprList($node->attrGroups), $this->mapAttributes($node));
$this->block->children[] = $function = new Op\Stmt\Function_($func, $this->parseAttributeGroups($node->attrGroups), $this->mapAttributes($node));
$func->callableOp = $function;
}

Expand Down Expand Up @@ -574,12 +575,13 @@ protected function parseStmt_Property(Stmt\Property $node)
$defaultVar = null;
$defaultBlock = null;
}

$this->block->children[] = new Op\Stmt\Property(
$this->parseExprNode($prop->name),
$visibility,
(bool) $static,
(bool) $readonly,
$this->parseExprList($node->attrGroups),
$this->parseAttributeGroups($node->attrGroups),
$this->parseTypeNode($node->type),
$defaultVar,
$defaultBlock,
Expand Down Expand Up @@ -941,14 +943,19 @@ protected function parseAttribute(Node\Attribute $attr)
{
$args = array_map([$this, 'parseArg'], $attr->args);

return new Op\Expr\Attribute($this->readVariable($this->parseExprNode($attr->name)), $args, $this->mapAttributes($attr));
return new Op\Attributes\Attribute($this->readVariable($this->parseExprNode($attr->name)), $args, $this->mapAttributes($attr));
}

protected function parseAttributeGroup(Node\AttributeGroup $attrGroup)
{
$attrs = $this->parseExprList($attrGroup->attrs);
$attrs = array_map([$this, 'parseAttribute'], $attrGroup->attrs);

return new Op\Expr\AttributeGroup($attrs, $this->mapAttributes($attrGroup));
return new Op\Attributes\AttributeGroup($attrs, $this->mapAttributes($attrGroup));
}

protected function parseAttributeGroups(array $attrGroups)
{
return array_map([$this, 'parseAttributeGroup'], $attrGroups);
}

protected function parseExpr_Array(Expr\Array_ $expr)
Expand Down Expand Up @@ -1570,7 +1577,7 @@ private function parseParameterList(Func $func, array $params)
$this->parseTypeNode($param->type),
$param->byRef,
$param->variadic,
$this->parseExprList($param->attrGroups),
$this->parseAttributeGroups($param->attrGroups),
$defaultVar,
$defaultBlock,
$this->mapAttributes($param)
Expand Down
27 changes: 27 additions & 0 deletions lib/PHPCfg/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ protected function renderOp(Op $op)

$result .= $this->renderAttributes($op->getAttributes());

if ($op instanceof Op\Stmt\Function_) {
$result .= $this->renderAttrGroups($op->attrGroups);
}

if ($op instanceof Op\Stmt\Property) {
$result .= $this->renderAttrGroups($op->attrGroups);
$result .= "\n flags: " . $this->indent($this->renderFlags($op));
$result .= "\n declaredType: " . $this->indent($this->renderType($op->declaredType));
}
Expand Down Expand Up @@ -163,9 +168,11 @@ protected function renderOp(Op $op)
}
}
if ($op instanceof Op\Stmt\ClassMethod) {
$result .= $this->renderAttrGroups($op->attrGroups);
$result .= "\n flags: " . $this->indent($this->renderFlags($op));
}
if ($op instanceof Op\Expr\Param) {
$result .= $this->renderAttrGroups($op->attrGroups);
$result .= "\n declaredType: " . $this->indent($this->renderType($op->declaredType));
}
if ($op instanceof Op\Expr\Include_) {
Expand Down Expand Up @@ -389,4 +396,24 @@ public function renderAttributes(array $attributes): string

return $result;
}

public function renderAttrGroups(array $attrGroups): string
{
$result = '';

foreach($attrGroups as $indexGroup => $attrGroup) {
$result .= "\n attrGroup[$indexGroup]: ";
$result .= $this->indent($this->renderAttributes($attrGroup->getAttributes()));
foreach($attrGroup->attrs as $indexAttr => $attr) {
$result .= "\n attr[$indexAttr]: ";
$result .= $this->indent($this->renderAttributes($attr->getAttributes()), 2);
$result .= "\n name: ".$this->renderOperand($attr->name);
foreach($attr->args as $indexArg => $arg) {
$result .= "\n args[$indexArg]: ".$this->renderOperand($arg);
}
}
}

return $result;
}
}
22 changes: 9 additions & 13 deletions test/PHPCfg/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,19 @@ function foowithattribute(\$a) {
attribute['filename']: foo.php
attribute['startLine']: 2
attribute['endLine']: 4
Expr_Attribute
attribute['filename']: foo.php
attribute['startLine']: 6
attribute['endLine']: 6
name: LITERAL('Attr')
result: Var#1
Expr_AttributeGroup
attribute['filename']: foo.php
attribute['startLine']: 6
attribute['endLine']: 6
attrs[0]: Var#1
result: Var#2
Stmt_Function<'foowithattribute'>
attribute['filename']: foo.php
attribute['startLine']: 6
attribute['endLine']: 9
attrGroups[0]: Var#2
attrGroup[0]:
attribute['filename']: foo.php
attribute['startLine']: 6
attribute['endLine']: 6
attr[0]:
attribute['filename']: foo.php
attribute['startLine']: 6
attribute['endLine']: 6
name: LITERAL('Attr')
Terminal_Return
Function 'foo': mixed
Expand Down
16 changes: 4 additions & 12 deletions test/code/anonymous_class.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,16 @@ Block#1
var: Var#2<$var>
expr: Var#1
result: Var#3
Expr_Attribute
name: LITERAL('Attr')
args[0]: LITERAL('foo')
result: Var#4
Expr_AttributeGroup
attrs[0]: Var#4
result: Var#5
Stmt_Class
name: LITERAL('{anonymousClass}#2')
attrGroups[0]: Var#5
stmts: Block#3
Expr_New
class: LITERAL('{anonymousClass}#2')
result: Var#6
result: Var#4
Expr_Assign
var: Var#7<$instance>
expr: Var#6
result: Var#8
var: Var#5<$instance>
expr: Var#4
result: Var#6
Terminal_Return

Block#2
Expand Down
Loading

0 comments on commit 1557195

Please sign in to comment.