Completed
Pull Request — master (#1414)
by
unknown
04:38 queued 01:00
created

AbstractMigration   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 321
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 35
lcom 2
cbo 3
dl 0
loc 321
ccs 70
cts 70
cp 1
rs 9.6
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 3
A init() 0 3 1
A setAdapter() 0 6 1
A getAdapter() 0 4 1
A setOutput() 0 6 1
A getOutput() 0 4 1
A getName() 0 4 1
A getEnvironment() 0 4 1
A setVersion() 0 6 1
A getVersion() 0 4 1
A setMigratingUp() 0 6 1
A isMigratingUp() 0 4 1
A execute() 0 4 1
A query() 0 4 1
A getQueryBuilder() 0 4 1
A fetchRow() 0 4 1
A fetchAll() 0 4 1
A createDatabase() 0 4 1
A dropDatabase() 0 4 1
A hasTable() 0 4 1
A table() 0 7 1
A dropTable() 0 5 1
A preFlightCheck() 0 11 4
A postFlightCheck() 0 8 3
1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2015 Rob Morgan
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated * documentation files (the "Software"), to
10
 * deal in the Software without restriction, including without limitation the
11
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
 * sell copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
 * IN THE SOFTWARE.
25
 *
26
 * @package    Phinx
27
 * @subpackage Phinx\Migration
28
 */
29
namespace Phinx\Migration;
30
31
use Phinx\Db\Adapter\AdapterInterface;
32
use Phinx\Db\Table;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Output\OutputInterface;
35
36
/**
37
 * Abstract Migration Class.
38
 *
39
 * It is expected that the migrations you write extend from this class.
40
 *
41
 * This abstract class proxies the various database methods to your specified
42
 * adapter.
43
 *
44
 * @author Rob Morgan <[email protected]>
45
 */
46
abstract class AbstractMigration implements MigrationInterface
47
{
48
    /**
49
     * @var string
50
     */
51
    protected $environment;
52
    /**
53
     * @var float
54
     */
55
    protected $version;
56
57
    /**
58
     * @var \Phinx\Db\Adapter\AdapterInterface
59
     */
60
    protected $adapter;
61
62
    /**
63
     * @var \Symfony\Component\Console\Output\OutputInterface
64
     */
65
    protected $output;
66
67
    /**
68
     * @var \Symfony\Component\Console\Input\InputInterface
69
     */
70
    protected $input;
71
72
    /**
73
     * Whether this migration is being applied or reverted
74
     *
75
     * @var bool
76
     */
77
    protected $isMigratingUp = true;
78
79
    /**
80
     * List of all the table objects created by this migration
81
     *
82 406
     * @var array
83
     */
84 406
    protected $tables = [];
85 406
86 384
    /**
87 384
     * Class Constructor.
88 406
     *
89 384
     * @param string $environment Environment Detected
90 384
     * @param int $version Migration Version
91
     * @param \Symfony\Component\Console\Input\InputInterface|null $input
92 406
     * @param \Symfony\Component\Console\Output\OutputInterface|null $output
93 406
     */
94
    final public function __construct($environment, $version, InputInterface $input = null, OutputInterface $output = null)
95
    {
96
        $this->environment = $environment;
97
        $this->version = $version;
0 ignored issues
show
Documentation Bug introduced by
The property $version was declared of type double, but $version is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
98
99
        if (!is_null($input)) {
100 406
            $this->setInput($input);
101
        }
102 406
103
        if (!is_null($output)) {
104
            $this->setOutput($output);
105
        }
106
107 1
        $this->init();
108
    }
109 1
110
    /**
111
     * Initialize method.
112
     *
113
     * @return void
114 1
     */
115
    protected function init()
116 1
    {
117
    }
118
119
    /**
120
     * {@inheritdoc}
121 21
     */
122
    public function setAdapter(AdapterInterface $adapter)
123 21
    {
124 21
        $this->adapter = $adapter;
125
126
        return $this;
127
    }
128
129
    /**
130 13
     * {@inheritdoc}
131
     */
132 13
    public function getAdapter()
133
    {
134
        return $this->adapter;
135
    }
136
137
    /**
138 384
     * {@inheritdoc}
139
     */
140 384
    public function setInput(InputInterface $input)
141 384
    {
142
        $this->input = $input;
143
144
        return $this;
145
    }
146
147 2
    /**
148
     * {@inheritdoc}
149 2
     */
150
    public function getInput()
151
    {
152
        return $this->input;
153
    }
154
155 385
    /**
156
     * {@inheritdoc}
157 385
     */
158 385
    public function setOutput(OutputInterface $output)
159
    {
160
        $this->output = $output;
161
162
        return $this;
163
    }
164 3
165
    /**
166 3
     * {@inheritdoc}
167
     */
168
    public function getOutput()
169
    {
170
        return $this->output;
171
    }
172 140
173
    /**
174 140
     * {@inheritdoc}
175
     */
176
    public function getName()
177
    {
178
        return get_class($this);
179
    }
180 1
181
    /**
182 1
     * {@inheritdoc}
183 1
     */
184
    public function getEnvironment()
185
    {
186
        return $this->environment;
187
    }
188
189 279
    /**
190
     * {@inheritdoc}
191 279
     */
192
    public function setVersion($version)
193
    {
194
        $this->version = $version;
195
196
        return $this;
197 10
    }
198
199 10
    /**
200 10
     * {@inheritdoc}
201
     */
202
    public function getVersion()
203
    {
204
        return $this->version;
205
    }
206 2
207
    /**
208 2
     * {@inheritdoc}
209
     */
210
    public function setMigratingUp($isMigratingUp)
211
    {
212
        $this->isMigratingUp = $isMigratingUp;
213
214 3
        return $this;
215
    }
216 3
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function isMigratingUp()
221
    {
222 1
        return $this->isMigratingUp;
223
    }
224 1
225
    /**
226
     * {@inheritdoc}
227
     */
228
    public function execute($sql)
229
    {
230 1
        return $this->getAdapter()->execute($sql);
231
    }
232 1
233
    /**
234
     * {@inheritdoc}
235
     */
236
    public function query($sql)
237
    {
238 1
        return $this->getAdapter()->query($sql);
239
    }
240 1
241
    /**
242
     * {@inheritdoc}
243
     */
244
    public function getQueryBuilder()
245
    {
246 3
        return $this->getAdapter()->getQueryBuilder();
247
    }
248
249 3
    /**
250 2
     * {@inheritdoc}
251 2
     */
252 3
    public function fetchRow($sql)
253 3
    {
254
        return $this->getAdapter()->fetchRow($sql);
255
    }
256
257
    /**
258 1
     * {@inheritdoc}
259
     */
260 1
    public function fetchAll($sql)
261 1
    {
262
        return $this->getAdapter()->fetchAll($sql);
263
    }
264
265
    /**
266 1
     * {@inheritdoc}
267
     */
268 1
    public function insert($table, $data)
269 1
    {
270
        trigger_error('insert() is deprecated since 0.10.0. Use $this->table($tableName)->insert($data)->save() instead.', E_USER_DEPRECATED);
271
        // convert to table object
272
        if (is_string($table)) {
273
            $table = new Table($table, [], $this->getAdapter());
274 1
        }
275
        $table->insert($data)->save();
276 1
    }
277
278
    /**
279
     * {@inheritdoc}
280
     */
281
    public function createDatabase($name, $options)
282 5
    {
283
        $this->getAdapter()->createDatabase($name, $options);
284 5
    }
285
286
    /**
287
     * {@inheritdoc}
288
     */
289
    public function dropDatabase($name)
290
    {
291
        $this->getAdapter()->dropDatabase($name);
292
    }
293 1
294
    /**
295 1
     * {@inheritdoc}
296 1
     */
297
    public function hasTable($tableName)
298
    {
299
        return $this->getAdapter()->hasTable($tableName);
300
    }
301
302
    /**
303
     * {@inheritdoc}
304
     */
305
    public function table($tableName, $options = [])
306
    {
307
        $table = new Table($tableName, $options, $this->getAdapter());
308
        $this->tables[] = $table;
309
310
        return $table;
311
    }
312
313
    /**
314
     * A short-hand method to drop the given database table.
315
     *
316
     * @deprecated since 0.10.0. Use $this->table($tableName)->drop()->save() instead.
317
     * @param string $tableName Table Name
318
     * @return void
319
     */
320
    public function dropTable($tableName)
321
    {
322
        trigger_error('dropTable() is deprecated since 0.10.0. Use $this->table($tableName)->drop()->save() instead.', E_USER_DEPRECATED);
323
        $this->table($tableName)->drop()->save();
324
    }
325
326
    /**
327
     * Perform checks on the migration, print a warning
328
     * if there are potential problems.
329
     *
330
     * Right now, the only check is if there is both a `change()` and
331
     * an `up()` or a `down()` method.
332
     *
333
     * @param string|null $direction
334
     *
335
     * @return void
336
     */
337
    public function preFlightCheck($direction = null)
338
    {
339
        if (method_exists($this, MigrationInterface::CHANGE)) {
340
            if (method_exists($this, MigrationInterface::UP) ||
341
                method_exists($this, MigrationInterface::DOWN) ) {
342
                $this->output->writeln(sprintf(
343
                    '<comment>warning</comment> Migration contains both change() and/or up()/down() methods.  <options=bold>Ignoring up() and down()</>.'
344
                ));
345
            }
346
        }
347
    }
348
349
    /**
350
     * Perform checks on the migration after completion
351
     *
352
     * Right now, the only check is whether all changes were committed
353
     *
354
     * @param string|null $direction direction of migration
355
     *
356
     * @return void
357
     */
358
    public function postFlightCheck($direction = null)
359
    {
360
        foreach ($this->tables as $table) {
361
            if ($table->hasPendingActions()) {
362
                throw new \RuntimeException('Migration has pending actions after execution!');
363
            }
364
        }
365
    }
366
}
367