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

GraphQLTokenParser::parse()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 28
rs 8.5806
cc 4
eloc 19
nc 4
nop 1
1
<?php
2
3
namespace Drupal\graphql_twig;
4
5
use GraphQL\Error\SyntaxError;
6
use Twig_Error_Syntax;
7
use Twig_Token;
8
9
/**
10
 * Parse the `graphql` twig tag.
11
 *
12
 * Parses the `{% graphql %}` twig tag. Only allowed on template root level.
13
 */
14
class GraphQLTokenParser extends \Twig_TokenParser {
15
16
  /**
17
   * {@inheritdoc}
18
   */
19
  public function parse(Twig_Token $token) {
20
    $stream = $this->parser->getStream();
21
    if (!$this->parser->isMainScope()) {
22
      throw new Twig_Error_Syntax(
23
        'GraphQL queries cannot be defined in blocks.',
24
        $token->getLine(),
25
        $stream->getSourceContext()
26
      );
27
    }
28
29
    $stream->expect(Twig_Token::BLOCK_END_TYPE);
30
    $values = $this->parser->subparse([$this, 'decideBlockEnd'], TRUE);
31
    $stream->expect(Twig_Token::BLOCK_END_TYPE);
32
    if ($values instanceof \Twig_Node_Text) {
0 ignored issues
show
Bug introduced by
The class Twig_Node_Text 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...
33
      try {
34
        return new GraphQLFragmentNode($values->getAttribute('data'));
35
      }
36
      catch (SyntaxError $error) {
0 ignored issues
show
Bug introduced by
The class GraphQL\Error\SyntaxError does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
37
        throw new Twig_Error_Syntax(
38
          $error->getMessage(),
39
          $token->getLine(),
40
          $stream->getSourceContext()
41
        );
42
      }
43
    }
44
45
    return NULL;
46
  }
47
48
  /**
49
   * {@inheritdoc}
50
   */
51
  public function decideBlockEnd(Twig_Token $token) {
52
    return $token->test('endgraphql');
53
  }
54
55
  /**
56
   * {@inheritdoc}
57
   */
58
  public function getTag() {
59
    return 'graphql';
60
  }
61
62
}
63