Completed
Pull Request — master (#392)
by Luc
14:50 queued 09:56
created

SchemaConfigurator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\SavedSearches\Doctrine;
4
5
use CultuurNet\UDB3\Doctrine\DBAL\SchemaConfiguratorInterface;
6
use Doctrine\DBAL\Schema\AbstractSchemaManager;
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\DBAL\Schema\Table;
9
use Doctrine\DBAL\Types\Type;
10
use ValueObjects\StringLiteral\StringLiteral;
11
12
class SchemaConfigurator implements SchemaConfiguratorInterface
13
{
14
    const ID = 'id';
15
    const USER = 'user_id';
16
    const NAME = 'name';
17
    const QUERY = 'query';
18
19
    /**
20
     * @var StringLiteral
21
     */
22
    private $tableName;
23
24
    /**
25
     * SchemaConfigurator constructor.
26
     * @param StringLiteral $tableName
27
     */
28
    public function __construct(StringLiteral $tableName)
29
    {
30
        $this->tableName = $tableName;
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36 View Code Duplication
    public function configure(AbstractSchemaManager $schemaManager)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        $schema = $schemaManager->createSchema();
39
40
        if (!$schema->hasTable($this->tableName->toNative())) {
41
            $table = $this->createTable($schema, $this->tableName);
42
43
            $schemaManager->createTable($table);
44
        }
45
    }
46
47
    /**
48
     * @param Schema $schema
49
     * @param StringLiteral $tableName
50
     * @return Table
51
     */
52
    private function createTable(Schema $schema, StringLiteral $tableName)
53
    {
54
        $table = $schema->createTable($tableName->toNative());
55
56
        $table->addColumn(self::ID, Type::GUID)
57
            ->setLength(36)
58
            ->setNotnull(true);
59
60
        $table->addColumn(self::USER, Type::GUID)
61
            ->setLength(36)
62
            ->setNotnull(true);
63
64
        $table->addColumn(self::NAME, Type::STRING)
65
            ->setLength(255)
66
            ->setNotnull(true);
67
68
        $table->addColumn(self::QUERY, Type::TEXT)
69
            ->setNotnull(true);
70
71
        $table->addIndex([self::ID]);
0 ignored issues
show
Documentation introduced by
array(self::ID) is of type array<integer,string,{"0":"string"}>, but the function expects a array<integer,array<integer,*>>.

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...
72
        $table->addIndex([self::USER]);
0 ignored issues
show
Documentation introduced by
array(self::USER) is of type array<integer,string,{"0":"string"}>, but the function expects a array<integer,array<integer,*>>.

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...
73
74
        return $table;
75
    }
76
}
77