Passed
Push — master ( 10a192...087136 )
by Aleksei
07:38 queued 05:42
created

Alter::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license MIT
7
 * @author  Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Migrations\Operation\Index;
13
14
use Cycle\Migrations\CapsuleInterface;
15
use Cycle\Migrations\Exception\Operation\IndexException;
16
use Cycle\Migrations\Operation\Traits\OptionsTrait;
17
18
final class Alter extends Index
19
{
20
    use OptionsTrait;
0 ignored issues
show
Bug introduced by
The trait Cycle\Migrations\Operation\Traits\OptionsTrait requires the property $aliases which is not provided by Cycle\Migrations\Operation\Index\Alter.
Loading history...
21
22
    /**
23
     * @param string $table
24
     * @param array  $columns
25
     * @param array  $options
26
     */
27
    public function __construct(string $table, array $columns, array $options = [])
28
    {
29
        parent::__construct($table, $columns);
30
        $this->options = $options;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function execute(CapsuleInterface $capsule): void
37
    {
38
        $schema = $capsule->getSchema($this->getTable());
39
40
        if (!$schema->hasIndex($this->columns)) {
41
            $columns = implode(',', $this->columns);
42
            throw new IndexException(
43
                "Unable to alter index '{$schema->getName()}'.({$columns}), no such index"
44
            );
45
        }
46
47
        $schema->index($this->columns)->unique(
48
            $this->getOption('unique', false)
0 ignored issues
show
Bug introduced by
It seems like $this->getOption('unique', false) can also be of type null; however, parameter $unique of Cycle\Database\Schema\AbstractIndex::unique() does only seem to accept boolean, 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

48
            /** @scrutinizer ignore-type */ $this->getOption('unique', false)
Loading history...
49
        );
50
    }
51
}
52