AddToTable::getItem()   B
last analyzed

Complexity

Conditions 6
Paths 18

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 25
nc 18
nop 1
dl 0
loc 42
ccs 0
cts 34
cp 0
crap 42
rs 8.8977
c 2
b 0
f 0
1
<?php namespace Xethron\MigrationsGenerator\Syntax;
2
3
/**
4
 * Class AddToTable
5
 * @package Xethron\MigrationsGenerator\Syntax
6
 */
7
class AddToTable extends Table
8
{
9
    /**
10
     * Return string for adding a column
11
     *
12
     * @param  array  $field
13
     * @return string
14
     */
15
    protected function getItem(array $field): string
16
    {
17
        $property = $field['field'];
18
19
        // If the field is an array,
20
        // make it an array in the Migration
21
        if (is_array($property)) {
22
            $property = "['".implode("', '", $property)."']";
23
        } else {
24
            $property = $property ? "'$property'" : null;
25
        }
26
27
        $type = $field['type'];
28
29
        $output = sprintf(
30
            "\$table->%s(%s)",
31
            $type,
32
            $property
33
        );
34
35
        // If we have args, then it needs
36
        // to be formatted a bit differently
37
        if (!empty($field['args'])) {
38
            if ($property !== null) {
39
                $output = sprintf(
40
                    "\$table->%s(%s, %s)",
41
                    $type,
42
                    $property,
43
                    implode(', ', $field['args'])
44
                );
45
            } else {
46
                $output = sprintf(
47
                    "\$table->%s(%s)",
48
                    $type,
49
                    implode(', ', $field['args'])
50
                );
51
            }
52
        }
53
        if (isset($field['decorators'])) {
54
            $output .= $this->addDecorators($field['decorators']);
55
        }
56
        return $output.';';
57
    }
58
}
59