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

AddMagicUnset   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 43
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A leaveNode() 13 13 2
A buildUnsetExpression() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}