Passed
Push — master ( ecea98...79bda6 )
by Jonas
12:45 queued 01:47
created

ManagesViews::stringifyBindings()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 5
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Staudenmeir\LaravelMigrationViews\Schema\Builders;
4
5
use Illuminate\Support\Str;
6
7
trait ManagesViews
8
{
9
    /**
10
     * Create a new view on the schema.
11
     *
12
     * @param string $name
13
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query
14
     * @param array|null $columns
15
     * @param bool $orReplace
16
     * @return void
17
     */
18 40
    public function createView($name, $query, array $columns = null, $orReplace = false)
19
    {
20 40
        $query = $this->getQueryString($query);
21
22 40
        $this->connection->statement(
23 40
            $this->grammar->compileCreateView($name, $query, $columns, $orReplace)
24
        );
25
    }
26
27
    /**
28
     * Create a new view on the schema or replace an existing one.
29
     *
30
     * @param string $name
31
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query
32
     * @param array|null $columns
33
     * @return void
34
     */
35 4
    public function createOrReplaceView($name, $query, array $columns = null)
36
    {
37 4
        $this->createView($name, $query, $columns, true);
38
    }
39
40
    /**
41
     * Convert the query and its bindings to an SQL string.
42
     *
43
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|string $query
44
     * @return string
45
     */
46 40
    protected function getQueryString($query)
47
    {
48 40
        if (is_string($query)) {
49 4
            return $query;
50
        }
51
52 36
        $bindings = $this->stringifyBindings(
53 36
            $query->getBindings()
0 ignored issues
show
Bug introduced by
It seems like $query->getBindings() can also be of type Illuminate\Database\Eloquent\Builder and Illuminate\Database\Query\Builder; however, parameter $bindings of Staudenmeir\LaravelMigra...ws::stringifyBindings() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
            /** @scrutinizer ignore-type */ $query->getBindings()
Loading history...
54
        );
55
56 36
        return Str::replaceArray('?', $bindings, $query->toSql());
0 ignored issues
show
Bug introduced by
It seems like $query->toSql() can also be of type Illuminate\Database\Eloquent\Builder and Illuminate\Database\Query\Builder; however, parameter $subject of Illuminate\Support\Str::replaceArray() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        return Str::replaceArray('?', $bindings, /** @scrutinizer ignore-type */ $query->toSql());
Loading history...
57
    }
58
59
    /**
60
     * Stringify the query bindings.
61
     *
62
     * @param array $bindings
63
     * @return array
64
     */
65 36
    protected function stringifyBindings(array $bindings)
66
    {
67 36
        $bindings = $this->connection->prepareBindings($bindings);
68
69 36
        foreach ($bindings as &$binding) {
70 36
            if (is_object($binding)) {
71 4
                $binding = (string) $binding;
72
            }
73
74 36
            if (is_string($binding)) {
75 15
                $binding = $this->connection->getPdo()->quote($binding);
76
            }
77
        }
78
79 36
        return $bindings;
80
    }
81
82
    /**
83
     * Rename a view on the schema.
84
     *
85
     * @param string $from
86
     * @param string $to
87
     * @return void
88
     */
89 3
    public function renameView($from, $to)
90
    {
91 3
        $this->rename($from, $to);
0 ignored issues
show
Bug introduced by
The method rename() does not exist on Staudenmeir\LaravelMigra...a\Builders\ManagesViews. Did you maybe mean renameView()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
        $this->/** @scrutinizer ignore-call */ 
92
               rename($from, $to);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
92
    }
93
94
    /**
95
     * Drop a view from the schema.
96
     *
97
     * @param string $name
98
     * @param bool $ifExists
99
     * @return void
100
     */
101 33
    public function dropView($name, $ifExists = false)
102
    {
103 33
        $this->connection->statement(
104 33
            $this->grammar->compileDropView($name, $ifExists)
105
        );
106
    }
107
108
    /**
109
     * Drop a view from the schema if it exists.
110
     *
111
     * @param string $name
112
     * @return void
113
     */
114 44
    public function dropViewIfExists($name)
115
    {
116 44
        $this->dropView($name, true);
117
    }
118
119
    /**
120
     * Determine if the given view exists on the schema.
121
     *
122
     * @param string $name
123
     * @return bool
124
     */
125 12
    public function hasView($name)
126
    {
127 12
        $view = $this->connection->getTablePrefix().$name;
128
129 12
        $view = $this->connection->selectOne(
130 12
            $this->grammar->compileViewExists(),
131 12
            $this->getBindingsForHasView($view)
132
        );
133
134 12
        return !is_null($view);
135
    }
136
137
    /**
138
     * Get the bindings for a "Has View" statement.
139
     *
140
     * @param string $view
141
     * @return array
142
     */
143 3
    protected function getBindingsForHasView($view)
144
    {
145 3
        return [$this->connection->getDatabaseName(), $view];
146
    }
147
148
    /**
149
     * Get the column listing for a given view.
150
     *
151
     * @param string $name
152
     * @return array
153
     */
154 3
    public function getViewColumnListing($name)
155
    {
156 3
        return $this->getColumnListing($name);
0 ignored issues
show
Bug introduced by
It seems like getColumnListing() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

156
        return $this->/** @scrutinizer ignore-call */ getColumnListing($name);
Loading history...
157
    }
158
}
159