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