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 ( d395a9...487bee )
by Pedro
04:51 queued 02:06
created

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