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 |
||
21 | abstract class AbstractInputObjectType extends AbstractType |
||
22 | { |
||
23 | |||
24 | use AutoNameTrait, FieldsAwareObjectTrait; |
||
25 | |||
26 | protected $isBuilt = false; |
||
27 | |||
28 | 8 | View Code Duplication | public function getConfig() |
|
|||
29 | { |
||
30 | 8 | if (!$this->isBuilt) { |
|
31 | 8 | $this->isBuilt = true; |
|
32 | 8 | $this->build($this->config); |
|
33 | 8 | } |
|
34 | |||
35 | 8 | return $this->config; |
|
36 | } |
||
37 | |||
38 | 1 | public function __construct($config = []) |
|
39 | { |
||
40 | 1 | if (empty($config)) { |
|
41 | $config = [ |
||
42 | 1 | 'name' => $this->getName() |
|
43 | 1 | ]; |
|
44 | 1 | } |
|
45 | 1 | $this->config = new InputObjectTypeConfig($config, $this); |
|
46 | 1 | } |
|
47 | |||
48 | /** |
||
49 | * @param InputObjectTypeConfig $config |
||
50 | */ |
||
51 | abstract public function build($config); |
||
52 | |||
53 | 7 | public function isValidValue($value) |
|
54 | { |
||
55 | 7 | if ($value instanceof InputObject) { |
|
56 | $value = $value->getValue(); |
||
57 | } |
||
58 | |||
59 | 7 | if (empty($value)) { |
|
60 | 1 | return true; |
|
61 | } |
||
62 | |||
63 | 6 | if (!is_array($value)) { |
|
64 | 1 | return false; |
|
65 | } |
||
66 | |||
67 | 6 | $typeConfig = $this->getConfig(); |
|
68 | 6 | $requiredFields = array_filter($typeConfig->getFields(), function (InputFieldInterface $field) { |
|
69 | 6 | return $field->getType()->getKind() == TypeMap::KIND_NON_NULL; |
|
70 | 6 | }); |
|
71 | |||
72 | 6 | foreach ($value as $valueKey => $valueItem) { |
|
73 | 6 | if (!$typeConfig->hasField($valueKey)) { |
|
74 | // Schema validation will generate the error message for us. |
||
75 | return false; |
||
76 | } |
||
77 | |||
78 | 6 | $field = $typeConfig->getField($valueKey); |
|
79 | 6 | if (!$field->getType()->isValidValue($valueItem)) { |
|
80 | 3 | $error = $field->getType()->getValidationError($valueItem) ?: '(no details available)'; |
|
81 | 3 | $this->lastValidationError = sprintf('Not valid type for field "%s" in input type "%s": %s', $field->getName(), $this->getName(), $error); |
|
82 | 3 | return false; |
|
83 | } |
||
84 | |||
85 | 5 | if (array_key_exists($valueKey, $requiredFields)) { |
|
86 | 5 | unset($requiredFields[$valueKey]); |
|
87 | 5 | } |
|
88 | 5 | } |
|
89 | 5 | if (count($requiredFields)) { |
|
90 | 1 | $this->lastValidationError = sprintf('%s %s required on %s', implode(', ', array_keys($requiredFields)), count($requiredFields) > 1 ? 'are' : 'is', $typeConfig->getName()); |
|
91 | 1 | } |
|
92 | |||
93 | 5 | return !(count($requiredFields) > 0); |
|
94 | } |
||
95 | |||
96 | 9 | public function getKind() |
|
100 | |||
101 | 2 | public function isInputType() |
|
105 | |||
106 | 4 | public function parseValue($value) |
|
107 | { |
||
108 | 4 | if (is_null($value)) return null; |
|
127 | |||
128 | } |
||
129 |
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.