Method   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 117
Duplicated Lines 6.84 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 2
dl 8
loc 117
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A makeProtected() 0 5 1
A makePrivate() 0 5 1
A makeStatic() 0 5 1
A makeFinal() 0 5 1
A __construct() 0 3 1
A makePublic() 0 5 1
A makeAbstract() 0 10 2
A addStmt() 0 9 2
A getNode() 8 8 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
namespace PhpParser\Builder;
4
5
use PhpParser;
6
use PhpParser\Node;
7
use PhpParser\Node\Stmt;
8
9
class Method extends FunctionLike
10
{
11
    protected $name;
12
    protected $type = 0;
13
    protected $stmts = array();
14
15
    /**
16
     * Creates a method builder.
17
     *
18
     * @param string $name Name of the method
19
     */
20
    public function __construct($name) {
21
        $this->name = $name;
22
    }
23
24
    /**
25
     * Makes the method public.
26
     *
27
     * @return $this The builder instance (for fluid interface)
28
     */
29
    public function makePublic() {
30
        $this->setModifier(Stmt\Class_::MODIFIER_PUBLIC);
31
32
        return $this;
33
    }
34
35
    /**
36
     * Makes the method protected.
37
     *
38
     * @return $this The builder instance (for fluid interface)
39
     */
40
    public function makeProtected() {
41
        $this->setModifier(Stmt\Class_::MODIFIER_PROTECTED);
42
43
        return $this;
44
    }
45
46
    /**
47
     * Makes the method private.
48
     *
49
     * @return $this The builder instance (for fluid interface)
50
     */
51
    public function makePrivate() {
52
        $this->setModifier(Stmt\Class_::MODIFIER_PRIVATE);
53
54
        return $this;
55
    }
56
57
    /**
58
     * Makes the method static.
59
     *
60
     * @return $this The builder instance (for fluid interface)
61
     */
62
    public function makeStatic() {
63
        $this->setModifier(Stmt\Class_::MODIFIER_STATIC);
64
65
        return $this;
66
    }
67
68
    /**
69
     * Makes the method abstract.
70
     *
71
     * @return $this The builder instance (for fluid interface)
72
     */
73
    public function makeAbstract() {
74
        if (!empty($this->stmts)) {
75
            throw new \LogicException('Cannot make method with statements abstract');
76
        }
77
78
        $this->setModifier(Stmt\Class_::MODIFIER_ABSTRACT);
79
        $this->stmts = null; // abstract methods don't have statements
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $stmts.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
81
        return $this;
82
    }
83
84
    /**
85
     * Makes the method final.
86
     *
87
     * @return $this The builder instance (for fluid interface)
88
     */
89
    public function makeFinal() {
90
        $this->setModifier(Stmt\Class_::MODIFIER_FINAL);
91
92
        return $this;
93
    }
94
95
    /**
96
     * Adds a statement.
97
     *
98
     * @param Node|PhpParser\Builder $stmt The statement to add
99
     *
100
     * @return $this The builder instance (for fluid interface)
101
     */
102
    public function addStmt($stmt) {
103
        if (null === $this->stmts) {
104
            throw new \LogicException('Cannot add statements to an abstract method');
105
        }
106
107
        $this->stmts[] = $this->normalizeNode($stmt);
108
109
        return $this;
110
    }
111
112
    /**
113
     * Returns the built method node.
114
     *
115
     * @return Stmt\ClassMethod The built method node
116
     */
117 View Code Duplication
    public function getNode() {
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...
118
        return new Stmt\ClassMethod($this->name, array(
119
            'type'   => $this->type,
120
            'byRef'  => $this->returnByRef,
121
            'params' => $this->params,
122
            'stmts'  => $this->stmts,
123
        ), $this->attributes);
124
    }
125
}
126