Migration_Driver_Mysql_Column::column_params_for()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
3
class Migration_Driver_Mysql_Column extends Migration_Driver_Column
4
{
5
	/**
6
	 * Valid types
7
	 * @var array
8
	 */
9
	static protected $types = array
10
	(
11
		'primary_key' => array('type' => 'INT', 'null' => FALSE, 'auto' => TRUE, 'primary' => TRUE),
12
		'string' => array('type' => 'VARCHAR', 'limit' => 255),
13
		'text' => array('type' => 'TEXT'),
14
		'integer' => array('type' => 'INT'),
15
		'float' => array('type' => 'FLOAT', 'limit' => 10, 'precision' => 2),
16
		'long' => array('type' => 'BIGINT'),
17
		'decimal' => array('type' => 'DECIMAL', 'limit' => 10, 'precision' => 2),
18
		'datetime' => array('type' => 'DATETIME'),
19
		'timestamp' => array('type' => 'TIMESTAMP'),
20
		'time' => array('type' => 'TIME'),
21
		'date' => array('type' => 'DATE'),
22
		'binary' => array('type' => 'BLOB', 'limit' => 255),
23
		'boolean' => array('type' => 'TINYINT', 'limit' => 1, 'null' => FALSE, 'default' => 0),
24
		'enum' => array('type' => 'ENUM', 'values' => array()),
25
	);
26
27
	static protected $native_types = array
28
	(
29
		'char' => 'string',
30
		'varchar' => 'string',
31
		'text' => 'text',
32
		'int' => 'integer',
33
		'integer' => 'integer',
34
		'tinyint' => 'boolean',
35
		'bigint' => 'integer',
36
		'float' => 'float',
37
		'decimal' => 'decimal',
38
		'datetime' => 'datetime',
39
		'timestamp' => 'timestamp',
40
		'time' => 'time',
41
		'date' => 'date',
42
		'blob' => 'binary',
43
		'enum' => 'enum',
44
	);
45
46
	public function column_params_for($column)
47
	{
48
		return Arr::get(self::$types, $column, array());
49
	}
50
51
	public function load($table_name)
52
	{
53
		if (is_string($table_name))
54 27
		{
55
			try
56 27
			{
57
				$result = $this->driver->pdo->query("SHOW COLUMNS FROM `$table_name` LIKE '{$this->name}'");
0 ignored issues
show
Bug introduced by
The property pdo does not seem to exist in Migration_Driver.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
58
			}
59 2
			catch (PDOException $e)
60
			{
61 2
				$result = NULL;
62 2
			}
63
64
			if ( ! $result OR $result->rowCount() !== 1)
65 2
			{
66
				throw new Migration_Exception("Column :column was not found in table :table", array(':column' => $this->name, ':table' => $this->name));
67 2
			}
68
69 2
			$result = $result->fetchObject();
70
		}
71
		else
72 2
		{
73 2
			$result = $table_name;
74 2
		}
75
76
		if (preg_match('/([^\(]+)(\((\d+)\))?( UNSIGNED)?/', $result->Type, $type))
77
		{
78
			$limit = Arr::get($type, 3);
79
			$unsigned = isset($type[4]) ? TRUE : NULL;
80
			$type = $type[1];
81
			$values = NULL;
82
		}
83
84
		if (preg_match('/enum\(([^\)]+)\)/', $result->Type, $enum_type))
85
		{
86
			$type = 'ENUM';
87
			$limit = NULL;
88
			$unsigned = NULL;
89
			$values = explode(',', $enum_type[1]);
90
			foreach ($values as & $value)
91
			{
92
				$value = trim($value, "'");
93
			}
94
		}
95
96
		$this->params(array(
97
			'type' => $type,
98
			'limit' => $limit,
0 ignored issues
show
Bug introduced by
The variable $limit does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
99
			'unsigned' => $unsigned,
0 ignored issues
show
Bug introduced by
The variable $unsigned does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
100
			'values' => $values,
0 ignored issues
show
Bug introduced by
The variable $values does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
101
			'null' => $result->Null == 'NO' ? FALSE : TRUE,
102
			'default' => $result->Default ? $result->Default : NULL,
103
			'auto' => $result->Extra == 'auto_increment',
104
			'primary' => $result->Key == 'PRI',
105
		));
106
107
		return $this;
108
	}
109
110
	public function type()
111
	{
112
		return Arr::get(self::native_types, strtolower($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...
113
	}
114
115
	public function sql()
116
	{
117
		extract(Arr::extract($this->params, Migration_Driver_Column::$available_params));
0 ignored issues
show
Bug introduced by
\Arr::extract($this->par...umn::$available_params) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
118
119
		return join(' ', array_filter(array(
120
			"`{$this->name}`",
121
			$type,
122
			$limit ? ($precision ? ( "({$limit}, {$precision})" ) : "({$limit})") : NULL,
123 28
			$values ? ('('.join(', ', array_map(array($this->driver->pdo, 'quote'), $values)).')') : NULL,
0 ignored issues
show
Bug introduced by
The property pdo does not seem to exist in Migration_Driver.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
124
			$unsigned ? ("UNSIGNED") : NULL,
125 28
			($default OR $default === 0 OR $default === '0') ? ("DEFAULT ".$this->driver->pdo->quote($default)) : NULL,
126
			$null !== NULL ? ($null ? "NULL" : "NOT NULL") : NULL,
127 28
			$auto ? ("AUTO_INCREMENT") : NULL,
128 28
			$comment ? ("COMMENT '{$comment}'") : NULL,
129 28
			$after ? ("AFTER `{$after}`") : NULL,
130 28
			$first ? ("FIRST") : NULL,
131 28
		)));
132 28
	}
133
}
134