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

Add   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 13
c 0
b 0
f 0
dl 0
loc 35
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 17 3
A __construct() 0 4 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 Add 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\Add.
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 create index '{$schema->getName()}'.({$columns}), index already exists"
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
        if ($this->hasOption('name')) {
52
            $schema->index($this->columns)->setName($this->getOption('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->getOption('name') can also be of type false and null; however, parameter $name of Cycle\Database\Schema\AbstractIndex::setName() 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

52
            $schema->index($this->columns)->setName(/** @scrutinizer ignore-type */ $this->getOption('name'));
Loading history...
53
        }
54
    }
55
}
56