Column   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 14.52 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 9
loc 62
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B scanTextColumns() 0 26 5
A primaryKey() 0 19 4
A textColumns() 9 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Tequilarapido\Database;
2
3
use Tequilarapido\Database\Database;
4
5
class Column
6
{
7
    protected static $db_primarykeys = array();
8
9
    public function scanTextColumns($database, $exclude = array())
10
    {
11
        // Text Columns
12
        $db_text_columns = $this->textColumns($database);
13
14
        $db_columns = array();
15
        foreach ($db_text_columns as $column) {
16
            if (is_numeric($column['TABLE_NAME'])) {
17
                continue;
18
            }
19
20
            if (in_array($column['TABLE_NAME'], $exclude)) {
21
                continue;
22
            }
23
24
            if (empty($db_columns[$column['TABLE_NAME']])) {
25
                $db_columns[$column['TABLE_NAME']] = array();
26
                $db_columns[$column['TABLE_NAME']]['columns'] = array();
27
                $db_columns[$column['TABLE_NAME']]['pk'] = $this->primaryKey($column['TABLE_NAME']);
28
            }
29
30
            $db_columns[$column['TABLE_NAME']]['columns'][] = $column['COLUMN_NAME'];
31
        }
32
33
        return $db_columns;
34
    }
35
36
    public function primaryKey($table)
37
    {
38
        // Have we already a PK for this table
39
        if (!empty(static::$db_primarykeys[$table])) {
40
            return static::$db_primarykeys[$table];
41
        }
42
43
        $query = "SHOW KEYS FROM $table ";
44
        $results = Database::select($query);
45
        $keys = array();
46
        foreach ($results as $row) {
47
            if ($row['Key_name'] == 'PRIMARY') {
48
                $keys[$row['Seq_in_index'] - 1] = $row['Column_name'];
49
            }
50
        }
51
52
        static::$db_primarykeys[$table] = current($keys);
53
        return static::$db_primarykeys[$table];
54
    }
55
56 View Code Duplication
    public static function textColumns($database)
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...
57
    {
58
        $query = "SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS ";
59
        $query .= "WHERE 1 ";
60
        $query .= "AND TABLE_SCHEMA = ? ";
61
        $query .= "AND ( DATA_TYPE LIKE '%char%' OR DATA_TYPE LIKE '%text%' OR DATA_TYPE LIKE '%BLOB%' )";
62
63
        return Database::select($query, array($database));
64
    }
65
66
}