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

Pgsql::parseForeignKeys()   C

Complexity

Conditions 11
Paths 25

Size

Total Lines 73
Code Lines 42

Duplication

Lines 41
Ratio 56.16 %

Importance

Changes 9
Bugs 3 Features 0
Metric Value
c 9
b 3
f 0
dl 41
loc 73
rs 5.5843
cc 11
eloc 42
nc 25
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B Pgsql::getSequence() 0 38 4

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 19 and the first side effect is on line 10.

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\AdapterConfig\AbstractAdapter;
6
use Classes\Db\Column;
7
use Classes\Db\Constrant;
8
use Classes\Db\DbTable;
9
10
require_once 'Classes/AdaptersDriver/AbsractAdapter.php';
11
require_once 'Classes/Db/Column.php';
12
require_once 'Classes/Db/Constrant.php';
13
require_once 'Classes/Db/DbTable.php';
14
15
/**
16
 * @author Pedro Alarcao <[email protected]>
17
 * @link   https://github.com/pedro151/ORM-Generator
18
 */
19
class Pgsql extends AbsractAdapter
20
{
21
22
    /**
23
     * @var int
24
     */
25
    protected $port = 5432;
26
27
    /**
28
     * @type array|\string[]
29
     */
30
    protected $schema = array ( 'public' );
31
32
    protected $dataTypes = array (
33
        /* Numeric Types */
34
        'smallint'         => 'int' ,
35
        'integer'          => 'int' ,
36
        'bigint'           => 'float' ,
37
        'decimal'          => 'float' ,
38
        'numeric'          => 'float' ,
39
        'real'             => 'float' ,
40
        'double precision' => 'float' ,
41
        'serial'           => 'int' ,
42
        'bigserial'        => 'float' ,
43
        /* Monetary Types */
44
        'money'            => 'float' ,
45
        /* Character Types */
46
        'character varyin' => 'string' ,
47
        'varchar'          => 'string' ,
48
        'character'        => 'string' ,
49
        'char'             => 'string' ,
50
        'text'             => 'string' ,
51
        /* Binary Data Types */
52
        'bytea'            => 'string' ,
53
        /* Date/Time Types */
54
        'datatime'         => 'date' ,
55
        'date'             => 'date' ,
56
57
        /* Boolean Type */
58
        'boolean'          => 'boolean'
59
    );
60
61
    public function __construct ( AbstractAdapter $adapterConfig )
62
    {
63
        parent::__construct ( $adapterConfig );
64
        if ( $adapterConfig->hasSchemas () )
65
        {
66
            $this->schema = $adapterConfig->getSchemas ();
67
        }
68
69
    }
70
71
    /**
72
     * Retorna um Array com nome das tabelas
73
     *
74
     * @param void $schema
0 ignored issues
show
Bug introduced by
There is no parameter named $schema. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
75
     *
76
     * @return string[]
77
     */
78 View Code Duplication
    public function getListNameTable ()
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...
79
    {
80
        if ( empty( $this->tableList ) )
81
        {
82
            $strSchema = implode ( "', '" , $this->schema );
83
84
            $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...
85
                "SELECT table_schema,
86
              table_name
87
             FROM information_schema.tables
88
             WHERE
89
              table_type = 'BASE TABLE'
90
              AND table_schema IN ( '$strSchema' )
91
              ORDER by
92
               table_schema,
93
               table_name
94
              ASC"
95
            )->fetchAll ();
96
        }
97
98
        return $this->tableList;
99
    }
100
101
    /**
102
     * retorna multiplos arrays com dados da column em array
103
     *
104
     * @return array
105
     */
106 View Code Duplication
    public function getListColumns ()
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...
107
    {
108
        $strSchema = implode ( "', '" , $this->schema );
109
110
        return $this->getPDO ()->query (
111
            "SELECT distinct
112
	c.table_schema,
113
	c.table_name,
114
	c.column_name ,
115
	c.data_type,
116
	is_nullable,
117
	character_maximum_length AS max_length
118
		FROM
119
		information_schema.tables AS st
120
		INNER JOIN  information_schema.columns AS c
121
		ON st.table_name=c.table_name and st.table_type = 'BASE TABLE'
122
		and  c.table_schema IN ('$strSchema')
123
		order by c.table_name asc"
124
        )->fetchAll ( \PDO::FETCH_ASSOC );
125
    }
126
127 View Code Duplication
    public function getListConstrant ()
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...
128
    {
129
        $strSchema = implode ( "', '" , $this->schema );
130
131
        return $this->getPDO ()->query (
132
            "SELECT distinct
133
                tc.constraint_type,
134
                tc.constraint_name,
135
                tc.table_schema,
136
                tc.table_name,
137
                kcu.column_name,
138
		        ccu.table_schema AS foreign_schema,
139
                ccu.table_name AS foreign_table,
140
                ccu.column_name as foreign_column
141
                  FROM
142
                information_schema.table_constraints AS tc
143
                    JOIN information_schema.key_column_usage AS kcu
144
                      ON tc.constraint_name = kcu.constraint_name
145
                       AND tc.table_schema IN ('$strSchema')
146
                       AND tc.constraint_type IN ('FOREIGN KEY','PRIMARY KEY')
147
                    JOIN information_schema.constraint_column_usage AS ccu
148
                      ON tc.constraint_name  = ccu.constraint_name
149
                        AND tc.constraint_schema = ccu.constraint_schema
150
                    ORDER by tc.table_schema"
151
        )->fetchAll ( \PDO::FETCH_ASSOC );
152
    }
153
154
155
    /**
156
     * Retorna o Nome da Sequence da tabela
157
     *
158
     * @param $table
159
     * @param $column
160
     *
161
     * @return string
162
     */
163
    public function getSequence ( $table , $column )
164
    {
165
        $pdo = $this->getPDO ();
166
        $return1 = $pdo->query ( "SELECT pg_get_serial_sequence('$table', '$column');" )
167
                       ->fetchColumn ();
168
169
        if ( $return1 )
170
        {
171
            return $return1;
172
        }
173
174
        $dtbase = explode ( '.' , $table );;
175
176
        $stmt = $pdo->prepare (
177
            "SELECT adsrc FROM pg_attrdef AS att
178
            INNER JOIN pg_class AS c
179
              ON adrelid = c.oid AND att.adnum=1 AND c.relname = ?
180
            INNER JOIN pg_catalog.pg_namespace n
181
              ON n.oid = c.relnamespace and n.nspname=?"
182
        );
183
184
        $schema = isset( $dtbase[ 1 ] ) ? $dtbase[ 1 ] : 'public';
185
186
        $stmt->bindParam ( 1 , $schema );
187
        $stmt->bindParam ( 2 , $dtbase[ 0 ] );
188
        $stmt->execute ();
189
        $return2 = $stmt->fetchColumn ();
190
        if ( $return2 )
191
        {
192
            return preg_filter (
193
                array (
194
                    '/nextval\(\'/' ,
195
                    '/\'::regclass\)/'
196
                ) , '' , $return2
197
            );
198
        }
199
200
    }
201
202
    /**
203
     * @inheritDoc
204
     */
205 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...
206
    {
207
        if ( $this->hasTables () )
208
        {
209
            return $this->getAllTables ();
210
        }
211
212
        foreach ( $this->getListColumns () as $table )
213
        {
214
            $schema = $table[ 'table_schema' ];
215
            $key = $table [ 'table_name' ];
216
            if ( ! $this->hasTable ( $key , $schema ) )
217
            {
218
                $this->createTable ( $key , $schema );
219
            }
220
221
            $column = Column::getInstance ()
222
                            ->populate (
223
                                array (
224
                                    'name'       => $table [ 'column_name' ] ,
225
                                    'type'       => $this->convertTypeToPhp ( $table[ 'data_type' ] ) ,
226
                                    'nullable'   => ( $table[ 'is_nullable' ] == 'YES' ) ,
227
                                    'max_length' => $table[ 'max_length' ]
228
                                )
229
                            );
230
231
            $this->getTable ( $key , $schema )
232
                 ->addColumn ( $column )
233
                 ->setNamespace (
234
                     $this->config->createClassNamespace ( $this->getTable ( $key , $schema ) )
235
                 );
236
        }
237
    }
238
239
    /**
240
     * @inheritDoc
241
     * @return string
242
     */
243
    public function getPDOString ()
244
    {
245
        return sprintf (
246
            "pgsql:host=%s;port=%s;dbname=%s" ,
247
            $this->host ,
248
            $this->port ,
249
            $this->database
250
251
        );
252
    }
253
254
    /**
255
     * @inheritDoc
256
     * @return string
257
     */
258
    public function getPDOSocketString ()
259
    {
260
        return sprintf (
261
            "pgsql:unix_socket=%s;dbname=%s" ,
262
            $this->socket ,
263
            $this->database
264
265
        );
266
    }
267
268
    /**
269
     * retorna o numero total de tabelas
270
     *
271
     * @return int
272
     */
273 View Code Duplication
    public function getTotalTables ()
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...
274
    {
275
        if ( empty( $this->totalTables ) )
276
        {
277
            $strSchema = implode ( "', '" , $this->schema );
278
279
            $this->totalTables = $this->getPDO ()->query (
280
                "SELECT COUNT(table_name)  AS total
281
             FROM information_schema.tables
282
             WHERE
283
              table_type = 'BASE TABLE'
284
              AND table_schema IN ( '" . $strSchema . "' )"
285
            )->fetchColumn ();
286
        }
287
288
        return (int) $this->totalTables;
289
    }
290
}
291