Column::getTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the "RocketORM" package.
5
 *
6
 * https://github.com/RocketORM/ORM
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rocket\ORM\Generator\Schema;
13
14
use Rocket\ORM\Model\Map\TableMap;
15
16
/**
17
 * @author Sylvain Lorinet <[email protected]>
18
 */
19
class Column
20
{
21
    /**
22
     * @var Table
23
     */
24
    protected $table;
25
26
    /**
27
     * @var string
28
     */
29
    public $name;
30
31
    /**
32
     * @var string
33
     */
34
    public $phpName;
35
36
    /**
37
     * @var int
38
     */
39
    public $type;
40
41
    /**
42
     * @var int
43
     */
44
    public $size;
45
46
    /**
47
     * @var int
48
     */
49
    public $decimal;
50
51
    /**
52
     * @var mixed
53
     */
54
    protected $default;
55
56
    /**
57
     * @var bool
58
     */
59
    public $isRequired = true;
60
61
    /**
62
     * @var bool
63
     */
64
    public $isPrimaryKey = false;
65
66
    /**
67
     * @var bool
68
     */
69
    public $isAutoIncrement = false;
70
71
    /**
72
     * @var array
73
     */
74
    public $values;
75
76
    /**
77
     * @var string
78
     */
79
    public $description;
80
81
82
    /**
83
     * @param string $name
84
     * @param array  $data
85
     */
86 36
    public function __construct($name, array $data)
87
    {
88 36
        $this->name            = $name;
89 36
        $this->phpName         = $data['phpName'];
90 36
        $this->size            = $data['size'];
91 36
        $this->type            = $data['type'];
92 36
        $this->decimal         = $data['decimal'];
93 36
        $this->default         = $data['default'];
94 36
        $this->isRequired      = $data['required'];
95 36
        $this->isPrimaryKey    = $data['primaryKey'];
96 36
        $this->isAutoIncrement = $data['autoIncrement'];
97 36
        $this->values          = $data['values'];
98 36
        $this->description     = $data['description'];
99 36
    }
100
101
    /**
102
     * @return Table
103
     */
104 1
    public function getTable()
105
    {
106 1
        return $this->table;
107
    }
108
109
    /**
110
     * @param Table $table
111
     */
112 1
    public function setTable(Table $table)
113
    {
114 1
        $this->table = $table;
115 1
    }
116
117
    /**
118
     * @return string
119
     */
120 19 View Code Duplication
    public function getTypeConstantName()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122 19
        $reflection = new \ReflectionClass('\\Rocket\\ORM\\Model\\Map\\TableMap');
123 19
        foreach ($reflection->getConstants() as $name => $value) {
124 19
            if ($this->type == $value) {
125 18
                return $name;
126
            }
127 17
        }
128
129 1
        throw new \LogicException(
130 1
            'Unknown value "' . $this->type . '" for constant TableMap::COLUMN_TYPE_* for column "' . $this->name . '"'
131 1
        );
132
    }
133
134
    /**
135
     * @param bool $raw
136
     *
137
     * @return int|mixed
138
     */
139 2
    public function getDefault($raw = false)
140
    {
141 2
        if (TableMap::COLUMN_TYPE_ENUM == $this->type) {
142 2
            if ($raw) {
143 1
                return $this->default;
144
            }
145
146 2
            foreach ($this->values as $i => $value) {
147 2
                if ($value == $this->default) {
148 1
                    return $i;
149
                }
150 2
            }
151
152 1
            throw new \LogicException('The default value for column "' . $this->name . '" is not found');
153
        }
154
155 1
        return $this->default;
156
    }
157
158
    /**
159
     * @return string
160
     */
161 1
    public function getAttributePhpDoc()
162
    {
163 1
        $doc = "/**" . PHP_EOL;
164
165 1
        $startDoc = '     * ';
166 1
        if (null != $this->description) {
167 1
            $doc .= $startDoc . str_replace('*/', '', $this->description) . PHP_EOL . $startDoc . PHP_EOL;
168 1
        }
169
170 1
        return $doc . $startDoc . '@var ' . $this->getTypeAsPhpDoc() . PHP_EOL . '     */';
171
    }
172
173
    /**
174
     * @return string
175
     */
176 11
    public function getTypeAsPhpDoc()
177
    {
178 11
        switch ($this->type) {
179 11
            case TableMap::COLUMN_TYPE_BOOLEAN:  return 'bool';
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
180 10
            case TableMap::COLUMN_TYPE_DATE:
181 10
            case TableMap::COLUMN_TYPE_DATETIME: return '\DateTime';
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
182 8
            case TableMap::COLUMN_TYPE_DOUBLE:   return 'double';
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
183 7
            case TableMap::COLUMN_TYPE_ENUM:
184 7
            case TableMap::COLUMN_TYPE_INTEGER:  return 'int';
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
185 5
            case TableMap::COLUMN_TYPE_FLOAT:    return 'float';
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
186 4
            case TableMap::COLUMN_TYPE_STRING:
187 4
            case TableMap::COLUMN_TYPE_TEXT:     return 'string';
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
188 1
        }
189
190 1
        return 'mixed';
191
    }
192
193
    /**
194
     * @param bool $firstLetterUpper
195
     *
196
     * @return string
197
     */
198 3
    public function getPhpName($firstLetterUpper = false)
199
    {
200 3
        if ($firstLetterUpper) {
201 3
            return ucfirst($this->phpName);
202
        }
203
204 3
        return $this->phpName;
205
    }
206
207
    /**
208
     * @return string
209
     */
210
    public function getMethodName()
211
    {
212
        // Do not prefix method name when the column phpName starts by "is" or "has"
213
        if (0 === strpos($this->phpName, 'is') || 0 === strpos($this->phpName, 'has')) {
214
            return $this->phpName;
215
        }
216
217
        return 'get' . $this->getPhpName(true);
218
    }
219
220
    /**
221
     * @return string
222
     */
223 9
    public function getTypeAsString()
224
    {
225 9
        $constant = $this->getTypeConstantName();
226 9
        $parts = explode('_', $constant);
227
228 9
        return strtolower($parts[sizeof($parts) - 1]);
229
    }
230
}
231