Completed
Push — 8.x-1.x ( 503084...707a9d )
by Philipp
01:24
created

GraphQLTemplateTrait::getGraphQLFragment()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 12
rs 9.4285
1
<?php
2
3
namespace Drupal\graphql_twig;
4
5
/**
6
 * Trait that will be attached to all GraphQL enabled Twig templates.
7
 */
8
trait GraphQLTemplateTrait {
9
10
  /**
11
   * Recursively build the GraphQL query.
12
   *
13
   * Builds the templates GraphQL query by iterating through all included or
14
   * embedded templates recursively.
15
   */
16
  public function getGraphQLQuery() {
17
18
    $query = '';
19
    $includes = [];
20
21
    if ($this instanceof \Twig_Template) {
0 ignored issues
show
Bug introduced by
The class Twig_Template 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...
22
      $query = $this->getGraphQLFragment();
23
24
      $includes = array_keys($this->getGraphQLIncludes());
25
26
      // Recursively collect all included fragments.
27
      $includes = array_map(function ($template) {
28
        return $this->loadTemplate($template)->getGraphQLFragment();
0 ignored issues
show
Bug introduced by
It seems like loadTemplate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
29
      }, $includes);
30
31
      // Always add includes from parent templates.
32
      if ($parent = $this->getGraphQLParent()) {
33
        $includes += array_map(function ($template) {
34
          return $this->loadTemplate($template)->getGraphQLQuery();
0 ignored issues
show
Bug introduced by
It seems like loadTemplate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
35
        }, array_keys($parent->getGraphQLIncludes()));
36
      }
37
    }
38
39
40
    return implode("\n", [-1 => $query] + $includes);
41
  }
42
43
  /**
44
   * Get the files parent template.
45
   *
46
   * @return \Twig_Template|null
47
   *   The parent template or null.
48
   */
49
  protected function getGraphQLParent() {
50
    return $this->graphqlParent ? $this->loadTemplate($this->graphqlParent) : NULL;
0 ignored issues
show
Bug introduced by
The property graphqlParent does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
It seems like loadTemplate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
51
  }
52
53
  /**
54
   * Retrieve the files graphql fragment.
55
   *
56
   * @return string
57
   *   The GraphQL fragment.
58
   */
59
  public function getGraphQLFragment() {
60
    $query = '';
61
    // If there is no query for this template, try to get one from the
62
    // parent template.
63
    if ($this->graphqlQuery) {
64
      $query = $this->graphqlQuery;
0 ignored issues
show
Bug introduced by
The property graphqlQuery does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
65
    }
66
    elseif ($parent = $this->getGraphQLParent()) {
67
      $query = $parent->getGraphQLFragment();
68
    }
69
    return $query;
70
  }
71
72
  /**
73
   * Retrieve a list of all direct or indirect included templates.
74
   *
75
   * @return string[]
76
   *   The list of included templates.
77
   */
78
  public function getGraphQLIncludes() {
79
    $includes = array_flip($this->graphqlIncludes);
0 ignored issues
show
Bug introduced by
The property graphqlIncludes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
80
    if ($includes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $includes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
81
      foreach ($includes as $include => $key) {
82
        $includes += $this->loadTemplate($include)->getGraphQLIncludes();
0 ignored issues
show
Bug introduced by
It seems like loadTemplate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
83
      }
84
    }
85
    return $includes;
86
  }
87
}