Completed
Pull Request — master (#1320)
by
unknown
01:46
created

AbstractAdapter::setDataDomain()   C

Complexity

Conditions 9
Paths 16

Size

Total Lines 59
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 10.8889

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 5
cts 7
cp 0.7143
rs 6.9133
c 0
b 0
f 0
cc 9
eloc 31
nc 16
nop 1
crap 10.8889

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2017 Cake Software Foundation
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\Db\Adapter
28
 */
29
namespace Phinx\Db\Adapter;
30
31
use Phinx\Db\Table;
32
use Phinx\Db\Table\Column;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Output\NullOutput;
35
use Symfony\Component\Console\Output\OutputInterface;
36
37
/**
38
 * Base Abstract Database Adapter.
39
 */
40
abstract class AbstractAdapter implements AdapterInterface
41
{
42
    /**
43
     * @var array
44
     */
45
    protected $options = [];
46
47
    /**
48
     * @var \Symfony\Component\Console\Input\InputInterface
49
     */
50
    protected $input;
51
52
    /**
53
     * @var \Symfony\Component\Console\Output\OutputInterface
54
     */
55
    protected $output;
56
57
    /**
58
     * @var string
59
     */
60
    protected $schemaTableName = 'phinxlog';
61
62
    /**
63
     * @var array
64
     */
65
    protected $dataDomain = [];
66
67
    /**
68
     * Class Constructor.
69 341
     *
70
     * @param array $options Options
71 341
     * @param \Symfony\Component\Console\Input\InputInterface $input Input Interface
72 341
     * @param \Symfony\Component\Console\Output\OutputInterface  $output Output Interface
73 264
     */
74 264
    public function __construct(array $options, InputInterface $input = null, OutputInterface $output = null)
75 341
    {
76 264
        $this->setOptions($options);
77 264
        if ($input !== null) {
78 341
            $this->setInput($input);
79
        }
80
        if ($output !== null) {
81
            $this->setOutput($output);
82
        }
83 287
    }
84
85 287
    /**
86
     * {@inheritdoc}
87 287
     */
88 6
    public function setOptions(array $options)
89 6
    {
90
        $this->options = $options;
91 287
92
        if (isset($options['default_migration_table'])) {
93
            $this->setSchemaTableName($options['default_migration_table']);
94
        }
95
96
        if (isset($options['data_domain'])) {
97 196
            $this->setDataDomain($options['data_domain']);
98
        }
99 196
100
        return $this;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105 8
     */
106
    public function getOptions()
107 8
    {
108
        return $this->options;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function hasOption($name)
115
    {
116
        return isset($this->options[$name]);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getOption($name)
123
    {
124 269
        if (!$this->hasOption($name)) {
125
            return null;
126 269
        }
127 269
128
        return $this->options[$name];
129
    }
130
131
    /**
132
     * {@inheritdoc}
133 218
     */
134
    public function setInput(InputInterface $input)
135 218
    {
136
        $this->input = $input;
137
138
        return $this;
139
    }
140
141 269
    /**
142
     * {@inheritdoc}
143 269
     */
144 269
    public function getInput()
145
    {
146
        return $this->input;
147
    }
148
149
    /**
150 8
     * {@inheritdoc}
151
     */
152 8
    public function setOutput(OutputInterface $output)
153
    {
154
        $this->output = $output;
155
156 8
        return $this;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function getOutput()
163
    {
164
        if ($this->output === null) {
165
            $output = new NullOutput();
166
            $this->setOutput($output);
167
        }
168
169
        return $this->output;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     *
175
     * @return array
176 195
     */
177
    public function getVersions()
178 195
    {
179
        $rows = $this->getVersionLog();
180
181
        return array_keys($rows);
182
    }
183
184
    /**
185
     * Gets the schema table name.
186
     *
187 7
     * @return string
188
     */
189 7
    public function getSchemaTableName()
190 7
    {
191
        return $this->schemaTableName;
192
    }
193
194
    /**
195
     * Sets the schema table name.
196 193
     *
197
     * @param string $schemaTableName Schema Table Name
198 193
     * @return $this
199
     */
200
    public function setSchemaTableName($schemaTableName)
201
    {
202
        $this->schemaTableName = $schemaTableName;
203
204 191
        return $this;
205
    }
206
207
    /**
208 191
     * Gets the data domain.
209
     *
210 191
     * @return array
211
     */
212 191
    public function getDataDomain()
213 191
    {
214 191
        return $this->dataDomain;
215 191
    }
216 191
217 191
    /**
218 191
     * Sets the data domain.
219 191
     *
220
     * @param array $schemaTableName Array for the data domain
0 ignored issues
show
Bug introduced by
There is no parameter named $schemaTableName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
221
     * @return $this
222 191
     */
223
    public function setDataDomain($dataDomain)
224
    {
225
        $this->dataDomain = [];
226
227
        // Iterate over data domain field definitions and perform initial and
228
        // simple normalization. We make sure the definition as a base 'type'
229
        // and it is compatible with the base Phinx types.
230
        foreach ($dataDomain as $type => $options) {
231
            if (!isset($options['type'])) {
232
                throw new \InvalidArgumentException(sprintf(
233
                    'You must specify a type for data domain type "%s".',
234
                    $type
235 208
                ));
236
            }
237 208
238
            // Replace type if it's the name of a Phinx constant
239
            if (defined('static::'.$options['type'])) {
240
                $options['type'] = constant('static::'.$options['type']);
241
            }
242
243
            if (!in_array($options['type'], $this->getColumnTypes())) {
244
                throw new \InvalidArgumentException(sprintf(
245 218
                    'An invalid column type "%s" was specified for data domain type "%s".',
246
                    $options['type'],
247 218
                    $type
248
                ));
249 218
            }
250
251
            $internal_type = $options['type'];
252
            unset($options['type']);
253
254
            // Do a simple replacement for the 'length' / 'limit' option and
255
            // detect hinting values for 'limit'.
256
            if (isset($options['length'])) {
257
                $options['limit'] = $options['length'];
258
                unset($options['length']);
259
            }
260
261
            if (isset($options['limit']) && !is_numeric($options['limit'])) {
262
                if (defined('static::'.$options['limit'])) {
263
                    $options['limit'] = constant('static::'.$options['limit']);
264
                } else {
265
                    throw new \InvalidArgumentException(sprintf(
266
                        'An invalid limit value "%s" was specified for data domain type "%s".',
267
                        $options['limit'],
268
                        $type
269
                    ));
270
                }
271
            }
272
273
            // Save the data domain types in a more suitable format
274
            $this->dataDomain[$type] = [
275
                'type' => $internal_type,
276
                'options' => $options
277
            ];
278
        }
279
280
        return $this;
281
    }
282
283
    /**
284
     * Returns a new Phinx\Db\Table\Column using the existent data domain.
285
     *
286
     * @param string $columnName The desired column name
287
     * @param string $type The type for the column. Can be a data domain type.
288
     * @param array $options Options array
289
     * @return Phinx\Db\Table\Column
290
     */
291
    public function getColumnForType($columnName, $type, $options)
292
    {
293
        $column = new Column();
294
        $column->setName($columnName);
295
296
        if (array_key_exists($type, $this->getDataDomain())) {
297
            $column->setType($this->dataDomain[$type]['type']);
298
            $column->setOptions($this->dataDomain[$type]['options']);
299
        } else {
300
            $column->setType($type);
301
        }
302
303
        $column->setOptions($options);
304
305
        return $column;
306
    }
307
308
    /**
309
     * {@inheritdoc}
310
     */
311
    public function hasSchemaTable()
312
    {
313
        return $this->hasTable($this->getSchemaTableName());
314
    }
315
316
    /**
317
     * {@inheritdoc}
318
     */
319
    public function createSchemaTable()
320
    {
321
        try {
322
            $options = [
323
                'id' => false,
324
                'primary_key' => 'version'
325
            ];
326
327
            $table = new Table($this->getSchemaTableName(), $options, $this);
328
            $table->addColumn('version', 'biginteger')
329
                ->addColumn('migration_name', 'string', ['limit' => 100, 'default' => null, 'null' => true])
330
                ->addColumn('start_time', 'timestamp', ['default' => null, 'null' => true])
331
                ->addColumn('end_time', 'timestamp', ['default' => null, 'null' => true])
332
                ->addColumn('breakpoint', 'boolean', ['default' => false])
333
                ->save();
334
        } catch (\Exception $exception) {
335
            throw new \InvalidArgumentException('There was a problem creating the schema table: ' . $exception->getMessage());
336
        }
337
    }
338
339
    /**
340
     * {@inheritdoc}
341
     */
342
    public function getAdapterType()
343
    {
344
        return $this->getOption('adapter');
345
    }
346
347
    /**
348
     * {@inheritdoc}
349
     */
350
    public function isValidColumnType(Column $column)
351
    {
352
        return in_array($column->getType(), $this->getColumnTypes());
353
    }
354
355
    /**
356
     * Determines if instead of executing queries a dump to standard output is needed
357
     *
358
     * @return bool
359
     */
360
    public function isDryRunEnabled()
361
    {
362
        $input = $this->getInput();
363
364
        return ($input && $input->hasOption('dry-run')) ? $input->getOption('dry-run') : false;
365
    }
366
}
367