Migration_Driver_Mysql_Table::sql()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.25

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 12
cts 16
cp 0.75
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 13
nc 6
nop 0
crap 4.25
1
<?php defined('SYSPATH') OR die('No direct script access.');
2
3
class Migration_Driver_Mysql_Table extends Migration_Driver_Table
4
{
5
6
	public function params(array $columns = NULL, array $options = NULL)
7
	{
8
		$columns = (array) $columns;
9
		$this->options = Arr::get( (array) $options, 'options');
0 ignored issues
show
Documentation Bug introduced by
It seems like \Arr::get((array) $options, 'options') of type * is incompatible with the declared type array of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
10
11
		if (Arr::get((array) $options, 'id', TRUE))
12
		{
13
			$columns = array_merge(array('id' => 'primary_key'), $columns);
14 1
		}
15
16 1
		foreach ($columns as $column_name => $params)
17 1
		{
18
			$this->columns[$column_name] = $this->driver->column($column_name)->params($params);
19 1
		}
20 1
21 1
		return $this;
22 1
	}
23
24 1
	public function load()
25
	{
26 1
		$this->columns = array();
27 1
		$this->options = array();
28
		$this->keys = array();
0 ignored issues
show
Bug introduced by
The property keys 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...
29 1
30
		$result = $this->driver->pdo->query("SHOW TABLE STATUS 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...
31
32 1
		if ($result->rowCount() !== 1)
33
		{
34 1
			throw new Migration_Exception(":table does not exist", array(':table' => $this->name));
35 1
		}
36 1
37
		$table->options[] = 'ENGINE='.$result->fetchObject()->Engine;
0 ignored issues
show
Bug introduced by
The variable $table does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
38 1
39
		$fields_reuslt = $this->driver->pdo->query("SHOW COLUMNS FROM `{$this->name}`");
40 1
41 1
		while($result = $fields_reuslt->fetchObject())
42 1
		{
43
			$this->columns[$result->Field] = $this->driver->column($result->Field)->load($result);
44
		}
45
		return $this;
46
	}
47
48
	public function sql()
49
	{
50
		$primary_keys = array();
51
		$columns = array();
52
53
		foreach ($this->columns as $column)
54
		{
55
			$columns[] = $column->sql();
56 1
			if ($column->param('primary'))
57
			{
58 1
				$primary_keys[] = "`".$column->name()."`";
59 1
			}
60
		}
61 1
62
		if ($primary_keys)
0 ignored issues
show
Bug Best Practice introduced by
The expression $primary_keys of type array 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...
63 1
		{
64 1
			$columns[] = 'PRIMARY KEY ('.join(', ', $primary_keys).')';
65 1
		}
66 1
67 1
		return join(' ', array_filter(array(
68 1
			"CREATE TABLE `{$this->name}`",
69
			'('. join(', ', $columns).')',
70
			$this->options
71 1
		)));
72 1
	}
73
}
74