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.

Config::update()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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 31 and the first side effect is on line 16.

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;
4
5
use Classes\AdapterConfig\None;
6
use Classes\AdapterConfig\Phalcon;
7
use Classes\AdapterConfig\ZendFrameworkOne;
8
use Classes\AdapterMakerFile\AbstractAdapter;
9
use Classes\AdaptersDriver\Dblib;
10
use Classes\AdaptersDriver\Mssql;
11
use Classes\AdaptersDriver\Mysql;
12
use Classes\AdaptersDriver\Pgsql;
13
use Classes\AdaptersDriver\Sqlsrv;
14
use Classes\Update\Version;
15
16
require_once 'AdapterConfig/None.php';
17
require_once 'AdapterConfig/Phalcon.php';
18
require_once 'AdapterConfig/ZendFrameworkOne.php';
19
require_once 'AdaptersDriver/Dblib.php';
20
require_once 'AdaptersDriver/Mssql.php';
21
require_once 'AdaptersDriver/Mysql.php';
22
require_once 'AdaptersDriver/Pgsql.php';
23
require_once 'AdaptersDriver/Sqlsrv.php';
24
require_once 'Update/Version.php';
25
require_once 'Update.php';
26
27
/**
28
 * @author Pedro Alarcao <[email protected]>
29
 * @link   https://github.com/pedro151/orm-generator
30
 */
31
class Config
32
{
33
34
    /**
35
     * @var string
36
     */
37
    public static $version = "1.5.0";
38
39
    /**
40
     * String that separates the parent section name
41
     *
42
     * @var string
43
     */
44
    protected $sectionSeparator = ':';
45
46
    /**
47
     * @var string
48
     */
49
    private $configIniDefault = '/configs/config.ini';
50
51
    /**
52
     * @var string
53
     */
54
    public $_basePath;
55
    /**
56
     * @var array
57
     */
58
    private $argv = array ();
59
60
    /**
61
     * @var \Classes\AdapterConfig\AbstractAdapter
62
     */
63
    private $adapterConfig;
64
65
    /**
66
     * @var \Classes\AdaptersDriver\AbsractAdapter
67
     */
68
    private $adapterDriver;
69
70
    private $frameworkList = array (
71
        'none' ,
72
        'zf1' ,
73
        'phalcon'
74
    );
75
76
    private $parameterList = array (
77
        'init'             => 'Creates the necessary configuration file to start using the orm-generator.' ,
78
        'name-ini'         => 'reference to another .ini file configuration (relative path).' ,
79
        'config-env'       => 'orm-generator configuration environment.' ,
80
        'framework'        => 'name framework used, which has the contents of the database configurations and framework template.' ,
81
        'driver'           => 'database driver name (Ex.: pgsql).' ,
82
        'database'         => 'database name.' ,
83
        'schema'           => 'database schema name (one or more than one).' ,
84
        'tables'           => 'table name (parameter can be used more then once).' ,
85
        'optional-classes' => 'List of optional Predefined classes to be created (Consult in github the list of each framework).' ,
86
        'clean-trash'      => 'delete all files that do not belong to your Database due' ,
87
        'status'           => 'show status of implementation carried out after completing the process.' ,
88
        'version'          => 'shows the version of orm-generator.' ,
89
        'help'             => "help command explaining all the options and manner of use." ,
90
        'path'             => "specify where to create the files (default is current directory)." ,
91
        'update'           => "Upgrade to latest version." ,
92
        'download'         => "download the reported version (ex. --download=1.5.0). "
93
    );
94
95
    public function __construct ( $argv , $basePath , $numArgs )
96
    {
97
        if ( array_key_exists ( 'help' , $argv ) or ( $numArgs > 1
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...
98
                                                      && count ( $argv ) < 1 )
99
        )
100
        {
101
            die ( $this->getUsage () );
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() 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...
102
        }
103
        if ( array_key_exists ( 'version' , $argv ) )
104
        {
105
            die ( $this->getVersion () );
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() 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...
106
        }
107
        if ( array_key_exists ( 'status' , $argv ) )
108
        {
109
            $argv[ 'status' ] = true;
110
        }
111
        if ( array_key_exists ( 'update' , $argv ) )
112
        {
113
            die ( $this->update () );
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() 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...
114
        }
115
        if ( array_key_exists ( 'download' , $argv ) )
116
        {
117
            die ( $this->download ( $argv[ 'download' ] ) );
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() 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...
118
        }
119
120
        self::$version = Version::getVersion();
121
        $this->argv = $this->parseConfig ( $basePath , $argv );
122
    }
123
124
    /**
125
     * Lista de ajuda quando digita 'help'
126
     *
127
     * @return string
128
     */
129
    public function getUsage ()
130
    {
131
        $version = self::$version;
132
        $list = $this->renderParam ();
133
134
        return <<<EOF
135
parameters:
136
$list
137
 example: php generate.php --framework=zf1 --database=foo --tables=foobar --status
138
139
$version
140
EOF;
141
    }
142
143
    public function renderParam ()
144
    {
145
        $return = "";
146
        foreach ( $this->parameterList as $param => $desc )
147
        {
148
            if ( strlen ( $param ) < 5 )
149
            {
150
                $return .= "\t--" . $param . "\t\t: " . $desc . "\n";
151
            } else
152
            {
153
                $return .= "\t--" . $param . "\t: " . $desc . "\n";
154
            }
155
156
        }
157
158
        return $return;
159
    }
160
161
    public function update ()
162
    {
163
        $update = new Update();
164
        $update->update ()
165
               ->modifyTempName ();
166
    }
167
168
    public function download ( $version )
169
    {
170
        $update = new Update();
171
        $update->downloadVersion ( $version )
172
               ->modifyTempName ();
173
    }
174
175
    public function getVersion ()
176
    {
177
        $version = self::$version;
178
        return "ORM Generator \nVersion: {$version}\ncreated by: Pedro Alarcao <https://github.com/pedro151/orm-generator>\n{$version->messageHasNewVersion()}";
0 ignored issues
show
Bug introduced by
The method messageHasNewVersion cannot be called on $version (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
179
    }
180
181
    /**
182
     * Analisa e estrutura a Configuracao do generate
183
     *
184
     * @param  string $basePath
185
     * @param array   $argv
186
     *
187
     * @return array
188
     * @throws \Exception
189
     */
190
    private function parseConfig ( $basePath , $argv )
191
    {
192
        $this->_basePath = $basePath;
193
194
        $configIni = isset( $argv[ 'name-ini' ] )
195
            ? $argv[ 'name-ini' ]
196
            : $this->_basePath
197
              . $this->configIniDefault;
198
199
        $configTemp = $this->loadIniFile ( realpath ( $configIni ) );
200
        $configCurrent = self::parseConfigEnv ( $configTemp , $argv );
201
202
        if ( ! isset( $configCurrent[ 'framework' ] ) )
203
        {
204
            throw new \Exception( "configure which framework you want to use! \n" );
205
        }
206
207
        if ( ! in_array ( $configCurrent[ 'framework' ] , $this->frameworkList ) )
208
        {
209
            $frameworks = implode ( "\n\t" , $this->frameworkList );
210
            throw new \Exception( "list of frameworks: \n\t\033[1;33m" . $frameworks
211
                                  . "\n\033[0m" );
212
        }
213
214
        return $argv + array_filter ( $configCurrent );
215
    }
216
217
    /**
218
     *
219
     * @param $configTemp
220
     * @param $argv
221
     *
222
     * @return string
223
     */
224
    private static function parseConfigEnv ( $configTemp , $argv )
225
    {
226
        $thisSection = isset( $configTemp[ key ( $configTemp ) ][ 'config-env' ] )
227
            ? $configTemp[ key (
228
                $configTemp
229
            ) ][ 'config-env' ] : null;
230
231
        $thisSection = isset( $argv[ 'config-env' ] ) ? $argv[ 'config-env' ]
232
            : $thisSection;
233
234
        if ( isset( $configTemp[ $thisSection ][ 'extends' ] ) )
235
        {
236
            #faz marge da config principal com a config extendida
237
            return $configTemp[ $thisSection ]
238
                   + $configTemp[ $configTemp[ $thisSection ][ 'extends' ] ];
239
        }
240
241
        return $configTemp[ key ( $configTemp ) ];
242
    }
243
244
    /**
245
     * Carregar o arquivo ini e pré-processa o separador de seção ':'
246
     * no nome da seção (que é usado para a extensão seção) de modo a que a
247
     * matriz resultante tem os nomes de seção corretos e as informações de
248
     * extensão é armazenado em uma sub-ch ve
249
     *
250
     * @param string $filename
251
     *
252
     * @throws \Exception
253
     * @return array
254
     */
255
    protected function loadIniFile ( $filename )
256
    {
257
        if ( ! is_file ( $filename ) )
258
        {
259
            throw new \Exception( "\033[0;31mError: configuration file does not exist! \033[0m\n" );
260
        }
261
262
        $loaded = parse_ini_file ( $filename , true );
263
        $iniArray = array ();
264
        foreach ( $loaded as $key => $data )
265
        {
266
            $pieces = explode ( $this->sectionSeparator , $key );
267
            $thisSection = trim ( $pieces[ 0 ] );
268
            switch ( count ( $pieces ) )
269
            {
270
                case 1:
271
                    $iniArray[ $thisSection ] = $data;
272
                    break;
273
274
                case 2:
275
                    $extendedSection = trim ( $pieces[ 1 ] );
276
                    $iniArray[ $thisSection ] = array_merge ( array ( 'extends' => $extendedSection ) , $data );
277
                    break;
278
279
                default:
280
                    throw new \Exception( "Section '$thisSection' may not extend multiple sections in $filename" );
281
            }
282
        }
283
284
        return $iniArray;
285
    }
286
287
    /**
288
     * analisa a opção e cria a instancia do Atapter do determinado framework
289
     *
290
     * @return \Classes\AdapterConfig\AbstractAdapter
291
     *
292
     */
293
    private function factoryConfig ()
294
    {
295
        switch ( strtolower ( $this->argv[ 'framework' ] ) )
296
        {
297
            case 'zf1':
298
                return new ZendFrameworkOne( $this->argv );
299
            case 'phalcon':
300
                return new Phalcon( $this->argv );
301
            default:
302
                return new None( $this->argv );
303
        }
304
305
    }
306
307
    /**
308
     * Analisa a opção e instancia o determinado banco de dados
309
     *
310
     * @param AdapterConfig\AbstractAdapter $config
311
     *
312
     * @return AdaptersDriver\AbsractAdapter
313
     */
314
    private function factoryDriver ( AdapterConfig\AbstractAdapter $config )
315
    {
316
        switch ( $this->argv[ 'driver' ] )
317
        {
318
            case 'pgsql':
319
            case 'pdo_pgsql':
320
                return new Pgsql( $config );
321
            case 'mysql':
322
            case 'pdo_mysql':
323
                return new Mysql( $config );
324
            case 'mssql':
325
                return new Mssql( $config );
326
            case 'dblib':
327
                return new Dblib( $config );
328
            case 'sqlsrv':
329
                return new Sqlsrv( $config );
330
        }
331
    }
332
333
    /**
334
     * @return AdapterConfig\AbstractAdapter
335
     */
336
    public function getAdapterConfig ()
337
    {
338
        if ( ! $this->adapterConfig instanceof AbstractAdapter )
339
        {
340
            $this->adapterConfig = $this->factoryConfig ();
341
        }
342
343
        return $this->adapterConfig;
344
    }
345
346
    /**
347
     * @return AdaptersDriver\AbsractAdapter
348
     */
349
    public function getAdapterDriver ( AdapterConfig\AbstractAdapter $config )
350
    {
351
        if ( ! $this->adapterDriver )
352
        {
353
            $this->adapterDriver = $this->factoryDriver ( $config );
354
        }
355
356
        return $this->adapterDriver;
357
    }
358
359
}
360