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

AbstractAdapter::createClassNamespace()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 28
rs 8.439
cc 6
eloc 13
nc 8
nop 1
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 14 and the first side effect is on line 7.

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\Maker\AbstractMaker;
6
7
require_once "Classes/Maker/AbstractMaker.php";
8
require_once 'Exception.php';
9
10
/**
11
 * @author Pedro Alarcao <[email protected]>
12
 * @link   https://github.com/pedro151/orm-generator
13
 */
14
abstract class AbstractAdapter
15
{
16
17
    protected $arrConfig = array (
18
        ############################# DATABASE
19
        //Driver do banco de dados
20
        'driver'   => null ,
21
        //Nome do banco de dados
22
        'database' => null ,
23
        //Host do banco
24
        'host'     => 'localhost' ,
25
        //Port do banco
26
        'port'     => '' ,
27
        //usuario do banco
28
        'username' => null ,
29
        //senha do banco
30
        'password' => null ,
31
        // lista de schemas do banco de dados
32
        'schema'   => array () ,
33
34
        'socket'          => null ,
35
36
        ########################### DOCS
37
        // autor que gerou o script
38
        'author'          => "Pedro" ,
39
        'license'         => "New BSD License" ,
40
        'copyright'       => "ORM Generator - Pedro151" ,
41
        'link'            => 'https://github.com/pedro151/orm-generator' ,
42
        // data que foi gerado o script
43
        'last_modify'     => null ,
44
45
        ########################## Ambiente/Arquivos
46
47
        // Nome do framework para o adapter
48
        'framework'       => null ,
49
        // namespace das classes
50
        'namespace'       => "" ,
51
        // caminho onde os arquivos devem ser criados
52
        'path'            => 'models' ,
53
        // flag para gerar pasta com o nome do driver do banco de dados
54
        'folder-database' => 0 ,
55
56
        ############################## Comandos adicionais
57
        //flag para mostrar o status da execução ao termino do processo
58
        'status'          => false ,
59
        // flags para criar todas as tabelas ou nao
60
        'allTables'       => true ,
61
        //Lista de tabelas a serem ignoradas
62
        'ignoreTable'     => array () ,
63
    );
64
65
    /**
66
     * @var string[] um array com todos os campos obrigatorios
67
     */
68
    protected $attRequered = array (
69
        'driver'   => true ,
70
        'database' => true ,
71
        'host'     => true ,
72
        'username' => true ,
73
        'path'     => true
74
    );
75
76
    protected $arrFunc = array ();
77
78
    private $framworkFiles = array ();
79
80
    const SEPARETOR = "";
81
82
    /**
83
     * verifica se todos valores obrigatorios tem valor
84
     *
85
     * @return bool
86
     */
87
    protected function checkConfig ()
88
    {
89
        if ( array_diff_key ( $this->attRequered , array_filter ( $this->arrConfig ) ) )
90
        {
91
            return false;
92
        }
93
94
        return true;
95
    }
96
97
    /**
98
     * retorna os parametros da configuração do framework
99
     *
100
     * @return array
101
     */
102
    abstract protected function getParams ();
103
104
    /**
105
     * Popula as config do generater com as configuraçoes do framework
106
     *
107
     * @return mixed
108
     */
109
    abstract protected function parseFrameworkConfig ();
110
111
    /**
112
     * Cria Instancias dos arquivos que devem ser gerados
113
     *
114
     * @return \Classes\AdapterMakerFile\AbstractAdapter[]
115
     */
116
    abstract public function getMakeFileInstances ();
117
118
    abstract protected function init ();
119
120
    /**
121
     * retorna a base do Namespace
122
     *
123
     * @return array
124
     */
125
    protected function getBaseNamespace ()
126
    {
127
        return array (
128
            $this->arrConfig[ 'namespace' ] ,
129
            'Model'
130
        );
131
    }
132
133
    /**
134
     * @param \Classes\Db\DbTable|\Classes\Db\Constrant $table
135
     *
136
     * @return mixed
137
     */
138
139
    public function createClassNamespace ( $table )
140
    {
141
        $arrNames = $this->getBaseNamespace ();
142
143
        if ( isset( $this->arrConfig[ 'folder-database' ] )
144
             && $this->arrConfig[ 'folder-database' ]
145
        )
146
        {
147
            $arrNames[] = AbstractMaker::getClassName ( $this->arrConfig[ 'driver' ] );
148
        }
149
150
        if ( isset( $this->arrConfig[ 'folder-name' ] )
151
             && $this->arrConfig[ 'folder-name' ]
152
        )
153
        {
154
            $arrNames[] = AbstractMaker::getClassName ( $this->arrConfig[ 'folder-name' ] );
155
        }
156
157
        if ( $table->hasSchema () )
158
        {
159
            $arrNames[] = AbstractMaker::getClassName ( $table->getSchema () );
160
        } else
161
        {
162
            $arrNames[] = AbstractMaker::getClassName ( $table->getDatabase () );
163
        }
164
165
        return implode ( static::SEPARETOR , array_filter ( $arrNames ) );
166
    }
167
168
    public function __construct ( $array )
169
    {
170
        $array += array (
171
            'author'      => ucfirst ( get_current_user () ) ,
172
            'last_modify' => date ( "d-m-Y H:i:s." )
173
        );
174
175
        $this->setFrameworkFiles ( $array );
176
        $this->parseFrameworkConfig ();
177
        $this->setParams ( $this->getParams () );
178
        $this->setParams ( $array );
179
        $this->init ();
180
        if ( ! $this->isValid () )
181
        {
182
            $var = array_diff_key ( $this->attRequered , array_filter ( $this->arrConfig ) );
183
            throw new Exception( $var );
184
        }
185
    }
186
187
    /**
188
     * Set os arquivos de configuracao do framework
189
     *
190
     * @param $array
191
     */
192
    public function setFrameworkFiles ( $array )
193
    {
194
        $this->framworkFiles[ 'library' ] = isset( $array[ 'framework-path-library' ] )
195
            ? $array[ 'framework-path-library' ]
196
            : null;
197
198
        $this->framworkFiles[ 'ini' ] = isset( $array[ 'framework-ini' ] )
199
            ? $array[ 'framework-ini' ]
200
            : null;
201
202
        $this->framworkFiles[ 'environment' ] = isset( $array[ 'environment' ] )
203
            ? $array[ 'environment' ]
204
            : 'production';
205
206
    }
207
208
    protected function isValidFrameworkFiles ()
209
    {
210
        if ( ! is_file ( $this->framworkFiles[ 'ini' ] ) )
211
        {
212
            return false;
213
        }
214
215
        if ( ! is_dir ( $this->framworkFiles[ 'library' ] ) )
216
        {
217
            return false;
218
        }
219
220
221
        if ( ! isset ( $this->framworkFiles[ 'environment' ] )
222
             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...
223
        )
224
        {
225
            return false;
226
        }
227
        set_include_path (
228
            implode (
229
                PATH_SEPARATOR ,
230
                array (
231
                    realpath ( $this->framworkFiles[ 'library' ] ) ,
232
                    get_include_path () ,
233
                )
234
            )
235
        );
236
237
        return true;
238
    }
239
240
    protected function getFrameworkIni ()
241
    {
242
        return $this->framworkFiles[ 'ini' ];
243
    }
244
245
    protected function getEnvironment ()
246
    {
247
        return $this->framworkFiles[ 'environment' ];
248
    }
249
250
    /**
251
     * Popula as variaveis de acordo com o arquivo de configuração do seu  framework
252
     */
253
    protected function isValid ()
254
    {
255
        return $this->checkConfig ();
256
    }
257
258
    private function setParams ( $array )
259
    {
260
        if ( count ( $array ) > 0 )
261
        {
262
            $this->arrConfig = array_filter ( $array ) + $this->arrConfig;
263
        }
264
    }
265
266
    /**
267
     * @return string
268
     */
269
    public function getDatabase ()
270
    {
271
        return $this->arrConfig[ 'database' ];
272
    }
273
274
    /**
275
     * @return bool
276
     */
277
    public function hasSchemas ()
278
    {
279
        return ! empty ( $this->arrConfig[ 'schema' ] );
280
    }
281
282
    /**
283
     * @return string[]
284
     */
285
    public function getSchemas ()
286
    {
287
        if ( is_string ( $this->arrConfig[ 'schema' ] ) )
288
        {
289
            return array ( $this->arrConfig[ 'schema' ] );
290
        }
291
292
        return $this->arrConfig[ 'schema' ];
293
    }
294
295
    public function setSchema ( $schema )
296
    {
297
        $this->arrConfig[ 'schema' ] = $schema;
298
    }
299
300
    /**
301
     * @return string
302
     */
303
    public function getHost ()
304
    {
305
        return $this->arrConfig[ 'host' ];
306
    }
307
308
    /**
309
     * @return int
310
     */
311
    public function getPort ()
312
    {
313
        return $this->arrConfig[ 'port' ];
314
    }
315
316
    /**
317
     * @return boolean
318
     */
319
    public function hasPort ()
320
    {
321
        return ! empty( $this->arrConfig[ 'port' ] );
322
    }
323
324
    /**
325
     * @return string
326
     */
327
    public function getSocket ()
328
    {
329
        return $this->arrConfig[ 'socket' ];
330
    }
331
332
    /**
333
     * @return string
334
     */
335
    public function getUser ()
336
    {
337
        return $this->arrConfig[ 'username' ];
338
    }
339
340
    /**
341
     * @return string
342
     */
343
    public function getPassword ()
344
    {
345
        return $this->arrConfig[ 'password' ];
346
    }
347
348
    /**
349
     * @return bool
350
     */
351
    public function isStatusEnabled ()
352
    {
353
        return (bool) $this->arrConfig[ 'status' ];
354
    }
355
356
    /**
357
     * @param $str
358
     *
359
     * @return string
360
     */
361
    public function __get ( $str )
362
    {
363
        $arr = array (
364
            'namespace' ,
365
            'framework' ,
366
            'author' ,
367
            'license' ,
368
            'copyright' ,
369
            'link' ,
370
            'last_modify' ,
371
            'path' ,
372
            'folder-database',
373
            'folder-name'
374
        );
375
376
        if ( in_array ( $str , $arr ) )
377
        {
378
            return $this->arrConfig[ strtolower ( $str ) ];
379
        }
380
381
        return;
382
    }
383
384
}
385