Skip to content

Commit 9416f63

Browse files
committed
Switch to trait based implementation of Attributable
1 parent beff271 commit 9416f63

File tree

7 files changed

+51
-44
lines changed

7 files changed

+51
-44
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of PHP-CFG, a Control flow graph implementation for PHP
7+
*
8+
* @copyright 2015 Anthony Ferrara. All rights reserved
9+
* @license MIT See LICENSE at the root of the project for more info
10+
*/
11+
12+
namespace PHPCfg\Op\Attributes;
13+
14+
use PHPCfg\Op;
15+
use PhpCfg\Operand;
16+
17+
trait Attributable
18+
{
19+
private array $attrGroups = [];
20+
21+
public function setAttributeGroups(array $attrGroups)
22+
{
23+
$this->attrGroups = $attrGroups;
24+
}
25+
26+
public function getAttributeGroups(): array
27+
{
28+
return $this->attrGroups;
29+
}
30+
31+
}

lib/PHPCfg/Op/Expr/Param.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,21 @@
1313

1414
use PHPCfg\Block;
1515
use PHPCfg\Op;
16+
use PHPCfg\Op\Attributes\Attributable;
1617
use PHPCfg\Op\AttributableOp;
1718
use PHPCfg\Op\Expr;
1819
use PhpCfg\Operand;
1920

2021
class Param extends Expr implements AttributableOp
2122
{
23+
use Attributable;
24+
2225
public Operand $name;
2326

2427
public bool $byRef;
2528

2629
public bool $variadic;
2730

28-
public array $attrGroups;
29-
3031
public ?Operand $defaultVar = null;
3132

3233
public ?Block $defaultBlock = null;
@@ -52,7 +53,7 @@ public function __construct(
5253
$this->declaredType = $type;
5354
$this->byRef = $byRef;
5455
$this->variadic = $variadic;
55-
$this->attrGroups = $attrGroups;
56+
$this->setAttributeGroups($attrGroups);
5657
if (!is_null($defaultVar)) {
5758
$this->defaultVar = $this->addReadRef($defaultVar);
5859
}
@@ -73,9 +74,4 @@ public function getSubBlocks(): array
7374
{
7475
return ['defaultBlock' => $this->defaultBlock];
7576
}
76-
77-
public function getAttributeGroups(): array
78-
{
79-
return $this->attrGroups;
80-
}
8177
}

lib/PHPCfg/Op/Stmt/ClassMethod.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
namespace PHPCfg\Op\Stmt;
1313

1414
use PHPCfg\Func;
15-
use PHPCfg\Op\AttributableOp;
1615
use PhpParser\Modifiers;
1716

18-
class ClassMethod extends Function_ implements AttributableOp
17+
class ClassMethod extends Function_
1918
{
2019
public int $visibility;
2120

@@ -25,8 +24,6 @@ class ClassMethod extends Function_ implements AttributableOp
2524

2625
public bool $abstract;
2726

28-
public array $attrGroups;
29-
3027
public function __construct(Func $func, int $visiblity, bool $static, bool $final, bool $abstract, array $attrGroups, array $attributes = [])
3128
{
3229
parent::__construct($func, $attrGroups, $attributes);
@@ -65,9 +62,4 @@ public function isStatic(): bool
6562
{
6663
return $this->static;
6764
}
68-
69-
public function getAttributeGroups(): array
70-
{
71-
return $this->attrGroups;
72-
}
7365
}

lib/PHPCfg/Op/Stmt/Class_.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,27 @@
1212
namespace PHPCfg\Op\Stmt;
1313

1414
use PHPCfg\Block;
15+
use PHPCfg\Op\Attributes\Attributable;
1516
use PHPCfg\Op\AttributableOp;
1617
use PHPCfg\Op;
1718

1819
class Class_ extends ClassLike implements AttributableOp
1920
{
21+
use Attributable;
22+
2023
public int $flags;
2124

2225
public ?Op\Type $extends;
2326

2427
public array $implements;
2528

26-
public array $attrGroups;
27-
2829
public function __construct(Op\Type\Literal $name, int $flags, ?Op\Type $extends, array $implements, Block $stmts, array $attrGroups, array $attributes = [])
2930
{
3031
parent::__construct($name, $stmts, $attributes);
3132
$this->flags = $flags;
3233
$this->extends = $extends;
3334
$this->implements = $implements;
34-
$this->attrGroups = $attrGroups;
35-
}
36-
37-
public function getAttributeGroups(): array
38-
{
39-
return $this->attrGroups;
35+
$this->setAttributeGroups($attrGroups);
4036
}
4137

4238
public function getTypeNames(): array

lib/PHPCfg/Op/Stmt/Function_.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,20 @@
1414
use PHPCfg\Func;
1515
use PHPCfg\Op\CallableOp;
1616
use PHPCfg\Op\AttributableOp;
17+
use PHPCfg\Op\Attributes\Attributable;
1718
use PHPCfg\Op\Stmt;
1819

1920
class Function_ extends Stmt implements CallableOp, AttributableOp
2021
{
21-
public Func $func;
22+
use Attributable;
2223

23-
public array $attrGroups;
24+
public Func $func;
2425

2526
public function __construct(Func $func, array $attrGroups, array $attributes = [])
2627
{
2728
parent::__construct($attributes);
2829
$this->func = $func;
29-
$this->attrGroups = $attrGroups;
30-
}
31-
32-
public function getAttributeGroups(): array
33-
{
34-
return $this->attrGroups;
30+
$this->setAttributeGroups($attrGroups);
3531
}
3632

3733
public function getFunc(): Func

lib/PHPCfg/Op/Stmt/Property.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
use PHPCfg\Block;
1515
use PHPCfg\Op\Stmt;
1616
use PHPCfg\Op;
17+
use PHPCfg\Op\Attributes\Attributable;
1718
use PHPCfg\Op\AttributableOp;
1819
use PHPCfg\Operand;
1920
use PhpParser\Modifiers;
2021

2122
class Property extends Stmt implements AttributableOp
2223
{
24+
use Attributable;
25+
2326
public Operand $name;
2427

2528
public int $visibility;
@@ -28,8 +31,6 @@ class Property extends Stmt implements AttributableOp
2831

2932
public bool $readonly;
3033

31-
public array $attrGroups;
32-
3334
public ?Operand $defaultVar = null;
3435

3536
public ?Block $defaultBlock = null;
@@ -43,7 +44,7 @@ public function __construct(Operand $name, int $visiblity, bool $static, bool $r
4344
$this->visibility = $visiblity;
4445
$this->static = $static;
4546
$this->readonly = $readonly;
46-
$this->attrGroups = $attrGroups;
47+
$this->setAttributeGroups($attrGroups);
4748
$this->declaredType = $declaredType;
4849
if (!is_null($defaultVar)) {
4950
$this->defaultVar = $this->addReadRef($defaultVar);
@@ -90,9 +91,4 @@ public function getSubBlocks(): array
9091
{
9192
return ['defaultBlock' => $this->defaultBlock];
9293
}
93-
94-
public function getAttributeGroups(): array
95-
{
96-
return $this->attrGroups;
97-
}
9894
}

lib/PHPCfg/Printer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected function renderOp(Op $op)
122122
$result .= $this->renderAttributes($op->getAttributes());
123123

124124
if ($op instanceof Op\AttributableOp) {
125-
$result .= $this->renderAttrGroups($op->attrGroups);
125+
$result .= $this->renderAttrGroups($op);
126126
}
127127

128128
if ($op instanceof Op\Stmt\Property || $op instanceof Op\Stmt\ClassMethod) {
@@ -401,11 +401,11 @@ public function renderAttributes(array $attributes): string
401401
return $result;
402402
}
403403

404-
public function renderAttrGroups(array $attrGroups): string
404+
public function renderAttrGroups(Op\AttributableOp $op): string
405405
{
406406
$result = '';
407407

408-
foreach ($attrGroups as $indexGroup => $attrGroup) {
408+
foreach ($op->getAttributeGroups() as $indexGroup => $attrGroup) {
409409
$result .= "\n attrGroup[$indexGroup]: ";
410410
$result .= $this->indent($this->renderAttributes($attrGroup->getAttributes()));
411411
foreach ($attrGroup->attrs as $indexAttr => $attr) {

0 commit comments

Comments
 (0)