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 |
||
19 | class SchemaConfig extends AbstractConfig |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * @var SchemaTypesList |
||
24 | */ |
||
25 | private $typesList; |
||
26 | /** |
||
27 | * @var SchemaDirectivesList; |
||
28 | */ |
||
29 | private $directiveList; |
||
30 | |||
31 | 77 | public function __construct(array $configData, $contextObject = null, $finalClass = false) |
|
37 | |||
38 | |||
39 | View Code Duplication | public function getRules() |
|
|
|||
40 | { |
||
41 | return [ |
||
42 | 'query' => ['type' => TypeService::TYPE_OBJECT_TYPE, 'required' => true], |
||
43 | 'mutation' => ['type' => TypeService::TYPE_OBJECT_TYPE], |
||
44 | 'types' => ['type' => TypeService::TYPE_ARRAY], |
||
45 | 'directives' => ['type' => TypeService::TYPE_ARRAY], |
||
46 | 'name' => ['type' => TypeService::TYPE_STRING], |
||
47 | ]; |
||
48 | } |
||
49 | |||
50 | 77 | protected function build() |
|
51 | { |
||
52 | 77 | parent::build(); |
|
53 | 77 | if (!empty($this->data['types'])) { |
|
54 | $this->typesList->addTypes($this->data['types']); |
||
55 | } |
||
56 | 77 | if (!empty($this->data['directives'])) { |
|
57 | $this->directiveList->addDirectives($this->data['directives']); |
||
58 | } |
||
59 | 77 | } |
|
60 | |||
61 | |||
62 | /** |
||
63 | * @return AbstractObjectType |
||
64 | */ |
||
65 | 77 | public function getQuery() |
|
69 | |||
70 | /** |
||
71 | * @param $query AbstractObjectType |
||
72 | * |
||
73 | * @return SchemaConfig |
||
74 | */ |
||
75 | 14 | public function setQuery($query) |
|
81 | |||
82 | /** |
||
83 | * @return ObjectType |
||
84 | */ |
||
85 | 22 | public function getMutation() |
|
89 | |||
90 | /** |
||
91 | * @param $query AbstractObjectType |
||
92 | * |
||
93 | * @return SchemaConfig |
||
94 | */ |
||
95 | 5 | public function setMutation($query) |
|
101 | |||
102 | public function getName() |
||
106 | |||
107 | 6 | public function getTypesList() |
|
111 | |||
112 | 2 | public function getDirectiveList() |
|
116 | |||
117 | } |
||
118 |
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.