| @@ 11-56 (lines=46) @@ | ||
| 8 | use PhpParser\Node; |
|
| 9 | use PhpParser\NodeVisitorAbstract; |
|
| 10 | ||
| 11 | class AddMagicUnset extends NodeVisitorAbstract |
|
| 12 | { |
|
| 13 | /** @var string */ |
|
| 14 | private $resolverProperty; |
|
| 15 | ||
| 16 | /** @var string */ |
|
| 17 | private $resolveMethod; |
|
| 18 | ||
| 19 | /** @var string */ |
|
| 20 | private $unsetPropertiesConst; |
|
| 21 | ||
| 22 | public function __construct(string $resolverProperty, string $resolveMethod, string $unsetPropertiesConst) |
|
| 23 | { |
|
| 24 | $this->resolveMethod = $resolveMethod; |
|
| 25 | $this->resolverProperty = $resolverProperty; |
|
| 26 | $this->unsetPropertiesConst = $unsetPropertiesConst; |
|
| 27 | } |
|
| 28 | ||
| 29 | public function leaveNode(Node $node) |
|
| 30 | { |
|
| 31 | if ($node instanceof Node\Stmt\Class_) { |
|
| 32 | $method = new Builder\Method('__unset'); |
|
| 33 | $method->makePublic(); |
|
| 34 | $method->addParam(new Builder\Param('name')); |
|
| 35 | $method->addStmt($this->buildUnsetExpression()); |
|
| 36 | ||
| 37 | $node->stmts[] = $method->getNode(); |
|
| 38 | } |
|
| 39 | ||
| 40 | return null; |
|
| 41 | } |
|
| 42 | ||
| 43 | private function buildUnsetExpression(): Node\Stmt\If_ |
|
| 44 | { |
|
| 45 | $if = new Node\Stmt\If_(Expressions::inConstArrayFunc('name', 'self', $this->unsetPropertiesConst)); |
|
| 46 | $if->stmts[] = Expressions::resolveIntoVar('entity', 'this', $this->resolverProperty, $this->resolveMethod); |
|
| 47 | $if->stmts[] = Expressions::throwExceptionOnNull( |
|
| 48 | new Node\Expr\Variable('entity'), |
|
| 49 | new Node\Stmt\Expression(Expressions::unsetFunc('entity', '{$name}')) |
|
| 50 | ); |
|
| 51 | $if->else = new Node\Stmt\Else_(); |
|
| 52 | $if->else->stmts[] = new Node\Stmt\Expression(Expressions::unsetFunc('this', '{$name}')); |
|
| 53 | ||
| 54 | return $if; |
|
| 55 | } |
|
| 56 | } |
|
| @@ 11-56 (lines=46) @@ | ||
| 8 | use PhpParser\Node; |
|
| 9 | use PhpParser\NodeVisitorAbstract; |
|
| 10 | ||
| 11 | class AddMagicIssetMethod extends NodeVisitorAbstract |
|
| 12 | { |
|
| 13 | /** @var string */ |
|
| 14 | private $resolverProperty; |
|
| 15 | ||
| 16 | /** @var string */ |
|
| 17 | private $resolveMethod; |
|
| 18 | ||
| 19 | /** @var string */ |
|
| 20 | private $unsetPropertiesProperty; |
|
| 21 | ||
| 22 | public function __construct(string $resolverProperty, string $resolveMethod, string $unsetPropertiesProperty) |
|
| 23 | { |
|
| 24 | $this->unsetPropertiesProperty = $unsetPropertiesProperty; |
|
| 25 | $this->resolveMethod = $resolveMethod; |
|
| 26 | $this->resolverProperty = $resolverProperty; |
|
| 27 | } |
|
| 28 | ||
| 29 | public function leaveNode(Node $node) |
|
| 30 | { |
|
| 31 | if ($node instanceof Node\Stmt\Class_) { |
|
| 32 | $method = new Builder\Method('__isset'); |
|
| 33 | $method->makePublic(); |
|
| 34 | $method->addParam(new Builder\Param('name')); |
|
| 35 | $method->addStmt($this->buildIssetExpression()); |
|
| 36 | ||
| 37 | $node->stmts[] = $method->getNode(); |
|
| 38 | } |
|
| 39 | ||
| 40 | return null; |
|
| 41 | } |
|
| 42 | ||
| 43 | private function buildIssetExpression(): Node\Stmt\If_ |
|
| 44 | { |
|
| 45 | $if = new Node\Stmt\If_(Expressions::inConstArrayFunc('name', 'self', $this->unsetPropertiesProperty)); |
|
| 46 | $if->stmts[] = Expressions::resolveIntoVar('entity', 'this', $this->resolverProperty, $this->resolveMethod); |
|
| 47 | $if->stmts[] = Expressions::throwExceptionOnNull( |
|
| 48 | new Node\Expr\Variable('entity'), |
|
| 49 | new Node\Stmt\Return_(Expressions::issetFunc('entity', '{$name}')) |
|
| 50 | ); |
|
| 51 | $if->else = new Node\Stmt\Else_(); |
|
| 52 | $if->else->stmts[] = new Node\Stmt\Return_(Expressions::issetFunc('this', '{$name}')); |
|
| 53 | ||
| 54 | return $if; |
|
| 55 | } |
|
| 56 | } |
|