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 |
||
14 | class Query extends AbstractAst implements FieldInterface |
||
15 | { |
||
16 | |||
17 | use AstArgumentsTrait; |
||
18 | use AstDirectivesTrait; |
||
19 | |||
20 | /** @var string */ |
||
21 | protected $name; |
||
22 | |||
23 | /** @var string */ |
||
24 | protected $alias; |
||
25 | |||
26 | /** @var Field[]|Query[] */ |
||
27 | protected $fields = []; |
||
28 | |||
29 | /** |
||
30 | * Query constructor. |
||
31 | * |
||
32 | * @param string $name |
||
33 | * @param string $alias |
||
34 | * @param array $arguments |
||
35 | * @param array $fields |
||
36 | * @param array $directives |
||
37 | * @param Location $location |
||
38 | */ |
||
39 | 89 | View Code Duplication | public function __construct($name, $alias = '', array $arguments, array $fields, array $directives, Location $location) |
49 | |||
50 | 64 | public function getName() |
|
54 | |||
55 | /** |
||
56 | * @return Field[]|Query[]|FragmentInterface[] |
||
57 | */ |
||
58 | 58 | public function getFields() |
|
62 | |||
63 | /** |
||
64 | * @return bool |
||
65 | */ |
||
66 | 22 | public function hasFields() |
|
70 | |||
71 | /** |
||
72 | * @param Field[]|Query[] $fields |
||
73 | */ |
||
74 | 89 | public function setFields($fields) |
|
81 | |||
82 | 64 | public function getAlias() |
|
86 | |||
87 | public function hasField($name, $deep = false) |
||
103 | |||
104 | } |
||
105 |
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.