Completed
Push — master ( c928d4...502244 )
by Viacheslav
12:21 queued 07:59
created

Columns::__set()   C

Complexity

Conditions 8
Paths 36

Size

Total Lines 50
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 6.3636
c 0
b 0
f 0
cc 8
eloc 31
nc 36
nop 2
1
<?php
2
3
namespace Yaoi\Database\Definition;
4
5
use Yaoi\String\Utils;
6
7
class Columns
8
{
9
    /**
10
     * @var Column[]
11
     */
12
    private $_arrayOfColumnData = array();
13
    /**
14
     * @var Table
15
     */
16
    private $table;
17
18
    public function __construct(Table $table)
19
    {
20
        $this->table = $table;
21
        $table->columns = $this;
22
    }
23
24
    public function __set($name, $column)
25
    {
26
        if (is_int($column)) {
27
            $column = new Column($column);
28
            //$this->_arrayOfColumnData[$name] = $column;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29
        }
30
31
        // another column reference
32
        if (!empty($column->table)
33
            && $column->table->schemaName != $this->table->schemaName) {
34
            $refColumn = $column;
35
            $column = clone $column;
36
37
            $column->propertyName = $name;
38
            $column->schemaName = Utils::fromCamelCase($name);
39
            $column->table = $this->table;
40
41
            //$this->_arrayOfColumnData[$name] = $column;
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
            $foreignKey = new ForeignKey(array($column), array($refColumn));
43
            $column->foreignKey = $foreignKey;
44
            //$this->table->addForeignKey($foreignKey);
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
45
            $column->setFlag(Column::AUTO_ID, false);
46
        } else {
47
            $column->propertyName = $name;
48
            $column->schemaName = Utils::fromCamelCase($name);
49
            $column->table = $this->table;
50
        }
51
52
53
        if ($column->flags & Column::AUTO_ID) {
54
            $this->table->autoIdColumn = $column;
55
            if (!$this->table->primaryKey) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->table->primaryKey of type Yaoi\Database\Definition\Column[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
56
                $this->table->setPrimaryKey($column);
57
            }
58
        }
59
60
        if ($column->isUnique) {
61
            $index = new Index($column);
62
            $index->setType(Index::TYPE_UNIQUE);
63
            $this->table->addIndex($index);
64
        } elseif ($column->isIndexed) {
65
            $index = new Index($column);
66
            $index->setType(Index::TYPE_KEY);
67
            $this->table->addIndex($index);
68
        }
69
70
        $this->table->database()->getUtility()->checkColumn($column);
71
        $this->_arrayOfColumnData[$name] = $column;
72
73
    }
74
75
    public function __get($name)
76
    {
77
        if (!isset($this->_arrayOfColumnData[$name])) {
78
            throw new Exception('Unknown column ' . $name);
79
        }
80
        return $this->_arrayOfColumnData[$name];
81
    }
82
83
    public function __isset($name)
84
    {
85
        return isset($this->_arrayOfColumnData[$name]);
86
    }
87
88
    public function __unset($name)
89
    {
90
        if (!isset($this->_arrayOfColumnData[$name])) {
91
            return;
92
        }
93
        unset($this->_arrayOfColumnData[$name]);
94
    }
95
96
    /**
97
     * @return Column[]
98
     */
99
    public function getArray()
100
    {
101
        return $this->_arrayOfColumnData;
102
    }
103
}