Passed
Push — master ( ba9ea6...f4d366 )
by Thomas
02:08 queued 11s
created

MigrationFieldAbstract::getColumn()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
3
namespace Webfactor\Laravel\Generators\Contracts;
4
5
abstract class MigrationFieldAbstract implements FieldTypeInterface
6
{
7
    private $name;
8
9
    private $nullable = false;
10
11
    private $unique = false;
12
13
    private $default = null;
14
15
    private $foreign = null;
16
17
    public function __construct(string $name, array $options = [])
18
    {
19
        $this->name = $name;
20
21
        foreach ($options as $option) {
22
            $this->fillObject($option);
23
        }
24
    }
25
26
    private function fillObject(string $param)
27
    {
28
        if ($param == 'nullable') {
29
            return $this->nullable = true;
30
        }
31
32
        if ($param == 'unique') {
33
            return $this->unique = true;
34
        }
35
36
        if ($param == 'foreign') {
37
            return $this->foreign = true;
38
        }
39
40
        if (starts_with($param, 'default(')) {
41
            preg_match('/\((.*)\)/', $param, $match);
42
43
            return $this->default = $match[1];
44
        }
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getName()
51
    {
52
        return $this->name;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getType()
59
    {
60
        return $this->type;
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
    }
62
63
    /**
64
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
65
     */
66
    public function getDefault()
67
    {
68
        return $this->default;
69
    }
70
71
    /**
72
     * @return bool
73
     */
74
    public function isNullable()
75
    {
76
        return $this->nullable;
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function isUnique(): bool
83
    {
84
        return $this->unique;
85
    }
86
87
    abstract public function getRule(): string;
88
89
    abstract public function getColumn(): array;
90
91
    abstract public function getField(): array;
92
}
93