MigrationTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addEnum() 0 18 2
A dropEnum() 0 4 1
1
<?php
2
3
/**
4
 * @author Roman Varkuta <[email protected]>
5
 * @license MIT
6
 * @see https://github.com/myclabs/php-enum PHP enum implementation
7
 * @version 1.0
8
 */
9
10
declare(strict_types=1);
11
12
namespace Kartavik\Yii2\Database\Pgsql;
13
14
use MyCLabs\Enum\Enum;
15
use yii\db;
16
17
/**
18
 * Trait MigrationTrait
19
 * @package Kartavik\Yii2\Database\Pgsql
20
 */
21
trait MigrationTrait
22
{
23
    use SchemaBuilderTrait;
24
25
    /**
26
     * @param string $name
27
     * @param array|Enum|string $enums
28
     *
29
     * @throws db\Exception
30
     */
31
    public function addEnum(string $name, $enums): void
32
    {
33
        $transaction = $this->getDb()->beginTransaction();
34
        $command = $transaction->db->createCommand(
35
            "CREATE TYPE $name AS {$this->formatEnumValues($this->fetchEnums($enums))}"
0 ignored issues
show
Documentation introduced by
$this->fetchEnums($enums) is of type array, but the function expects a object<Kartavik\Yii2\Database\iterable>.

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...
36
        );
37
38
        try {
39
            echo "    > create type $name ... ";
40
            $command->execute();
41
            echo 'done';
42
        } catch (\Exception $exception) {
43
            echo "already exist, skipping";
44
            $transaction->rollBack();
45
        }
46
47
        echo PHP_EOL;
48
    }
49
50
    /**
51
     * @param string $name
52
     *
53
     * @throws db\Exception
54
     */
55
    public function dropEnum(string $name): void
56
    {
57
        $this->getDb()->createCommand("DROP TYPE IF EXISTS $name")->execute();
58
    }
59
}
60