Completed
Push — 8.x-1.x ( 8c46ac...86e5ca )
by Philipp
03:33
created

GraphQLNode::__construct()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 2
eloc 14
nc 2
nop 3
1
<?php
2
3
namespace Drupal\graphql_twig;
4
5
use GraphQL\Language\AST\DefinitionNode;
6
use GraphQL\Language\AST\OperationDefinitionNode;
7
use GraphQL\Language\AST\VariableDefinitionNode;
8
use GraphQL\Language\Parser;
9
use Twig_Compiler;
10
11
/**
12
 * GraphQL meta information Twig node.
13
 *
14
 * A Twig node that will be attached to templates `class_end` to output the
15
 * collected graphql query and inheritance metadata. Not parsed directly but
16
 * injected by the `GraphQLNodeVisitor`.
17
 */
18
class GraphQLNode extends \Twig_Node {
19
20
  /**
21
   * The modules query string.
22
   *
23
   * @var string
24
   */
25
  protected $query = "";
26
27
  /**
28
   * The modules parent class.
29
   *
30
   * @var string
31
   */
32
  protected $parent = "";
33
34
  /**
35
   * The modules includes.
36
   *
37
   * @var array
38
   */
39
  protected $includes = [];
40
41
  /**
42
   * Boolean indicator if this fragment includes operations.
43
   *
44
   * @var bool
45
   */
46
  public $hasOperations = FALSE;
47
48
  /**
49
   * The list of arguments accepted by operations in this fragment.
50
   *
51
   * @var array
52
   */
53
  public $arguments = [];
54
55
  /**
56
   * GraphQLNode constructor.
57
   *
58
   * @param string $query
59
   *   The query string.
60
   * @param string $parent
61
   *   The parent template identifier.
62
   * @param array $includes
63
   *   Identifiers for any included/referenced templates.
64
   */
65
  public function __construct($query, $parent, $includes) {
66
    $this->query = $query;
67
    $this->parent = $parent;
68
    $this->includes = $includes;
69
70
    if ($this->query) {
71
      $document = Parser::parse($this->query);
72
73
      /** @var \GraphQL\Language\AST\OperationDefinitionNode[] $operations */
74
      $operations = array_filter(iterator_to_array($document->definitions->getIterator()), function (DefinitionNode $node) {
75
        return $node instanceof OperationDefinitionNode;
0 ignored issues
show
Bug introduced by
The class GraphQL\Language\AST\OperationDefinitionNode does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
76
      });
77
78
      $this->hasOperations = (bool) $operations;
79
80
      $this->arguments = array_map(function (VariableDefinitionNode $node) {
81
        return $node->variable->name->value;
82
      }, array_reduce($operations, function ($carry, OperationDefinitionNode $node) {
83
        return array_merge($carry, iterator_to_array($node->variableDefinitions->getIterator()));
84
      }, []));
85
    }
86
87
    parent::__construct();
88
  }
89
90
  /**
91
   * {@inheritdoc}
92
   */
93
  public function compile(Twig_Compiler $compiler) {
94
    $compiler
95
      // Make the template implement the GraphQLTemplateTrait.
96
      ->write("\nuse \Drupal\graphql_twig\GraphQLTemplateTrait;\n")
97
      // Write metadata properties.
98
      ->write("\npublic static function hasGraphQLOperations() { return ")->repr($this->hasOperations)->write("; }\n")
99
      ->write("\npublic static function rawGraphQLQuery() { return ")->string($this->query)->write("; }\n")
100
      ->write("\npublic static function rawGraphQLParent() { return ")->string($this->parent)->write("; }\n");
101
102
    $compiler->write("\npublic static function rawGraphQLIncludes() { return [");
103
104
    foreach ($this->includes as $include) {
105
      $compiler->string($include)->write(",");
106
    }
107
108
    $compiler->write("]; }\n");
109
110
    $compiler->write("\npublic static function rawGraphQLArguments() { return [");
111
    foreach ($this->arguments as $argument) {
112
      $compiler->string($argument)->write(",");
113
    }
114
    $compiler->write("]; }\n");
115
  }
116
117
}
118