Test Setup Failed
Pull Request — master (#10)
by
unknown
01:32
created

Grammar   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 122
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A compileHasAttribute() 0 21 3
A compileRenameAttribute() 0 28 1
A compileDropAttribute() 0 22 3
A wrapBindVar() 0 9 2
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Schema;
4
5
use Illuminate\Database\Schema\Grammars\Grammar as IlluminateGrammar;
6
use Illuminate\Support\Fluent;
7
use LaravelFreelancerNL\FluentAQL\Facades\AQB;
8
9
class Grammar extends IlluminateGrammar
10
{
11
    /**
12
     * ArangoDB handles transactions itself. However most schema actions
13
     * are not done through queries but rather through commands.
14
     * So we just run schema actions sequentially.
15
     *
16
     * @var bool
17
     */
18
    protected $transactions = false;
19
20
    /**
21
     * Compile AQL to check if an attribute is in use within a document in the collection.
22
     * If multiple attributes are set then all must be set in one document.
23
     * @param string $collection
24
     * @param Fluent $command
25
     * @return Fluent
26
     */
27
    public function compileHasAttribute($collection, Fluent $command)
28
    {
29
        if (is_string($command->attribute)) {
30
            $command->attribute = [$command->attribute];
31
        }
32
33
        $filter = [];
34
        foreach ($command->attribute as $attribute) {
35
            $filter[] = ['doc.'.$attribute, '!=', 'null'];
36
        }
37
38
        $aqb = AQB::for('doc', $collection)
0 ignored issues
show
Bug introduced by
It seems like get() 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...
39
            ->filter($filter)
0 ignored issues
show
Documentation introduced by
$filter is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
            ->limit(1)
41
            ->return('true')
42
            ->get();
43
44
        $command->aqb = $aqb;
45
46
        return $command;
47
    }
48
49
    /**
50
     * Compile AQL to rename an attribute, if the new name isn't already in use.
51
     *
52
     * @param string $collection
53
     * @param Fluent $command
54
     * @return Fluent
55
     */
56
    public function compileRenameAttribute($collection, Fluent $command)
57
    {
58
        $bindings['@collection'] = $collection;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$bindings was never initialized. Although not strictly required by PHP, it is generally a good practice to add $bindings = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
59
60
        $bindings['from'] = $this->wrapBindVar($command->to);
61
        $bindings['to'] = $this->wrapBindVar($command->from);
62
63
        $filter = [
64
            ['doc.'.$command->from, '!=', 'null'],
65
            ['doc.'.$command->to],
66
        ];
67
68
        $aqb = AQB::for('doc', $collection)
0 ignored issues
show
Bug introduced by
It seems like update() 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...
69
            ->filter($filter)
0 ignored issues
show
Documentation introduced by
$filter is of type array<integer,array<inte...,{\"0\":\"string\"}>"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
            ->update(
71
                'doc',
72
                [
73
                    $command->from => 'null',
74
                    $command->to => 'doc.'.$command->from,
75
                ],
76
                $collection)
77
            ->options(['keepNull' => true])
78
            ->get();
79
80
        $command->aqb = $aqb;
81
82
        return $command;
83
    }
84
85
    /**
86
     * Compile AQL to drop one or more attributes.
87
     *
88
     * @param string $collection
89
     * @param Fluent $command
90
     * @return Fluent
91
     */
92
    public function compileDropAttribute($collection, Fluent $command)
93
    {
94
        $filter = [];
95
        if (is_string($command->attributes)) {
96
            $command->attributes = [$command->attributes];
97
        }
98
99
        $data = [];
100
        foreach ($command->attributes as $attribute) {
101
            $filter[] = ['doc.'.$attribute, '!=', 'null', 'OR'];
102
            $data[$attribute] = 'null';
103
        }
104
        $aqb = AQB::for('doc', $collection)
0 ignored issues
show
Bug introduced by
It seems like update() 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...
105
            ->filter($filter)
0 ignored issues
show
Documentation introduced by
$filter is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
106
            ->update('doc', $data, $collection)
107
            ->options(['keepNull' => false])
108
            ->get();
109
110
        $command->aqb = $aqb;
111
112
        return $command;
113
    }
114
115
    /**
116
     * Prepare a bindVar for inclusion in an AQL query.
117
     *
118
     * @param $attribute
119
     * @return string
120
     */
121
    public function wrapBindVar($attribute)
122
    {
123
        $attribute = trim($attribute);
124
        if (strpos($attribute, '@') === 0) {
125
            $attribute = "`$attribute`";
126
        }
127
128
        return $attribute;
129
    }
130
}
131