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 |
||
| 17 | class InputValueType extends AbstractObjectType |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @param AbstractSchema|Field $value |
||
| 21 | * |
||
| 22 | * @return TypeInterface |
||
| 23 | */ |
||
| 24 | 2 | public function resolveType($value) |
|
| 25 | { |
||
| 26 | 2 | return $value->getConfig()->getType(); |
|
|
|
|||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param AbstractSchema|Field $value |
||
| 31 | * |
||
| 32 | * @return string|null |
||
| 33 | */ |
||
| 34 | 3 | public function resolveDefaultValue($value) |
|
| 35 | { |
||
| 36 | 3 | $resolvedValue = $value->getConfig()->getDefaultValue(); |
|
| 37 | |||
| 38 | 3 | return $resolvedValue === null ? $resolvedValue : json_encode($resolvedValue); |
|
| 39 | } |
||
| 40 | |||
| 41 | 5 | View Code Duplication | public function build($config) |
| 42 | { |
||
| 43 | $config |
||
| 44 | 5 | ->addField('name', new NonNullType(TypeMap::TYPE_STRING)) |
|
| 45 | 5 | ->addField('description', TypeMap::TYPE_STRING) |
|
| 46 | 5 | ->addField(new Field([ |
|
| 47 | 5 | 'name' => 'type', |
|
| 48 | 5 | 'type' => new NonNullType(new QueryType()), |
|
| 49 | 5 | 'resolve' => [$this, 'resolveType'] |
|
| 50 | 5 | ])) |
|
| 51 | 5 | ->addField('defaultValue', [ |
|
| 52 | 5 | 'type' => TypeMap::TYPE_STRING, |
|
| 53 | 5 | 'resolve' => [$this, 'resolveDefaultValue'] |
|
| 54 | 5 | ]); |
|
| 55 | 5 | } |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @return string type name |
||
| 59 | */ |
||
| 60 | 7 | public function getName() |
|
| 64 | } |
||
| 65 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: