AddMagicUnset   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 73
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
A leaveNode() 13 13 2
A buildUnsetExpression() 19 19 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
3
/**
4
 * Spiral Framework. Cycle ProxyFactory
5
 *
6
 * @license MIT
7
 * @author  Valentin V (Vvval)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\ORM\Promise\Visitor;
13
14
use PhpParser\Builder;
15
use PhpParser\Node;
16
use PhpParser\NodeVisitorAbstract;
17
18
use function Cycle\ORM\Promise\exprUnsetFunc;
19
use function Cycle\ORM\Promise\ifInConstArray;
20
use function Cycle\ORM\Promise\resolveIntoVar;
21
use function Cycle\ORM\Promise\throwExceptionOnNull;
22
23 View Code Duplication
final 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...
24
{
25
    /** @var string */
26
    private $class;
27
28
    /** @var string */
29
    private $resolverProperty;
30
31
    /** @var string */
32
    private $resolveMethod;
33
34
    /** @var string */
35
    private $unsetPropertiesConst;
36
37
    /**
38
     * @param string $class
39
     * @param string $resolverProperty
40
     * @param string $resolveMethod
41
     * @param string $unsetPropertiesConst
42
     */
43
    public function __construct(
44
        string $class,
45
        string $resolverProperty,
46
        string $resolveMethod,
47
        string $unsetPropertiesConst
48
    ) {
49
        $this->class = $class;
50
        $this->resolveMethod = $resolveMethod;
51
        $this->resolverProperty = $resolverProperty;
52
        $this->unsetPropertiesConst = $unsetPropertiesConst;
53
    }
54
55
    /**
56
     * @param Node $node
57
     * @return int|Node|Node[]|null
58
     */
59
    public function leaveNode(Node $node)
60
    {
61
        if ($node instanceof Node\Stmt\Class_) {
62
            $method = new Builder\Method('__unset');
63
            $method->makePublic();
64
            $method->addParam(new Builder\Param('name'));
65
            $method->addStmt($this->buildUnsetExpression());
66
67
            $node->stmts[] = $method->getNode();
68
        }
69
70
        return null;
71
    }
72
73
    /**
74
     * @return Node\Stmt\If_
75
     */
76
    private function buildUnsetExpression(): Node\Stmt\If_
77
    {
78
        $if = ifInConstArray('name', 'self', $this->unsetPropertiesConst);
79
        $if->stmts[] = resolveIntoVar('entity', 'this', $this->resolverProperty, $this->resolveMethod);
80
        $if->stmts[] = throwExceptionOnNull(
81
            new Node\Expr\Variable('entity'),
82
            exprUnsetFunc('entity', '{$name}'),
83
            'Property `%s` not loaded in `__unset()` method for `%s`',
84
            [
85
                new Node\Arg(new Node\Expr\Variable('name')),
86
                $this->class
87
            ]
88
        );
89
        $if->else = new Node\Stmt\Else_([
90
            exprUnsetFunc('this', '{$name}')
91
        ]);
92
93
        return $if;
94
    }
95
}
96