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
Push — master ( bf1ef2...cd89f1 )
by Pedro
10:51 queued 06:31
created

AbstractAdapter::getTablesName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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
        'clean-trash'     => false,
62
63
        ############################## Comandos adicionais
64
        //flag para mostrar o status da execução ao termino do processo
65
        'status'          => false ,
66
        // flags para criar todas as tabelas ou nao
67
        'tables'          => array () ,
68
        // lista de Classes opcionais pre-definidas para serem criadas
69
        'optional-classes'=> array (),
70
        //Lista de tabelas a serem ignoradas
71
        'ignoreTable'     => array () ,
72
    );
73
74
    /**
75
     * @var string[] um array com todos os campos obrigatorios
76
     */
77
    protected $attRequered = array (
78
        'driver'   => true ,
79
        'database' => true ,
80
        'host'     => true ,
81
        'username' => true ,
82
        'path'     => true
83
    );
84
85
    protected $arrFunc = array ();
86
87
    private $framworkFiles = array ();
88
89
    public $reservedWord = array ();
90
91
    private static $dataTypesDefault = array (
92
        'int'       => 'int' ,
93
        'float'     => 'float' ,
94
        'string'    => 'string' ,
95
        'text'      => 'string' ,
96
        'date'      => 'date' ,
97
        'datetime'  => 'datetime' ,
98
        'timestamp' => 'timestamp' ,
99
        'boolean'   => 'boolean'
100
    );
101
102
    protected $dataTypes = array ();
103
104
    const SEPARETOR = "";
105
106
    /**
107
     * verifica se todos valores obrigatorios tem valor
108
     *
109
     * @return bool
110
     */
111
    protected function checkConfig ()
112
    {
113
        if ( array_diff_key ( $this->attRequered , array_filter ( $this->arrConfig ) ) )
114
        {
115
            return false;
116
        }
117
118
        return true;
119
    }
120
121
    /**
122
     * retorna os parametros da configuração do framework
123
     *
124
     * @return array
125
     */
126
    abstract protected function getParams ();
127
128
    /**
129
     * Popula as config do generater com as configuraçoes do framework
130
     *
131
     * @return mixed
132
     */
133
    abstract protected function parseFrameworkConfig ();
134
135
    /**
136
     * Cria Instancias dos arquivos que devem ser gerados
137
     *
138
     * @return \Classes\AdapterMakerFile\AbstractAdapter[]
139
     */
140
    abstract public function getMakeFileInstances ();
141
142
    abstract protected function init ();
143
144
    /**
145
     * retorna a base do Namespace
146
     *
147
     * @return array
148
     */
149
    protected function getBaseNamespace ()
150
    {
151
        return array (
152
            $this->arrConfig[ 'namespace' ] ,
153
            'Model'
154
        );
155
    }
156
157
    /**
158
     * @param \Classes\Db\DbTable|\Classes\Db\Constrant $table
159
     *
160
     * @return mixed
161
     */
162
163
    public function createClassNamespace ( $table )
164
    {
165
        $arrNames = $this->getBaseNamespace ();
166
167
        if ( isset( $this->arrConfig[ 'folder-database' ] )
168
             && $this->arrConfig[ 'folder-database' ]
169
        )
170
        {
171
            $arrNames[] = AbstractMaker::getClassName ( $this->arrConfig[ 'driver' ] );
172
        }
173
174
        if ( isset( $this->arrConfig[ 'folder-name' ] )
175
             && $this->arrConfig[ 'folder-name' ]
176
        )
177
        {
178
            $arrNames[] = AbstractMaker::getClassName ( $this->arrConfig[ 'folder-name' ] );
179
        }
180
181
        if ( $table->hasSchema () )
182
        {
183
            $arrNames[] = $this->replaceReservedWord ( AbstractMaker::getClassName ( $table->getSchema () ) );
184
        } else
185
        {
186
            $arrNames[] = $this->replaceReservedWord ( AbstractMaker::getClassName ( $table->getDatabase () ) );
187
        }
188
189
        return implode ( static::SEPARETOR , array_filter ( $arrNames ) );
190
    }
191
192
    public function __construct ( $array )
193
    {
194
        $this->dataTypes = $this->dataTypes + self::$dataTypesDefault;
195
        $array += array (
196
            'version'     => Config::$version ,
197
            'author'      => ucfirst ( get_current_user () ) ,
198
            'last_modify' => date ( "d-m-Y" )
199
        );
200
201
        $this->setFrameworkFiles ( $array );
202
        $this->parseFrameworkConfig ();
203
        $this->setParams ( $this->getParams () );
204
        $this->setParams ( $array );
205
        $this->init ();
206
        $this->validTableNames ();
207
        if ( ! $this->isValid () )
208
        {
209
            $var = array_diff_key ( $this->attRequered , array_filter ( $this->arrConfig ) );
210
            throw new Exception( $var );
211
        }
212
    }
213
214
    /**
215
     * Set os arquivos de configuracao do framework
216
     *
217
     * @param $array
218
     */
219
    public function setFrameworkFiles ( $array )
220
    {
221
        $this->framworkFiles[ 'library' ] = isset( $array[ 'framework-path-library' ] )
222
            ? $array[ 'framework-path-library' ] : null;
223
224
        $this->framworkFiles[ 'ini' ] = isset( $array[ 'framework-ini' ] )
225
            ? $array[ 'framework-ini' ] : null;
226
227
        $this->framworkFiles[ 'environment' ] = isset( $array[ 'environment' ] )
228
            ? $array[ 'environment' ] : 'production';
229
230
    }
231
232
    protected function isValidFrameworkFiles ()
233
    {
234
        if ( ! is_file ( $this->framworkFiles[ 'ini' ] ) )
235
        {
236
            return false;
237
        }
238
239
        if ( ! is_dir ( $this->framworkFiles[ 'library' ] ) )
240
        {
241
            return false;
242
        }
243
244
        if ( ! isset ( $this->framworkFiles[ 'environment' ] )
245
             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...
246
        )
247
        {
248
            return false;
249
        }
250
        set_include_path (
251
            implode (
252
                PATH_SEPARATOR ,
253
                array (
254
                    realpath ( $this->framworkFiles[ 'library' ] ) ,
255
                    get_include_path () ,
256
                )
257
            )
258
        );
259
260
        return true;
261
    }
262
263
    protected function getFrameworkIni ()
264
    {
265
        return $this->framworkFiles[ 'ini' ];
266
    }
267
268
    protected function getEnvironment ()
269
    {
270
        return $this->framworkFiles[ 'environment' ];
271
    }
272
273
    /**
274
     * Popula as variaveis de acordo com o arquivo de configuração do seu  framework
275
     */
276
    protected function isValid ()
277
    {
278
        return $this->checkConfig ();
279
    }
280
281
    private function setParams ( $array )
282
    {
283
        if ( count ( $array ) > 0 )
284
        {
285
            $this->arrConfig = array_filter ( $array ) + $this->arrConfig;
286
        }
287
    }
288
289
    /**
290
     * @return string
291
     */
292
    public function getDatabase ()
293
    {
294
        return $this->arrConfig[ 'database' ];
295
    }
296
297
    /**
298
     * @return bool
299
     */
300
    public function hasSchemas ()
301
    {
302
        return ! empty ( $this->arrConfig[ 'schema' ] );
303
    }
304
305
    /**
306
     * @return string[]
307
     */
308
    public function getSchemas ()
309
    {
310
        if ( is_string ( $this->arrConfig[ 'schema' ] ) )
311
        {
312
            return array ( $this->arrConfig[ 'schema' ] );
313
        }
314
315
        return $this->arrConfig[ 'schema' ];
316
    }
317
318
    public function setSchema ( $schema )
319
    {
320
        $this->arrConfig[ 'schema' ] = $schema;
321
    }
322
323
    /**
324
     * @return string
325
     */
326
    public function getHost ()
327
    {
328
        return $this->arrConfig[ 'host' ];
329
    }
330
331
    /**
332
     * @return int
333
     */
334
    public function getPort ()
335
    {
336
        return $this->arrConfig[ 'port' ];
337
    }
338
339
    /**
340
     * @return boolean
341
     */
342
    public function hasPort ()
343
    {
344
        return ! empty( $this->arrConfig[ 'port' ] );
345
    }
346
347
    /**
348
     * @return boolean
349
     */
350
    public function isCleanTrash(){
351
        return (boolean) $this->arrConfig[ 'clean-trash' ];
352
    }
353
354
    /**
355
     * @return string
356
     */
357
    public function getSocket ()
358
    {
359
        return $this->arrConfig[ 'socket' ];
360
    }
361
362
    /**
363
     * @return string
364
     */
365
    public function getUser ()
366
    {
367
        return $this->arrConfig[ 'username' ];
368
    }
369
370
    /**
371
     * @return string
372
     */
373
    public function getPassword ()
374
    {
375
        return $this->arrConfig[ 'password' ];
376
    }
377
378
    /**
379
     * @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...
380
     *
381
     * @return string
382
     */
383
    public function replaceReservedWord ( $palavra )
384
    {
385
        if ( isset( $this->reservedWord[ strtolower ( $palavra ) ] ) )
386
        {
387
            return $this->reservedWord[ strtolower ( $palavra ) ];
388
        }
389
390
        return $palavra;
391
    }
392
393
    /**
394
     * @return bool
395
     */
396
    public function isStatusEnabled ()
397
    {
398
        return (bool) $this->arrConfig[ 'status' ];
399
    }
400
401
    public function validTableNames ()
402
    {
403
        $matches = preg_grep ( '*\.*' , $this->getTablesName () );
404
        if ( count ( $matches ) )
405
        {
406
           die("\033[0;31mError: Table name must not contain the schema.\033[0m\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...
407
        }
408
    }
409
410
    /**
411
     * @return bool
412
     */
413
    public function hasTablesName (){
414
        return ! empty( $this->arrConfig[ 'tables' ] );
415
    }
416
417
    /**
418
     * @return string[]
419
     */
420
    public function getTablesName ()
421
    {
422
        if ( is_string ( $this->arrConfig[ 'tables' ] ) )
423
        {
424
            return array ( $this->arrConfig[ 'tables' ] );
425
        }
426
427
        return $this->arrConfig[ 'tables' ];
428
    }
429
430
    /**
431
     * @return string
432
     */
433
    public function getListTablesName(){
434
        $str = implode("','", $this->getTablesName() );
435
        return "'$str'";
436
    }
437
438
    /**
439
     * @return bool
440
     */
441
    public function hasOptionalClasses (){
442
        return ! empty( $this->arrConfig[ 'optional-classes' ] );
443
    }
444
445
    /**
446
     * @return string[]
447
     */
448
    public function getOptionalClasses ()
449
    {
450
        if ( is_string ( $this->arrConfig[ 'optional-classes' ] ) )
451
        {
452
            return array ( $this->arrConfig[ 'optional-classes' ] );
453
        }
454
455
        return $this->arrConfig[ 'optional-classes' ];
456
    }
457
458
    /**
459
     * @param $str
460
     *
461
     * @return string
462
     */
463
    public function __get ( $str )
464
    {
465
        $arr = array (
466
            'namespace' ,
467
            'framework' ,
468
            'author' ,
469
            'license' ,
470
            'version' ,
471
            'copyright' ,
472
            'link' ,
473
            'last_modify' ,
474
            'path' ,
475
            'folder-database' ,
476
            'folder-name'
477
        );
478
479
        if ( in_array ( $str , $arr ) )
480
        {
481
            return $this->arrConfig[ strtolower ( $str ) ];
482
        }
483
484
        return;
485
    }
486
487
    /**
488
     * @param string $type
489
     *
490
     * @return string
491
     */
492
    public static function convertTypeToPHP ( $type )
493
    {
494
        return self::$dataTypesDefault[ $type ];
495
    }
496
497
    /**
498
     * @param string $type
499
     *
500
     * @return string
501
     */
502
    public function convertTypeToTypeFramework ( $type )
503
    {
504
        return $this->dataTypes[ $type ];
505
    }
506
}
507