| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /******************************************************************************* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  *  This file is part of the GraphQL Bundle package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  *  (c) YnloUltratech <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  *  For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  *  file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  ******************************************************************************/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Ynlo\GraphQLBundle\Definition\Loader\Annotation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Ynlo\GraphQLBundle\Annotation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Ynlo\GraphQLBundle\Definition\ArgumentAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Ynlo\GraphQLBundle\Definition\ArgumentDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Ynlo\GraphQLBundle\Definition\QueryDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Ynlo\GraphQLBundle\Util\ClassUtils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Ynlo\GraphQLBundle\Util\TypeUtil; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Parse Query annotation to fetch queries | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class QueryAnnotationParser implements AnnotationParserInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     use AnnotationReaderAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 22 |  |     public function supports($annotation): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 22 |  |         return $annotation instanceof Annotation\Query; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 22 |  |      * @param Annotation\Query $annotation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function parse($annotation, \ReflectionClass $refClass, Endpoint $endpoint) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 22 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $query = new QueryDefinition(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 22 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 22 |  |         if ($annotation->name) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $query->setName($annotation->name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 22 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $query->setName(lcfirst(ClassUtils::getDefaultName($refClass->getName()))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 22 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $endpoint->addQuery($query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 22 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 22 |  |         if ($annotation->type) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 22 |  |             $query->setNode(TypeUtil::normalize($annotation->type)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $query->setType(TypeUtil::normalize($annotation->type)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 22 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 22 |  |         $query->setList(TypeUtil::isTypeList($annotation->type)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 22 |  |         $query->setNonNull(TypeUtil::isTypeNonNull($annotation->type)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $query->setNonNullList(TypeUtil::isTypeNonNullList($annotation->type)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 22 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 22 |  |         if (!$query->getType()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 22 |  |             $nodeType = ClassUtils::getNodeFromClass($refClass->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 22 |  |             $objectDefinition = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 22 |  |             if ($nodeType && $endpoint->hasType($nodeType)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $objectDefinition = $endpoint->getType($nodeType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 22 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 22 |  |             if ($objectDefinition) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 22 |  |                 $query->setType($objectDefinition->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 $query->setNode($objectDefinition->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 //avoid throw Exception when the schema is empty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 //when the schema is empty the NodeInterface has not been loaded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 //then the node(id) and nodes(ids) queries fails | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 if ('Node' === $nodeType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     $endpoint->removeQuery($query->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $error = sprintf('Does not exist any valid type for class "%s"', $refClass->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 throw new \RuntimeException($error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 22 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         if ($annotation->arguments) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             foreach ($annotation->arguments as $argAnnotation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 if ($argAnnotation instanceof Annotation\Argument) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     $this->resolveArgument($query, $argAnnotation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 22 |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 22 |  |             $argAnnotations = $this->reader->getClassAnnotations($refClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 22 |  |             foreach ($argAnnotations as $argAnnotation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 22 |  |                 if ($argAnnotation instanceof Annotation\Argument) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                     $this->resolveArgument($query, $argAnnotation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 22 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 22 |  |         $query->setResolver($annotation->resolver ?? $refClass->getName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 22 |  |         $query->setDeprecationReason($annotation->deprecationReason); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $query->setDescription($annotation->description); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 22 |  |         $query->setRoles((array) $annotation->roles); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 22 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if ($annotation->roles) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 22 |  |             $annotation->options['roles'] = (array) $annotation->roles; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         foreach ($annotation->options as $option => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $query->setMeta($option, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 22 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 22 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 22 |  |      * @param ArgumentAwareInterface $argumentAware | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 22 |  |      * @param object                 $argAnnotation | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 | 22 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 119 | 22 |  |     public function resolveArgument(ArgumentAwareInterface $argumentAware, $argAnnotation) | 
            
                                                                        
                            
            
                                    
            
            
                | 120 | 22 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 121 | 22 |  |         $arg = new ArgumentDefinition(); | 
            
                                                                        
                            
            
                                    
            
            
                | 122 | 22 |  |         $arg->setName($argAnnotation->name); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 | 22 |  |         $arg->setDescription($argAnnotation->description); | 
            
                                                                        
                            
            
                                    
            
            
                | 124 | 22 |  |         $arg->setInternalName($argAnnotation->internalName); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 | 22 |  |         $arg->setDefaultValue($argAnnotation->defaultValue); | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |         $arg->setType(TypeUtil::normalize($argAnnotation->type)); | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |         $arg->setList(TypeUtil::isTypeList($argAnnotation->type)); | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |         $arg->setNonNullList(TypeUtil::isTypeNonNullList($argAnnotation->type)); | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |         $arg->setNonNull(TypeUtil::isTypeNonNull($argAnnotation->type)); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         $argumentAware->addArgument($arg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  | 
            
                        
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.