| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Fubhy\GraphQL\Type\Definition\Types; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Fubhy\GraphQL\Type\Definition\FieldDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Object Type Definition. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Almost all of the GraphQL types you define will be object types. Object types | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * have a name, but most importantly describe their fields. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * When two types need to refer to each other, or a type needs to refer to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * itself in a field, you can use a function expression (aka a closure or a | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * thunk) to supply the fields lazily. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class ObjectType extends Type implements OutputTypeInterface, CompositeTypeInterface, NullableTypeInterface, UnmodifiedTypeInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var \Fubhy\GraphQL\Type\Definition\Types\InterfaceType[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $interfaces; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var \Fubhy\GraphQL\Type\Definition\FieldDefinition[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $fieldMap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @var callable|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     protected $isTypeOf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param array $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param \Fubhy\GraphQL\Type\Definition\Types\InterfaceType[] $interfaces | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param callable|null $isTypeOf | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string|null $description | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 369 |  |     public function __construct($name, array $fields = [], array $interfaces = [], callable $isTypeOf = NULL, $description = NULL) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 369 |  |         parent::__construct($name, $description); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 369 |  |         $this->fields = $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 369 |  |         $this->interfaces = $interfaces; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 369 |  |         $this->isTypeOf = $isTypeOf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 369 |  |         foreach ($this->interfaces as $interface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 84 |  |             $interface->addPossibleType($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 369 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 369 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return \Fubhy\GraphQL\Type\Definition\FieldDefinition[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 64 | 306 | View Code Duplication |     public function getFields() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 306 |  |         if (!isset($this->fieldMap)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 303 |  |             $this->fieldMap = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 303 |  |             foreach ($this->fields as $name => $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 291 |  |                 if (!isset($field['name'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 291 |  |                     $field['name'] = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 291 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 291 |  |                 $this->fieldMap[$name] = new FieldDefinition($field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 303 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 303 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             unset($this->fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 306 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $this->fieldMap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param array $fields | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     public function setFields(array $fields) { | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         unset($this->fields); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         $this->fieldMap = $fields; | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param string $field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return \Fubhy\GraphQL\Type\Definition\FieldDefinition | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function getField($field) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $fields = $this->getFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 153 |  |         if (!isset($fields[$field])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             throw new \LogicException(sprintf('Undefined field %s.', $field)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 153 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         return $fields[$field]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @return \Fubhy\GraphQL\Type\Definition\Types\InterfaceType[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 9 |  |     public function getInterfaces() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 9 |  |         return isset($this->interfaces) ? $this->interfaces : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return bool|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function isTypeOf($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return isset($this->isTypeOf) ? call_user_func($this->isTypeOf, $value) : NULL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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.