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 (#13)
by Pedro
04:05
created

AbstractAdapter::hasTablesName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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 16 and the first side effect is on line 8.

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\AdapterConfig;
4
5
use Classes\Config;
6
use Classes\Maker\AbstractMaker;
7
8
require_once "Classes/Maker/AbstractMaker.php";
9
require_once "Classes/Config.php";
10
require_once 'Exception.php';
11
12
/**
13
 * @author Pedro Alarcao <[email protected]>
14
 * @link   https://github.com/pedro151/orm-generator
15
 */
16
abstract class AbstractAdapter
17
{
18
19
    protected $arrConfig = array (
20
        ############################# DATABASE
21
        //Driver do banco de dados
22
        'driver'          => null ,
23
        //Nome do banco de dados
24
        'database'        => null ,
25
        //Host do banco
26
        'host'            => 'localhost' ,
27
        //Port do banco
28
        'port'            => '' ,
29
        //usuario do banco
30
        'username'        => null ,
31
        //senha do banco
32
        'password'        => null ,
33
        // lista de schemas do banco de dados
34
        'schema'          => array () ,
35
        'version'         => '' ,
36
        'socket'          => null ,
37
38
        ########################### DOCS
39
        // autor que gerou o script
40
        'author'          => "Pedro" ,
41
        'license'         => "New BSD License" ,
42
        'copyright'       => "ORM Generator - Pedro151" ,
43
        'link'            => 'https://github.com/pedro151/orm-generator' ,
44
        'version'         => '' ,
45
        // data que foi gerado o script
46
        'last_modify'     => null ,
47
48
        ########################## Ambiente/Arquivos
49
50
        // Nome do framework para o adapter
51
        'framework'       => null ,
52
        // namespace das classes
53
        'namespace'       => "" ,
54
        // caminho onde os arquivos devem ser criados
55
        'path'            => 'models' ,
56
        // flag para gerar pasta com o nome do driver do banco de dados
57
        'folder-database' => 0 ,
58
        // string com o nome da pastar personalizada
59
        'folder-name'     => '' ,
60
61
        ############################## Comandos adicionais
62
        //flag para mostrar o status da execução ao termino do processo
63
        'status'          => false ,
64
        // flags para criar todas as tabelas ou nao
65
        'tables'          => array () ,
66
        //Lista de tabelas a serem ignoradas
67
        'ignoreTable'     => array () ,
68
    );
69
70
    /**
71
     * @var string[] um array com todos os campos obrigatorios
72
     */
73
    protected $attRequered = array (
74
        'driver'   => true ,
75
        'database' => true ,
76
        'host'     => true ,
77
        'username' => true ,
78
        'path'     => true
79
    );
80
81
    protected $arrFunc = array ();
82
83
    private $framworkFiles = array ();
84
85
    public $reservedWord = array ();
86
87
    private static $dataTypesDefault = array (
88
        'int'      => 'int' ,
89
        'float'    => 'float' ,
90
        'string'   => 'string' ,
91
        'text'     => 'string' ,
92
        'date'     => 'date' ,
93
        'datetime' => 'date' ,
94
        'boolean'  => 'boolean'
95
    );
96
97
    protected $dataTypes = array ();
98
99
    const SEPARETOR = "";
100
101
    /**
102
     * verifica se todos valores obrigatorios tem valor
103
     *
104
     * @return bool
105
     */
106
    protected function checkConfig ()
107
    {
108
        if ( array_diff_key ( $this->attRequered , array_filter ( $this->arrConfig ) ) )
109
        {
110
            return false;
111
        }
112
113
        return true;
114
    }
115
116
    /**
117
     * retorna os parametros da configuração do framework
118
     *
119
     * @return array
120
     */
121
    abstract protected function getParams ();
122
123
    /**
124
     * Popula as config do generater com as configuraçoes do framework
125
     *
126
     * @return mixed
127
     */
128
    abstract protected function parseFrameworkConfig ();
129
130
    /**
131
     * Cria Instancias dos arquivos que devem ser gerados
132
     *
133
     * @return \Classes\AdapterMakerFile\AbstractAdapter[]
134
     */
135
    abstract public function getMakeFileInstances ();
136
137
    abstract protected function init ();
138
139
    /**
140
     * retorna a base do Namespace
141
     *
142
     * @return array
143
     */
144
    protected function getBaseNamespace ()
145
    {
146
        return array (
147
            $this->arrConfig[ 'namespace' ] ,
148
            'Model'
149
        );
150
    }
151
152
    /**
153
     * @param \Classes\Db\DbTable|\Classes\Db\Constrant $table
154
     *
155
     * @return mixed
156
     */
157
158
    public function createClassNamespace ( $table )
159
    {
160
        $arrNames = $this->getBaseNamespace ();
161
162
        if ( isset( $this->arrConfig[ 'folder-database' ] )
163
             && $this->arrConfig[ 'folder-database' ]
164
        )
165
        {
166
            $arrNames[] = AbstractMaker::getClassName ( $this->arrConfig[ 'driver' ] );
167
        }
168
169
        if ( isset( $this->arrConfig[ 'folder-name' ] )
170
             && $this->arrConfig[ 'folder-name' ]
171
        )
172
        {
173
            $arrNames[] = AbstractMaker::getClassName ( $this->arrConfig[ 'folder-name' ] );
174
        }
175
176
        if ( $table->hasSchema () )
177
        {
178
            $arrNames[] = $this->replaceReservedWord ( AbstractMaker::getClassName ( $table->getSchema () ) );
179
        } else
180
        {
181
            $arrNames[] = $this->replaceReservedWord ( AbstractMaker::getClassName ( $table->getDatabase () ) );
182
        }
183
184
        return implode ( static::SEPARETOR , array_filter ( $arrNames ) );
185
    }
186
187
    public function __construct ( $array )
188
    {
189
        $this->dataTypes = $this->dataTypes + self::$dataTypesDefault;
190
        $array += array (
191
            'version'     => Config::$version ,
192
            'author'      => ucfirst ( get_current_user () ) ,
193
            'last_modify' => date ( "d-m-Y H:i:s." )
194
        );
195
196
        $this->setFrameworkFiles ( $array );
197
        $this->parseFrameworkConfig ();
198
        $this->setParams ( $this->getParams () );
199
        $this->setParams ( $array );
200
        $this->init ();
201
        $this->validTableNames ();
202
        if ( ! $this->isValid () )
203
        {
204
            $var = array_diff_key ( $this->attRequered , array_filter ( $this->arrConfig ) );
205
            throw new Exception( $var );
206
        }
207
    }
208
209
    /**
210
     * Set os arquivos de configuracao do framework
211
     *
212
     * @param $array
213
     */
214
    public function setFrameworkFiles ( $array )
215
    {
216
        $this->framworkFiles[ 'library' ] = isset( $array[ 'framework-path-library' ] )
217
            ? $array[ 'framework-path-library' ] : null;
218
219
        $this->framworkFiles[ 'ini' ] = isset( $array[ 'framework-ini' ] )
220
            ? $array[ 'framework-ini' ] : null;
221
222
        $this->framworkFiles[ 'environment' ] = isset( $array[ 'environment' ] )
223
            ? $array[ 'environment' ] : 'production';
224
225
    }
226
227
    protected function isValidFrameworkFiles ()
228
    {
229
        if ( ! is_file ( $this->framworkFiles[ 'ini' ] ) )
230
        {
231
            return false;
232
        }
233
234
        if ( ! is_dir ( $this->framworkFiles[ 'library' ] ) )
235
        {
236
            return false;
237
        }
238
239
        if ( ! isset ( $this->framworkFiles[ 'environment' ] )
240
             or empty( $this->framworkFiles[ 'environment' ] )
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
241
        )
242
        {
243
            return false;
244
        }
245
        set_include_path (
246
            implode (
247
                PATH_SEPARATOR ,
248
                array (
249
                    realpath ( $this->framworkFiles[ 'library' ] ) ,
250
                    get_include_path () ,
251
                )
252
            )
253
        );
254
255
        return true;
256
    }
257
258
    protected function getFrameworkIni ()
259
    {
260
        return $this->framworkFiles[ 'ini' ];
261
    }
262
263
    protected function getEnvironment ()
264
    {
265
        return $this->framworkFiles[ 'environment' ];
266
    }
267
268
    /**
269
     * Popula as variaveis de acordo com o arquivo de configuração do seu  framework
270
     */
271
    protected function isValid ()
272
    {
273
        return $this->checkConfig ();
274
    }
275
276
    private function setParams ( $array )
277
    {
278
        if ( count ( $array ) > 0 )
279
        {
280
            $this->arrConfig = array_filter ( $array ) + $this->arrConfig;
281
        }
282
    }
283
284
    /**
285
     * @return string
286
     */
287
    public function getDatabase ()
288
    {
289
        return $this->arrConfig[ 'database' ];
290
    }
291
292
    /**
293
     * @return bool
294
     */
295
    public function hasSchemas ()
296
    {
297
        return ! empty ( $this->arrConfig[ 'schema' ] );
298
    }
299
300
    /**
301
     * @return string[]
302
     */
303
    public function getSchemas ()
304
    {
305
        if ( is_string ( $this->arrConfig[ 'schema' ] ) )
306
        {
307
            return array ( $this->arrConfig[ 'schema' ] );
308
        }
309
310
        return $this->arrConfig[ 'schema' ];
311
    }
312
313
    public function setSchema ( $schema )
314
    {
315
        $this->arrConfig[ 'schema' ] = $schema;
316
    }
317
318
    /**
319
     * @return string
320
     */
321
    public function getHost ()
322
    {
323
        return $this->arrConfig[ 'host' ];
324
    }
325
326
    /**
327
     * @return int
328
     */
329
    public function getPort ()
330
    {
331
        return $this->arrConfig[ 'port' ];
332
    }
333
334
    /**
335
     * @return boolean
336
     */
337
    public function hasPort ()
338
    {
339
        return ! empty( $this->arrConfig[ 'port' ] );
340
    }
341
342
    /**
343
     * @return string
344
     */
345
    public function getSocket ()
346
    {
347
        return $this->arrConfig[ 'socket' ];
348
    }
349
350
    /**
351
     * @return string
352
     */
353
    public function getUser ()
354
    {
355
        return $this->arrConfig[ 'username' ];
356
    }
357
358
    /**
359
     * @return string
360
     */
361
    public function getPassword ()
362
    {
363
        return $this->arrConfig[ 'password' ];
364
    }
365
366
    /**
367
     * @param string $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...
368
     *
369
     * @return string
370
     */
371
    public function replaceReservedWord ( $palavra )
372
    {
373
        if ( isset( $this->reservedWord[ strtolower ( $palavra ) ] ) )
374
        {
375
            return $this->reservedWord[ strtolower ( $palavra ) ];
376
        }
377
378
        return $palavra;
379
    }
380
381
    /**
382
     * @return bool
383
     */
384
    public function isStatusEnabled ()
385
    {
386
        return (bool) $this->arrConfig[ 'status' ];
387
    }
388
389
    public function validTableNames ()
390
    {
391
        $matches = preg_grep ( '*\.*' , $this->getTablesName () );
392
        if ( count ( $matches ) )
393
        {
394
           die("error: Table name must not contain the schema\n");
0 ignored issues
show
Coding Style Compatibility introduced by
The method validTableNames() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
395
        }
396
    }
397
398
    /**
399
     * @return bool
400
     */
401
    public function hasTablesName (){
402
        return ! empty( $this->arrConfig[ 'tables' ] );
403
    }
404
405
    /**
406
     * @return string[]
407
     */
408
    public function getTablesName ()
409
    {
410
        if ( is_string ( $this->arrConfig[ 'tables' ] ) )
411
        {
412
            return array ( $this->arrConfig[ 'tables' ] );
413
        }
414
415
        return $this->arrConfig[ 'tables' ];
416
    }
417
418
    /**
419
     * @return string
420
     */
421
    public function getListTablesName(){
422
        $str = implode("','", $this->getTablesName() );
423
        return "'$str'";
424
    }
425
426
    /**
427
     * @param $str
428
     *
429
     * @return string
430
     */
431
    public function __get ( $str )
432
    {
433
        $arr = array (
434
            'namespace' ,
435
            'framework' ,
436
            'author' ,
437
            'license' ,
438
            'version' ,
439
            'copyright' ,
440
            'link' ,
441
            'last_modify' ,
442
            'path' ,
443
            'folder-database' ,
444
            'folder-name'
445
        );
446
447
        if ( in_array ( $str , $arr ) )
448
        {
449
            return $this->arrConfig[ strtolower ( $str ) ];
450
        }
451
452
        return;
453
    }
454
455
    /**
456
     * @param string $type
457
     *
458
     * @return string
459
     */
460
    public static function convertTypeToPHP ( $type )
461
    {
462
        return self::$dataTypesDefault[ $type ];
463
    }
464
465
    /**
466
     * @param string $type
467
     *
468
     * @return string
469
     */
470
    public function convertTypeToTypeFramework ( $type )
471
    {
472
        return $this->dataTypes[ $type ];
473
    }
474
}
475