Completed
Pull Request — master (#3)
by Valentin
01:55
created

AddMagicGetMethod   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 19.44 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A leaveNode() 0 13 2
A buildGetExpression() 7 7 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
class AddMagicGetMethod extends NodeVisitorAbstract
12
{
13
    /** @var string */
14
    private $resolverProperty;
15
16
    /** @var string */
17
    private $resolveMethod;
18
19
    public function __construct(string $resolverProperty, string $resolveMethod)
20
    {
21
        $this->resolverProperty = $resolverProperty;
22
        $this->resolveMethod = $resolveMethod;
23
    }
24
25
    public function leaveNode(Node $node)
26
    {
27
        if ($node instanceof Node\Stmt\Class_) {
28
            $method = new Builder\Method('__get');
29
            $method->makePublic();
30
            $method->addParam(new Builder\Param('name'));
31
            $method->addStmt($this->buildGetExpression());
32
33
            $node->stmts[] = $method->getNode();
34
        }
35
36
        return null;
37
    }
38
39 View Code Duplication
    private function buildGetExpression(): Node\Stmt\If_
0 ignored issues
show
Duplication introduced by
This method 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...
40
    {
41
        $resolved = Expressions::resolveMethodCall('this', $this->resolverProperty, $this->resolveMethod);
42
        $stmt = new Node\Stmt\Return_(new Node\Expr\PropertyFetch($resolved, '{$name}'));
43
44
        return Expressions::throwExceptionOnNull($resolved, $stmt);
45
    }
46
}