Test Failed
Pull Request — master (#332)
by
unknown
17:15
created

Command::refreshMaterializedView()   B

Complexity

Conditions 10
Paths 15

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
eloc 15
nc 15
nop 3
dl 0
loc 32
ccs 0
cts 0
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Pgsql;
6
7
use LogicException;
8
use Yiisoft\Db\Driver\Pdo\AbstractPdoCommand;
9
10
/**
11
 * Implements a database command that can be executed with a PDO (PHP Data Object) database connection for PostgreSQL
12
 * Server.
13
 */
14
final class Command extends AbstractPdoCommand
15 1
{
16
    public function showDatabases(): array
17 1
    {
18
        $sql = <<<SQL
19 1
        SELECT datname FROM pg_database WHERE datistemplate = false AND datname NOT IN ('postgres', 'template0', 'template1')
20
        SQL;
21 1
22
        return $this->setSql($sql)->queryColumn();
23
    }
24
25
    /**
26
     * @see {https://www.postgresql.org/docs/current/sql-refreshmaterializedview.html}
27
     *
28
     * @param string $viewName
29
     * @param bool|null $concurrently Add [ CONCURRENTLY ] to refresh command
30
     * @param bool|null $withData Add [ WITH [ NO ] DATA ] to refresh command
31
     * @return void
32
     * @throws \Throwable
33
     * @throws \Yiisoft\Db\Exception\Exception
34
     * @throws \Yiisoft\Db\Exception\InvalidConfigException
35
     */
36
    public function refreshMaterializedView(string $viewName, ?bool $concurrently = null, ?bool $withData = null): void
37
    {
38
        if ($concurrently || ($concurrently === null || $withData === null)) {
39
40
            $tableSchema = $this->db->getTableSchema($viewName);
41
            $hasUnique = count($this->db->getSchema()->findUniqueIndexes($tableSchema)) > 0;
0 ignored issues
show
Bug introduced by
It seems like $tableSchema can also be of type null; however, parameter $table of Yiisoft\Db\Schema\Schema...ce::findUniqueIndexes() does only seem to accept Yiisoft\Db\Schema\TableSchemaInterface, 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

41
            $hasUnique = count($this->db->getSchema()->findUniqueIndexes(/** @scrutinizer ignore-type */ $tableSchema)) > 0;
Loading history...
42
43
            if ($concurrently && !$hasUnique) {
44
                throw new LogicException('CONCURRENTLY refresh is not allowed without unique index.');
45
            }
46
47
            $concurrently = $hasUnique;
48
        }
49
50
        $sql = 'REFRESH MATERIALIZED VIEW';
51
52
        if ($concurrently) {
53
54
            if ($withData === false) {
55
                throw new LogicException('CONCURRENTLY and WITH NO DATA may not be specified together.');
56
            }
57
58
            $sql .= ' CONCURRENTLY';
59
        }
60
61
        $sql .= ' ' . $this->db->getQuoter()->quoteTableName($viewName);
62
63
        if (is_bool($withData)) {
64
            $sql .= ' WITH ' . ($withData ? 'DATA' : 'NO DATA');
65
        }
66
67
        $this->setSql($sql)->execute();
68
    }
69
}
70