Passed
Push — master ( 327a11...1a5769 )
by Valentin
58s
created

AddMagicUnset::buildUnsetExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Cycle\ORM\Promise\Visitor;
5
6
use Cycle\ORM\Promise\Expressions;
7
use PhpParser\Builder;
8
use PhpParser\Node;
9
use PhpParser\NodeVisitorAbstract;
10
11 View Code Duplication
class AddMagicUnset extends NodeVisitorAbstract
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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->unsetPropertiesConst = $unsetPropertiesConst;
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('__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::unsetFunc('entity', '{$name}');
48
        $if->else = new Node\Stmt\Else_();
49
        $if->else->stmts[] = Expressions::unsetFunc('this', '{$name}');
50
51
        return $if;
52
    }
53
}