GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

QueryCacheBuilder::truncate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Neurony\QueryCache\Database;
4
5
use Exception;
6
use Illuminate\Database\ConnectionInterface;
7
use Illuminate\Database\Query\Builder as QueryBuilder;
8
use Illuminate\Database\Query\Grammars\Grammar;
9
use Illuminate\Database\Query\Processors\Processor;
10
11
class QueryCacheBuilder extends QueryBuilder
12
{
13
    /**
14
     * The cache tag value.
15
     * The value comes from the Neurony\QueryCache\Traits\IsCacheable.
16
     *
17
     * @var string
18
     */
19
    protected $cacheTag;
20
21
    /**
22
     * The cache type value.
23
     * Can have one of the values present in the QueryCache class -> TYPE_CACHE constants.
24
     * The value comes from the Neurony\QueryCache\IsCacheable.
25
     *
26
     * @var string
27
     */
28
    protected $cacheType;
29
30
    /**
31
     * Create a new query builder instance.
32
     *
33
     * @param ConnectionInterface $connection
34
     * @param Grammar|null $grammar
35
     * @param Processor|null $processor
36
     * @param string|null $cacheTag
37
     * @param int|null $cacheType
38
     */
39
    public function __construct(
40
        ConnectionInterface $connection,
41
        Grammar $grammar = null,
42
        Processor $processor = null,
43
        $cacheTag = null, $cacheType = null
44
    ) {
45
        parent::__construct($connection, $grammar, $processor);
46
47
        $this->cacheType = $cacheType;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cacheType can also be of type integer. However, the property $cacheType is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
48
        $this->cacheTag = $cacheTag;
49
    }
50
51
    /**
52
     * Returns a unique string that can identify this query.
53
     *
54
     * @return string
55
     */
56
    public function getQueryCacheKey(): string
57
    {
58
        return json_encode([
59
            $this->toSql() => $this->getBindings(),
60
        ]);
61
    }
62
63
    /**
64
     * Flush the query cache based on the model's cache tag.
65
     *
66
     * @return void
67
     * @throws Exception
68
     */
69
    public function flushQueryCache(): void
70
    {
71
        cache()->store(
72
            app('cache.query')->getAllQueryCacheStore()
73
        )->tags($this->cacheTag)->flush();
74
    }
75
76
    /**
77
     * Insert a new record into the database.
78
     *
79
     * @param array $values
80
     * @return bool
81
     * @throws Exception
82
     */
83
    public function insert(array $values): bool
84
    {
85
        $this->flushQueryCache();
86
87
        return parent::insert($values);
88
    }
89
90
    /**
91
     * Update a record in the database.
92
     *
93
     * @param array $values
94
     * @return int
95
     * @throws Exception
96
     */
97
    public function update(array $values): int
98
    {
99
        $this->flushQueryCache();
100
101
        return parent::update($values);
102
    }
103
104
    /**
105
     * Delete a record from the database.
106
     *
107
     * @param int|null $id
108
     * @return int|null
109
     * @throws Exception
110
     */
111
    public function delete($id = null): ?int
112
    {
113
        $this->flushQueryCache();
114
115
        return parent::delete($id);
116
    }
117
118
    /**
119
     * Run a truncate statement on the table.
120
     *
121
     * @return void
122
     * @throws Exception
123
     */
124
    public function truncate(): void
125
    {
126
        $this->flushQueryCache();
127
128
        parent::truncate();
129
    }
130
131
    /**
132
     * Run the query as a "select" statement against the connection.
133
     *
134
     * @return array
135
     * @throws Exception
136
     */
137
    protected function runSelect(): array
138
    {
139
        switch ($this->cacheType) {
140
            case app('cache.query')->cacheAllQueriesForeverType():
141
                return $this->runSelectWithAllQueriesCached();
142
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
143
            case app('cache.query')->cacheOnlyDuplicateQueriesOnceType():
144
                return $this->runSelectWithDuplicateQueriesCached();
145
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
146
            default:
147
                return parent::runSelect();
148
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
149
        }
150
    }
151
152
    /**
153
     * Run the query as a "select" statement against the connection.
154
     * Also while fetching the results, cache all queries.
155
     *
156
     * @return mixed
157
     * @throws Exception
158
     */
159
    protected function runSelectWithAllQueriesCached()
160
    {
161
        return cache()->store(
162
            app('cache.query')->getAllQueryCacheStore()
163
        )->tags($this->cacheTag)->rememberForever($this->getQueryCacheKey(), function () {
164
            return parent::runSelect();
165
        });
166
    }
167
168
    /**
169
     * Run the query as a "select" statement against the connection.
170
     * Also while fetching the results, cache only duplicate queries for the current request.
171
     *
172
     * @return mixed
173
     * @throws Exception
174
     */
175
    protected function runSelectWithDuplicateQueriesCached()
176
    {
177
        return cache()->store(
178
            app('cache.query')->getDuplicateQueryCacheStore()
179
        )->tags($this->cacheTag)->remember($this->getQueryCacheKey(), 1, function () {
180
            return parent::runSelect();
181
        });
182
    }
183
}
184