GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( fd5ec3...0fd339 )
by Pedro
02:41
created

Mysql::parseForeignKeys()   C

Complexity

Conditions 11
Paths 25

Size

Total Lines 72
Code Lines 40

Duplication

Lines 40
Ratio 55.56 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 40
loc 72
rs 5.6331
cc 11
eloc 40
nc 25
nop 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A Mysql::getPDOString() 0 10 1
A Mysql::getPDOSocketString() 0 9 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 9.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Classes\AdaptersDriver;
4
5
use Classes\Db\Column;
6
use Classes\Db\Constrant;
7
use Classes\Db\DbTable;
8
9
require_once 'Classes/AdaptersDriver/AbsractAdapter.php';
10
require_once 'Classes/Db/Column.php';
11
require_once 'Classes/Db/Constrant.php';
12
require_once 'Classes/Db/DbTable.php';
13
14
/**
15
 * @author Pedro Alarcao <[email protected]>
16
 * @link   https://github.com/pedro151/ORM-Generator
17
 */
18
class Mysql extends AbsractAdapter
19
{
20
21
    /**
22
     * @var int
23
     */
24
    protected $port = 3306;
25
26
    /**
27
     * converts MySQL data types to PHP data types
28
     *
29
     * @param string $str
30
     *
31
     * @return string
32
     */
33
    protected function convertTypeToPhp ( $str )
34
    {
35
        if ( preg_match ( '/(tinyint\(1\)|bit)/' , $str ) )
36
        {
37
            $res = 'boolean';
38
        } elseif ( preg_match ( '/(datetime|timestamp|blob|char|enum|text|date)/' , $str ) )
39
        {
40
            $res = 'string';
41
        } elseif ( preg_match ( '/(decimal|numeric|float|double)/' , $str ) )
42
        {
43
            $res = 'float';
44
        } elseif ( preg_match ( '#^(?:tiny|small|medium|long|big|var)?(\w+)(?:\(\d+\))?(?:\s\w+)*$#' , $str , $matches ) )
45
        {
46
            $res = $matches[ 1 ];
47
        } else
48
        {
49
            print "Can't convert column type to PHP - Unrecognized type: $str";
50
        }
51
52
        return $res;
0 ignored issues
show
Bug introduced by
The variable $res 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...
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58 View Code Duplication
    public function parseTables ()
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...
59
    {
60
        if ( $this->hasTables () )
61
        {
62
            return $this->getAllTables ();
63
        }
64
65
        $schema = 0;
66
        foreach ( $this->getListColumns () as $table )
67
        {
68
            $key = $table [ 'table_name' ];
69
            if ( ! $this->hasTable ( $key , $schema ) )
70
            {
71
                $this->createTable ( $key , $schema );
72
            }
73
74
            $column = Column::getInstance ()
75
                            ->populate (
76
                                array (
77
                                    'name'       => $table [ 'column_name' ] ,
78
                                    'type'       => $this->convertTypeToPhp ( $table[ 'data_type' ] ) ,
79
                                    'nullable'   => ( $table[ 'is_nullable' ] == 'YES' ) ,
80
                                    'max_length' => $table[ 'max_length' ]
81
                                )
82
                            );
83
84
            $this->getTable ( $key , $schema )
85
                 ->addColumn ( $column )
86
                 ->setNamespace (
87
                     $this->config->createClassNamespace ( $this->getTable ( $key , $schema ) )
88
                 );
89
        }
90
    }
91
92
    /**
93
     * @inheritDoc
94
     * @return string
95
     */
96
    public function getPDOString ()
97
    {
98
        return sprintf (
99
            "mysql:host=%s;port=%s;dbname=%s" ,
100
            $this->host ,
101
            $this->port ,
102
            $this->database
103
104
        );
105
    }
106
107
    /**
108
     * @inheritDoc
109
     * @return string
110
     */
111
    public function getPDOSocketString ()
112
    {
113
        return sprintf (
114
            "mysql:unix_socket=%s;dbname=%s" ,
115
            $this->socket ,
116
            $this->database
117
118
        );
119
    }
120
121
    /**
122
     * @inheritDoc
123
     * @return string[]
124
     */
125
    public function getListNameTable ()
126
    {
127
        if ( empty( $this->tableList ) )
128
        {
129
            $this->tableList = $this->getPDO ()->query (
0 ignored issues
show
Bug introduced by
The property tableList 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...
130
                "show tables"
131
            )->fetchAll ();
132
        }
133
134
        return $this->tableList;
135
    }
136
137
    /**
138
     * retorna multiplos arrays com dados da column em array
139
     *
140
     * @return array[]
141
     */
142
    public function getListColumns ()
143
    {
144
145
        return $this->getPDO ()->query (
146
            "select
147
                0 AS table_schema,
148
                table_name,
149
                column_name ,
150
                data_type,
151
                is_nullable,
152
                character_maximum_length AS max_length
153
            from information_schema.columns
154
            where table_schema IN ('{$this->database}')
155
            order by table_name,ordinal_position"
156
        )->fetchAll ( \PDO::FETCH_ASSOC );
157
    }
158
159
    /**
160
     * @return array
161
     */
162
    public function getListConstrant ()
163
    {
164
        return $this->getPDO ()->query (
165
            "SELECT distinct
166
     i.constraint_type,
167
     k.constraint_name,
168
     --k.table_schema,
169
     0 AS table_schema,
170
     k.table_name,
171
	 k.column_name,
172
     k.REFERENCED_TABLE_SCHEMA AS foreign_schema,
173
	 k.REFERENCED_TABLE_NAME AS foreign_table,
174
	 k.REFERENCED_COLUMN_NAME AS foreign_column
175
FROM information_schema.TABLE_CONSTRAINTS as i
176
inner join information_schema.key_column_usage as k
177
ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME and k.TABLE_SCHEMA <> 'mysql'
178
WHERE
179
i.TABLE_SCHEMA IN ('{$this->database}') AND i.CONSTRAINT_TYPE IN ('FOREIGN KEY', 'PRIMARY KEY' )
180
order by k.table_schema, k.table_name;"
181
        )->fetchAll ( \PDO::FETCH_ASSOC );
182
    }
183
184
    /**
185
     * retorna o numero total de tabelas
186
     *
187
     * @return int
188
     */
189
    public function getTotalTables ()
190
    {
191
        if ( empty( $this->totalTables ) )
192
        {
193
194
            $this->totalTables = $this->getPDO ()->query (
195
                "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '{$this->database}'"
196
            )->fetchColumn ();
197
        }
198
199
        return (int) $this->totalTables;
200
    }
201
202
    /**
203
     * @inheritDoc
204
     *
205
     * @param $table
206
     * @param $column
207
     */
208
    public function getSequence ( $table , $column )
209
    {
210
        $return = $this->getPDO ()
211
                       ->query ( "select * from information_schema.columns where extra like '%auto_increment%' and  TABLE_SCHEMA='{$this->database}' AND TABLE_NAME='{$table}' AND COLUMN_NAME='{$column}';" )
212
                       ->fetch ( \PDO::FETCH_ASSOC );
213
214
        if ( ! $return )
215
        {
216
            return;
217
        }
218
219
        return "{$return['TABLE_NAME']}_{$return['COLUMN_NAME']}_seq";
220
    }
221
222
}
223