Passed
Pull Request — master (#29)
by Sébastien
08:57
created

AbstractSchemaManager   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Test Coverage

Coverage 95.08%

Importance

Changes 0
Metric Value
wmc 16
eloc 55
dl 0
loc 185
ccs 58
cts 61
cp 0.9508
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A transaction() 0 30 4
A setConnection() 0 6 1
A __construct() 0 3 1
A table() 0 10 1
A getUseDrop() 0 3 1
A getConnection() 0 3 1
A change() 0 14 1
A add() 0 9 2
A isBuffered() 0 3 1
A useDrop() 0 5 1
A simulate() 0 10 2
1
<?php
2
3
namespace Bdf\Prime\Schema;
4
5
use BadMethodCallException;
6
use Bdf\Prime\Connection\ConnectionInterface;
7
use Bdf\Prime\Connection\TransactionManagerInterface;
8
use Bdf\Prime\Exception\DBALException;
9
use Bdf\Prime\Exception\PrimeException;
10
use Bdf\Prime\Platform\PlatformInterface;
11
use Bdf\Prime\Schema\Builder\TableBuilder;
12
use Bdf\Prime\Schema\Builder\TypesHelperTableBuilder;
13
use Doctrine\DBAL\DBALException as DoctrineDBALException;
14
15
/**
16
 * Class AbstractSchemaManager
17
 *
18
 * @template C as ConnectionInterface
19
 * @implements SchemaManagerInterface<C>
20
 */
21
abstract class AbstractSchemaManager implements SchemaManagerInterface
22
{
23
    /**
24
     * The database connection instance.
25
     *
26
     * @var C
27
     */
28
    protected $connection;
29
30
    /**
31
     * The connection platform.
32
     *
33
     * @var PlatformInterface
34
     */
35
    protected $platform;
36
37
    /**
38
     * The use drop flag. Allows builder to use drop command on diff
39
     *
40
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
41
     */
42
    protected $useDrop = true;
43
44
    /**
45
     * The auto flush flag. Allows builder execute query
46
     *
47
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
48
     * @internal
0 ignored issues
show
Coding Style Documentation introduced by
@internal tag is not allowed in member variable comment
Loading history...
49
     */
50
    protected $autoFlush = true;
51
52
53
    /**
54
     * Create a new schema builder.
55
     *
56
     * @param C $connection
57
     * @throws PrimeException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
58
     */
59 180
    public function __construct(ConnectionInterface $connection)
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
60
    {
61 180
        $this->setConnection($connection);
62 180
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function getConnection()
68
    {
69 1
        return $this->connection;
70
    }
71
72
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $connection should have a doc-comment as per coding-style.
Loading history...
73
     * {@inheritdoc}
74
     */
75 180
    public function setConnection(ConnectionInterface $connection)
76
    {
77 180
        $this->connection = $connection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $connection of type Bdf\Prime\Connection\ConnectionInterface is incompatible with the declared type Bdf\Prime\Schema\C of property $connection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
78 180
        $this->platform   = $connection->platform();
79
80 180
        return $this;
81
    }
82
83
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $flag should have a doc-comment as per coding-style.
Loading history...
84
     * {@inheritdoc}
85
     */
86 628
    public function useDrop($flag = true)
87
    {
88 628
        $this->useDrop = $flag;
89
90 628
        return $this;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 1
    public function getUseDrop()
97
    {
98 1
        return $this->useDrop;
99
    }
100
101
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $tableName should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $callback should have a doc-comment as per coding-style.
Loading history...
102
     * {@inheritdoc}
103
     */
104 211
    public function table($tableName, callable $callback)
105
    {
106 211
        $table = new TypesHelperTableBuilder(
107 211
            new TableBuilder($tableName),
108 211
            $this->platform->types()
109
        );
110
        
111 211
        $callback($table);
112
113 211
        return $this->add($table->build());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->add($table->build()) returns the type Bdf\Prime\Schema\Manager\QueryManagerInterface which is incompatible with the return type mandated by Bdf\Prime\Schema\Manager...nagerInterface::table() of Bdf\Prime\Schema\Manager\TableManagerInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
114
    }
115
116
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $table should have a doc-comment as per coding-style.
Loading history...
117
     * {@inheritdoc}
118
     */
119 832
    public function add(TableInterface $table)
120
    {
121 832
        if ($this->hasTable($table->name())) {
122 83
            return $this->push(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->push($this...Table($table->name()))) returns the type Bdf\Prime\Schema\Manager\QueryManagerInterface which is incompatible with the return type mandated by Bdf\Prime\Schema\Manager...ManagerInterface::add() of Bdf\Prime\Schema\Manager\TableManagerInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
123 83
                $this->diff($table, $this->loadTable($table->name()))
124
            );
125
        }
126
127 791
        return $this->push($this->schema($table));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->push($this->schema($table)) returns the type Bdf\Prime\Schema\Manager\QueryManagerInterface which is incompatible with the return type mandated by Bdf\Prime\Schema\Manager...ManagerInterface::add() of Bdf\Prime\Schema\Manager\TableManagerInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
Deprecated Code introduced by
The function Bdf\Prime\Schema\SchemaManagerInterface::schema() has been deprecated: since 1.3 Doctrine schema should be used only internally ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

127
        return $this->push(/** @scrutinizer ignore-deprecated */ $this->schema($table));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
128
    }
129
130
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $tableName should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $callback should have a doc-comment as per coding-style.
Loading history...
131
     * {@inheritdoc}
132
     */
133 2
    public function change($tableName, callable $callback)
134
    {
135 2
        $table = $this->loadTable($tableName);
136 2
        $builder = TableBuilder::fromTable($table);
137
138 2
        $callback(
139 2
            new TypesHelperTableBuilder(
140 2
                $builder,
141 2
                $this->platform->types()
142
            )
143
        );
144
145 2
        return $this->push(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->push($this...lder->build(), $table)) returns the type Bdf\Prime\Schema\Manager\QueryManagerInterface which is incompatible with the return type mandated by Bdf\Prime\Schema\Manager...agerInterface::change() of Bdf\Prime\Schema\Manager\TableManagerInterface.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
146 2
            $this->diff($builder->build(), $table)
147
        );
148
    }
149
150
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $operations should have a doc-comment as per coding-style.
Loading history...
151
     * {@inheritdoc}
152
     */
153 15
    public function simulate(callable $operations = null)
154
    {
155 15
        $newSchema = clone $this;
156 15
        $newSchema->autoFlush = false;
157
158 15
        if ($operations !== null) {
159 3
            $operations($newSchema);
160
        }
161
162 15
        return $newSchema;
163
    }
164
165
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $operations should have a doc-comment as per coding-style.
Loading history...
166
     * {@inheritdoc}
167
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
168 2
    public function transaction(callable $operations)
169
    {
170 2
        $last = $this->autoFlush;
171 2
        $this->autoFlush = false;
172
173 2
        if (!$this->connection instanceof TransactionManagerInterface) {
174
            throw new BadMethodCallException('The connection '.$this->connection->getName().' do not handle transactions');
175
        }
176
177
        try {
178 2
            $operations($this);
179
180 2
            $this->connection->beginTransaction();
181 2
            $this->flush();
182 1
            $this->connection->commit();
183 1
        } catch (DoctrineDBALException $e) {
184
            $this->connection->rollBack();
185
186
            /** @psalm-suppress InvalidScalarArgument */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Block comments must be started with /*
Loading history...
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
187
            throw new DBALException($e->getMessage(), $e->getCode(), $e);
188 1
        } catch (\Exception $e) {
189 1
            $this->connection->rollBack();
190
191 1
            throw $e;
192 1
        } finally {
193 2
            $this->autoFlush = $last;
194 2
            $this->clear();
195
        }
196
197 1
        return $this;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203 4
    public function isBuffered()
204
    {
205 4
        return !$this->autoFlush;
206
    }
207
}
208