AddUseStmts   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 87
Duplicated Lines 13.79 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 12
loc 87
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A leaveNode() 12 12 2
A removeDuplicates() 0 18 5
A getExistingUseParts() 0 15 4
A buildUseStmts() 0 10 2

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\Use_;
15
use PhpParser\Node;
16
use PhpParser\NodeVisitorAbstract;
17
18
use function Cycle\ORM\Promise\inject;
19
20
/**
21
 * Add use statement to the code.
22
 */
23
final class AddUseStmts extends NodeVisitorAbstract
24
{
25
    /** @var array */
26
    private $useStmts;
27
28
    /**
29
     * @param array $useStmts
30
     */
31
    public function __construct(array $useStmts)
32
    {
33
        $this->useStmts = $useStmts;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 View Code Duplication
    public function leaveNode(Node $node)
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
        if ($node instanceof Node\Stmt\Namespace_) {
42
            $node->stmts = inject(
43
                $node->stmts,
44
                Node\Stmt\Class_::class,
45
                $this->removeDuplicates($node->stmts, $this->buildUseStmts())
46
            );
47
        }
48
49
        return null;
50
    }
51
52
    /**
53
     * @param Node\Stmt[] $stmts
54
     * @param Node[]      $nodes
55
     * @return Node\Stmt\Use_[]
56
     */
57
    private function removeDuplicates(array $stmts, array $nodes): array
58
    {
59
        $uses = $this->getExistingUseParts($stmts);
60
61
        foreach ($nodes as $i => $node) {
62
            if (!$node instanceof Node\Stmt\Use_) {
63
                continue;
64
            }
65
66
            foreach ($node->uses as $use) {
67
                if (in_array($use->name->parts, $uses, true)) {
68
                    unset($nodes[$i]);
69
                }
70
            }
71
        }
72
73
        return $nodes;
74
    }
75
76
    /**
77
     * @param Node\Stmt[] $stmts
78
     * @return string[]
79
     */
80
    private function getExistingUseParts(array $stmts): array
81
    {
82
        $parts = [];
83
        foreach ($stmts as $stmt) {
84
            if (!$stmt instanceof Node\Stmt\Use_) {
85
                continue;
86
            }
87
88
            foreach ($stmt->uses as $use) {
89
                $parts[] = $use->name->parts;
90
            }
91
        }
92
93
        return $parts;
94
    }
95
96
    /**
97
     * @return Node[]
98
     */
99
    private function buildUseStmts(): array
100
    {
101
        $stmts = [];
102
        foreach ($this->useStmts as $name) {
103
            $stmt = new Use_(new Node\Name($name), Node\Stmt\Use_::TYPE_NORMAL);
104
            $stmts[] = $stmt->getNode();
105
        }
106
107
        return $stmts;
108
    }
109
}
110