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.
Completed
Pull Request — master (#29)
by Pedro
06:35
created

Mssql::getHost()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 0
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 Mssql extends AbsractAdapter
20
{
21
22
    /**
23
     * @var int
24
     */
25
    protected $port;
26
27
    protected $schema = array ( 'public' );
28
29
    public function __construct ( AbstractAdapter $adapterConfig )
30
    {
31
        parent::__construct ( $adapterConfig );
32
        if ( $adapterConfig->hasSchemas () ) {
33
            $this->schema = $adapterConfig->getSchemas ();
34
        }
35
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    protected function convertTypeToSimple ( $str )
42
    {
43
        if ( preg_match ( '/(tinyint|bit)/' , $str ) )
44
        {
45
            $res = 'boolean';
46
        } elseif ( preg_match ( '/(date|time|text|binary|char|xml|uniqueidentifier)/' , $str ) )
47
        {
48
            $res = 'string';
49
        } elseif ( preg_match ( '/(decimal|numeric|real|float|money)/' , $str ) )
50
        {
51
            $res = 'float';
52
        } elseif ( preg_match ( '#^(?:tiny|small|medium|long|big|var)?(\w+)(?:\(\d+\))?(?:\s\w+)*$#' , $str , $matches ) )
53
        {
54
            $res = $matches[ 1 ];
55
        }
56
57
        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...
58
    }
59
60
    protected function getHost(){
61
        $host = $this->host;
62
        if (!empty($this->port)) {
63
            $seperator = ':';
64
            if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
65
                $seperator = ',';
66
            }
67
            $host .=  $seperator . $this->port;
68
            unset($this->port);
69
        }
70
71
        return $host;
72
    }
73
74
    /**
75
     * @inheritDoc
76
     * @return string
77
     */
78
    public function getPDOString ()
79
    {
80
        return sprintf (
81
            "mssql:host=%s;dbname=%s" ,
82
            $this->getHost() ,
83
            $this->database
84
        );
85
    }
86
87
    /**
88
     * @inheritDoc
89
     * @return string
90
     */
91
    public function getPDOSocketString ()
92
    {
93
        // TODO: implement here
94
        return "";
95
    }
96
97
    /**
98
     * @inheritDoc
99
     * @return string[]
100
     */
101 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...
102
    {
103
        if ( empty( $this->tableList ) )
104
        {
105
106
            $sqlTables = ! empty( $this->tablesName )
107
                ? "AND table_name IN ( $this->tablesName )" : '';
108
109
            $strSchema = implode ( "', '" , $this->schema );
110
111
            $this->tableList = $this->getPDO ()
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...
112
                                    ->query (
113
                                        "SELECT table_schema,
114
              table_name
115
             FROM {$this->database}.information_schema.tables
116
             WHERE
117
              table_type = 'BASE TABLE'
118
              AND table_schema IN ( '$strSchema' ) $sqlTables
119
              ORDER by
120
               table_schema,
121
               table_name
122
              ASC"
123
                                    )
124
                                    ->fetchAll ();
125
        }
126
127
        return $this->tableList;
128
    }
129
130
    /**
131
     * retorna multiplos arrays com dados da column em array
132
     *
133
     * @return array[]
134
     */
135
136
    /**
137
     * retorna multiplos arrays com dados da column em array
138
     *
139
     * @return array
140
     */
141
    public function getListColumns ()
142
    {
143
        $sqlTables = ! empty( $this->tablesName )
144
            ? "AND c.table_name IN ( $this->tablesName )" : '';
145
        $strSchema = implode ( "', '" , $this->schema );
146
147
        return $this->getPDO ()
148
                    ->query (
149
                        "SELECT distinct
150
	c.table_schema,
151
	c.table_name,
152
	c.column_name ,
153
	c.data_type,
154
	is_nullable,
155
	character_maximum_length AS max_length
156
		FROM
157
		{$this->database}.information_schema.tables AS st
158
		INNER JOIN  {$this->database}.information_schema.columns AS c
159
		ON st.table_name=c.table_name and st.table_type = 'BASE TABLE'
160
		 $sqlTables and  c.table_schema IN ('$strSchema')
161
		order by c.table_name asc"
162
                    )
163
                    ->fetchAll ( \PDO::FETCH_ASSOC );
164
    }
165
166
    /**
167
     * retorna o numero total de tabelas
168
     *
169
     * @return int
170
     */
171 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...
172
    {
173
        if ( empty( $this->totalTables ) )
174
        {
175
            $sqlTables = ! empty( $this->tablesName )
176
                ? "AND table_name IN ( $this->tablesName )" : '';
177
178
            $strSchema = implode ( "', '" , $this->schema );
179
180
            $this->totalTables = $this->getPDO ()
181
                                      ->query (
182
                                          "SELECT COUNT(table_name)  AS total
183
             FROM {$this->database}.information_schema.tables
184
             WHERE
185
              table_type = 'BASE TABLE'
186
              AND table_schema IN ( '" . $strSchema . "' ) $sqlTables"
187
                                      )
188
                                      ->fetchColumn ();
189
        }
190
191
        return (int) $this->totalTables;
192
    }
193
194
    public function getSequence ( $table , $column , $schema = 0 )
195
    {
196
        $return = $this->getPDO ()
197
                       ->query (
198
                           "SELECT is_identity FROM sys.columns WHERE object_id = object_id('{$schema}.{$table}')  AND name = '{$column}';"
199
                       )
200
                       ->fetchColumn();
201
202
        if ( ! $return )
203
        {
204
            return;
205
        }
206
207
        return "{$table}_{$column}_seq";
208
    }
209
210
    public function getListConstrant ()
211
    {
212
        $sqlTables = ! empty( $this->tablesName )
213
            ? "AND tc.table_name IN ( $this->tablesName )" : '';
214
        $strSchema = implode ( "', '" , $this->schema );
215
216
        return $this->getPDO ()
217
                    ->query (
218
                        "SELECT distinct
219
                tc.constraint_type,
220
                tc.constraint_name,
221
                tc.table_schema,
222
                tc.table_name,
223
                kcu.column_name,
224
		        ccu.table_schema AS foreign_schema,
225
                ccu.table_name AS foreign_table,
226
                ccu.column_name as foreign_column
227
                  FROM
228
                {$this->database}.information_schema.table_constraints AS tc
229
                    JOIN {$this->database}.information_schema.key_column_usage AS kcu
230
                      ON tc.constraint_name = kcu.constraint_name
231
                       AND tc.table_schema IN ('$strSchema')
232
                       AND tc.constraint_type IN ('FOREIGN KEY','PRIMARY KEY')
233
                       $sqlTables
234
                    JOIN {$this->database}.information_schema.constraint_column_usage AS ccu
235
                      ON tc.constraint_name  = ccu.constraint_name
236
                        AND tc.constraint_schema = ccu.constraint_schema
237
                    ORDER by tc.table_schema"
238
                    )
239
                    ->fetchAll ( \PDO::FETCH_ASSOC );
240
    }
241
242
}
243