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

Config::checkHasNewVersion()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 13
nc 5
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Config::update() 0 6 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 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
        'clean-trash' => 'delete all files that do not belong to your Database due' ,
86
        'status'      => 'show status of implementation carried out after completing the process.' ,
87
        'version'     => 'shows the version of orm-generator.' ,
88
        'help'        => "help command explaining all the options and manner of use." ,
89
        'path'        => "specify where to create the files (default is current directory)." ,
90
        'update'      => ".",
91
        'download'    => ""
92
    );
93
94
    public function __construct ( $argv , $basePath , $numArgs )
95
    {
96
        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...
97
                                                      && count ( $argv ) < 1 )
98
        )
99
        {
100
            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...
101
        }
102
        if ( array_key_exists ( 'version' , $argv ) )
103
        {
104
            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...
105
        }
106
        if ( array_key_exists ( 'status' , $argv ) )
107
        {
108
            $argv[ 'status' ] = true;
109
        }
110
        if ( array_key_exists ( 'update' , $argv ) )
111
        {
112
            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...
113
        }
114
        if ( array_key_exists ( 'download' , $argv ) )
115
        {
116
            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...
117
        }
118
119
        $this->argv = $this->parseConfig ( $basePath , $argv );
120
    }
121
122
    /**
123
     * Lista de ajuda quando digita 'help'
124
     *
125
     * @return string
126
     */
127
    public function getUsage ()
128
    {
129
        $version = $this->getVersion ();
130
        $list = $this->renderParam ();
131
132
        return <<<EOF
133
parameters:
134
$list
135
 example: php generate.php --framework=zf1 --database=foo --tables=foobar --status
136
137
$version
138
EOF;
139
    }
140
141
    public function renderParam ()
142
    {
143
        $return = "";
144
        foreach ( $this->parameterList as $param => $desc )
145
        {
146
            if ( strlen ( $param ) < 5 )
147
            {
148
                $return .= "\t--" . $param . "\t\t: " . $desc . "\n";
149
            } else
150
            {
151
                $return .= "\t--" . $param . "\t: " . $desc . "\n";
152
            }
153
154
        }
155
156
        return $return;
157
    }
158
159
    public function update ()
160
    {
161
        $update = new Update();
162
        $update->update ()
163
               ->modifyTempName ();
164
    }
165
166
    public function download ( $version )
167
    {
168
        $update = new Update();
169
        $update->downloadVersion ( $version )
170
               ->modifyTempName ();
171
    }
172
173
    public function getVersion ()
174
    {
175
        $version = new Version();
176
177
        return "ORM Generator \nVersion: {$version->getVersion()}\ncreated by: Pedro Alarcao <https://github.com/pedro151/orm-generator>\n{$version->messageHasNewVersion()}";
178
    }
179
180
    /**
181
     * Analisa e estrutura a Configuracao do generate
182
     *
183
     * @param  string $basePath
184
     * @param array   $argv
185
     *
186
     * @return array
187
     * @throws \Exception
188
     */
189
    private function parseConfig ( $basePath , $argv )
190
    {
191
        $this->_basePath = $basePath;
192
193
        $configIni = isset( $argv[ 'name-ini' ] )
194
            ? $argv[ 'name-ini' ]
195
            : $this->_basePath
196
              . $this->configIniDefault;
197
198
        $configTemp = $this->loadIniFile ( realpath ( $configIni ) );
199
        $configCurrent = self::parseConfigEnv ( $configTemp , $argv );
200
201
        if ( ! isset( $configCurrent[ 'framework' ] ) )
202
        {
203
            throw new \Exception( "configure which framework you want to use! \n" );
204
        }
205
206
        if ( ! in_array ( $configCurrent[ 'framework' ] , $this->frameworkList ) )
207
        {
208
            $frameworks = implode ( "\n\t" , $this->frameworkList );
209
            throw new \Exception( "list of frameworks: \n\t\033[1;33m" . $frameworks
210
                                  . "\n\033[0m" );
211
        }
212
213
        return $argv + array_filter ( $configCurrent );
214
    }
215
216
    /**
217
     *
218
     * @param $configTemp
219
     * @param $argv
220
     *
221
     * @return string
222
     */
223
    private static function parseConfigEnv ( $configTemp , $argv )
224
    {
225
        $thisSection = isset( $configTemp[ key ( $configTemp ) ][ 'config-env' ] )
226
            ? $configTemp[ key (
227
                $configTemp
228
            ) ][ 'config-env' ] : null;
229
230
        $thisSection = isset( $argv[ 'config-env' ] ) ? $argv[ 'config-env' ]
231
            : $thisSection;
232
233
        if ( isset( $configTemp[ $thisSection ][ 'extends' ] ) )
234
        {
235
            #faz marge da config principal com a config extendida
236
            return $configTemp[ $thisSection ]
237
                   + $configTemp[ $configTemp[ $thisSection ][ 'extends' ] ];
238
        }
239
240
        return $configTemp[ key ( $configTemp ) ];
241
    }
242
243
    /**
244
     * Carregar o arquivo ini e pré-processa o separador de seção ':'
245
     * no nome da seção (que é usado para a extensão seção) de modo a que a
246
     * matriz resultante tem os nomes de seção corretos e as informações de
247
     * extensão é armazenado em uma sub-ch ve
248
     *
249
     * @param string $filename
250
     *
251
     * @throws \Exception
252
     * @return array
253
     */
254
    protected function loadIniFile ( $filename )
255
    {
256
        if ( ! is_file ( $filename ) )
257
        {
258
            throw new \Exception( "\033[0;31mError: configuration file does not exist! \033[0m\n" );
259
        }
260
261
        $loaded = parse_ini_file ( $filename , true );
262
        $iniArray = array ();
263
        foreach ( $loaded as $key => $data )
264
        {
265
            $pieces = explode ( $this->sectionSeparator , $key );
266
            $thisSection = trim ( $pieces[ 0 ] );
267
            switch ( count ( $pieces ) )
268
            {
269
                case 1:
270
                    $iniArray[ $thisSection ] = $data;
271
                    break;
272
273
                case 2:
274
                    $extendedSection = trim ( $pieces[ 1 ] );
275
                    $iniArray[ $thisSection ] = array_merge ( array ( 'extends' => $extendedSection ) , $data );
276
                    break;
277
278
                default:
279
                    throw new \Exception( "Section '$thisSection' may not extend multiple sections in $filename" );
280
            }
281
        }
282
283
        return $iniArray;
284
    }
285
286
    /**
287
     * analisa a opção e cria a instancia do Atapter do determinado framework
288
     *
289
     * @return \Classes\AdapterConfig\AbstractAdapter
290
     *
291
     */
292
    private function factoryConfig ()
293
    {
294
        switch ( strtolower ( $this->argv[ 'framework' ] ) )
295
        {
296
            case 'zf1':
297
                return new ZendFrameworkOne( $this->argv );
298
            case 'phalcon':
299
                return new Phalcon( $this->argv );
300
            default:
301
                return new None( $this->argv );
302
        }
303
304
    }
305
306
    /**
307
     * Analisa a opção e instancia o determinado banco de dados
308
     *
309
     * @param AdapterConfig\AbstractAdapter $config
310
     *
311
     * @return AdaptersDriver\AbsractAdapter
312
     */
313
    private function factoryDriver ( AdapterConfig\AbstractAdapter $config )
314
    {
315
        switch ( $this->argv[ 'driver' ] )
316
        {
317
            case 'pgsql':
318
            case 'pdo_pgsql':
319
                return new Pgsql( $config );
320
            case 'mysql':
321
            case 'pdo_mysql':
322
                return new Mysql( $config );
323
            case 'mssql':
324
                return new Mssql( $config );
325
            case 'dblib':
326
                return new Dblib( $config );
327
            case 'sqlsrv':
328
                return new Sqlsrv( $config );
329
        }
330
    }
331
332
    /**
333
     * @return AdapterConfig\AbstractAdapter
334
     */
335
    public function getAdapterConfig ()
336
    {
337
        if ( ! $this->adapterConfig instanceof AbstractAdapter )
338
        {
339
            $this->adapterConfig = $this->factoryConfig ();
340
        }
341
342
        return $this->adapterConfig;
343
    }
344
345
    /**
346
     * @return AdaptersDriver\AbsractAdapter
347
     */
348
    public function getAdapterDriver ( AdapterConfig\AbstractAdapter $config )
349
    {
350
        if ( ! $this->adapterDriver )
351
        {
352
            $this->adapterDriver = $this->factoryDriver ( $config );
353
        }
354
355
        return $this->adapterDriver;
356
    }
357
358
}
359