Completed
Pull Request — master (#389)
by Grégoire
01:23
created

MethodNode   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 197
Duplicated Lines 6.09 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
wmc 38
lcom 3
cbo 2
dl 12
loc 197
rs 8.3999
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getVisibility() 0 4 1
A setVisibility() 12 12 2
A isStatic() 0 4 1
A setStatic() 0 4 1
A returnsReference() 0 4 1
A setReturnsReference() 0 4 1
A getName() 0 4 1
A addArgument() 0 4 1
A getArguments() 0 4 1
A hasReturnType() 0 4 1
C setReturnType() 0 39 16
A getReturnType() 0 4 1
A setNullableReturnType() 0 4 1
A hasNullableReturnType() 0 4 1
A setCode() 0 4 1
A getCode() 0 9 2
A useParentCode() 0 8 1
A generateArgument() 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
 * This file is part of the Prophecy.
5
 * (c) Konstantin Kudryashov <[email protected]>
6
 *     Marcello Duarte <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Prophecy\Doubler\Generator\Node;
13
14
use Prophecy\Exception\InvalidArgumentException;
15
16
/**
17
 * Method node.
18
 *
19
 * @author Konstantin Kudryashov <[email protected]>
20
 */
21
class MethodNode
22
{
23
    private $name;
24
    private $code;
25
    private $visibility = 'public';
26
    private $static = false;
27
    private $returnsReference = false;
28
    private $returnType;
29
    private $nullableReturnType = false;
30
31
    /**
32
     * @var ArgumentNode[]
33
     */
34
    private $arguments = array();
35
36
    /**
37
     * @var TypeHintReference
38
     */
39
    private $typeHintReference;
40
41
    /**
42
     * @param string $name
43
     * @param string $code
44
     */
45
    public function __construct($name, $code = null, TypeHintReference $typeHintReference = null)
46
    {
47
        $this->name = $name;
48
        $this->code = $code;
49
        $this->typeHintReference = $typeHintReference ?: new TypeHintReference();
50
    }
51
52
    public function getVisibility()
53
    {
54
        return $this->visibility;
55
    }
56
57
    /**
58
     * @param string $visibility
59
     */
60 View Code Duplication
    public function setVisibility($visibility)
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...
61
    {
62
        $visibility = strtolower($visibility);
63
64
        if (!in_array($visibility, array('public', 'private', 'protected'))) {
65
            throw new InvalidArgumentException(sprintf(
66
                '`%s` method visibility is not supported.', $visibility
67
            ));
68
        }
69
70
        $this->visibility = $visibility;
71
    }
72
73
    public function isStatic()
74
    {
75
        return $this->static;
76
    }
77
78
    public function setStatic($static = true)
79
    {
80
        $this->static = (bool) $static;
81
    }
82
83
    public function returnsReference()
84
    {
85
        return $this->returnsReference;
86
    }
87
88
    public function setReturnsReference()
89
    {
90
        $this->returnsReference = true;
91
    }
92
93
    public function getName()
94
    {
95
        return $this->name;
96
    }
97
98
    public function addArgument(ArgumentNode $argument)
99
    {
100
        $this->arguments[] = $argument;
101
    }
102
103
    /**
104
     * @return ArgumentNode[]
105
     */
106
    public function getArguments()
107
    {
108
        return $this->arguments;
109
    }
110
111
    public function hasReturnType()
112
    {
113
        return null !== $this->returnType;
114
    }
115
116
    /**
117
     * @param string $type
118
     */
119
    public function setReturnType($type = null)
120
    {
121
        switch ($type) {
122
            case '':
123
                $this->returnType = null;
124
                break;
125
126
            case 'double':
127
            case 'real':
128
                $type = 'float';
0 ignored issues
show
Unused Code introduced by
$type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
129
                // intentional fall through
130
131
            case 'boolean':
132
                $type = 'bool';
0 ignored issues
show
Unused Code introduced by
$type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
                // intentional fall through
134
135
            case 'integer':
136
                $type = 'int';
137
                // intentional fall through
138
139
            case 'string':
140
            case 'float':
141
            case 'int':
142
            case 'bool':
143
            case 'array':
144
            case 'object':
145
            case 'callable':
146
            case 'iterable':
147
            case 'void':
148
                if ($this->typeHintReference->isBuiltInTypeHint($type)) {
149
                    $this->returnType = $type;
150
                    break;
151
                }
152
                // intentional fall-through
153
154
            default:
155
                $this->returnType = '\\' . ltrim($type, '\\');
156
        }
157
    }
158
159
    public function getReturnType()
160
    {
161
        return $this->returnType;
162
    }
163
164
    /**
165
     * @param bool $bool
166
     */
167
    public function setNullableReturnType($bool = true)
168
    {
169
        $this->nullableReturnType = (bool) $bool;
170
    }
171
172
    /**
173
     * @return bool
174
     */
175
    public function hasNullableReturnType()
176
    {
177
        return $this->nullableReturnType;
178
    }
179
180
    /**
181
     * @param string $code
182
     */
183
    public function setCode($code)
184
    {
185
        $this->code = $code;
186
    }
187
188
    public function getCode()
189
    {
190
        if ($this->returnsReference)
191
        {
192
            return "throw new \Prophecy\Exception\Doubler\ReturnByReferenceException('Returning by reference not supported', get_class(\$this), '{$this->name}');";
193
        }
194
195
        return (string) $this->code;
196
    }
197
198
    public function useParentCode()
199
    {
200
        $this->code = sprintf(
201
            'return parent::%s(%s);', $this->getName(), implode(', ',
202
                array_map(array($this, 'generateArgument'), $this->arguments)
203
            )
204
        );
205
    }
206
207
    private function generateArgument(ArgumentNode $arg)
208
    {
209
        $argument = '$'.$arg->getName();
210
211
        if ($arg->isVariadic()) {
212
            $argument = '...'.$argument;
213
        }
214
215
        return $argument;
216
    }
217
}
218