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 |
||
11 | class Query |
||
12 | { |
||
13 | |||
14 | /** @var string */ |
||
15 | protected $name; |
||
16 | |||
17 | /** @var string */ |
||
18 | protected $alias; |
||
19 | |||
20 | /** @var Argument[] */ |
||
21 | protected $arguments; |
||
22 | |||
23 | /** @var Field[]|Query[] */ |
||
24 | protected $fields; |
||
25 | |||
26 | 45 | public function __construct($name, $alias = null, $arguments = [], $fields = []) |
|
27 | { |
||
28 | 45 | $this->name = $name; |
|
29 | 45 | $this->alias = $alias; |
|
30 | 45 | $this->arguments = $arguments; |
|
31 | 45 | $this->fields = $fields; |
|
32 | 45 | } |
|
33 | |||
34 | 22 | public function getName() |
|
35 | { |
||
36 | 22 | return $this->name; |
|
37 | } |
||
38 | |||
39 | 1 | public function hasArguments() |
|
40 | { |
||
41 | 1 | return (bool)count($this->arguments); |
|
42 | } |
||
43 | |||
44 | /** |
||
45 | * @return Argument[] |
||
46 | */ |
||
47 | 23 | public function getArguments() |
|
48 | { |
||
49 | 23 | return $this->arguments; |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param $arguments Argument[] |
||
54 | */ |
||
55 | 1 | public function setArguments($arguments) |
|
59 | |||
60 | 2 | public function addArgument(Argument $argument) |
|
61 | { |
||
62 | 2 | $this->arguments[$argument->getName()] = $argument; |
|
63 | 2 | } |
|
64 | |||
65 | 1 | View Code Duplication | public function getKeyValueArguments() |
|
|||
66 | { |
||
67 | 1 | $arguments = []; |
|
68 | |||
69 | 1 | foreach ($this->getArguments() as $argument) { |
|
70 | 1 | $arguments[$argument->getName()] = $argument->getValue()->getValue(); |
|
71 | } |
||
72 | |||
73 | 1 | return $arguments; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return Field[]|Query[]|FragmentInterface[] |
||
78 | */ |
||
79 | 20 | public function getFields() |
|
80 | { |
||
81 | 20 | return $this->fields; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return bool |
||
86 | */ |
||
87 | 18 | public function hasFields() |
|
88 | { |
||
89 | 18 | return (bool)count($this->fields); |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param Field[]|Query[] $fields |
||
94 | */ |
||
95 | 1 | public function setFields($fields) |
|
99 | |||
100 | 22 | public function getAlias() |
|
104 | |||
105 | public function hasField($name, $deep = false) |
||
106 | { |
||
107 | foreach ($this->getFields() as $field) { |
||
108 | if ($field->getName() == $name) { |
||
109 | return true; |
||
110 | } |
||
111 | |||
112 | if ($deep && $field instanceof Query) { |
||
121 | |||
122 | } |
||
123 |
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.