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 (#13)
by Pedro
05:20
created

Config::renderParam()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
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 28 and the first side effect is on line 15.

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
15
require_once 'AdapterConfig/None.php';
16
require_once 'AdapterConfig/Phalcon.php';
17
require_once 'AdapterConfig/ZendFrameworkOne.php';
18
require_once 'AdaptersDriver/Dblib.php';
19
require_once 'AdaptersDriver/Mssql.php';
20
require_once 'AdaptersDriver/Mysql.php';
21
require_once 'AdaptersDriver/Pgsql.php';
22
require_once 'AdaptersDriver/Sqlsrv.php';
23
24
/**
25
 * @author Pedro Alarcao <[email protected]>
26
 * @link   https://github.com/pedro151/orm-generator
27
 */
28
class Config
29
{
30
31
    /**
32
     * @var string
33
     */
34
    public static $version = "1.4.4";
35
36
    /**
37
     * String that separates the parent section name
38
     *
39
     * @var string
40
     */
41
    protected $sectionSeparator = ':';
42
43
    /**
44
     * @var string
45
     */
46
    private $configIniDefault = '/configs/config.ini';
47
48
    /**
49
     * @var string
50
     */
51
    public $_basePath;
52
    /**
53
     * @var array
54
     */
55
    private $argv = array ();
56
57
    /**
58
     * @var \Classes\AdapterConfig\AbstractAdapter
59
     */
60
    private $adapterConfig;
61
62
    /**
63
     * @var \Classes\AdaptersDriver\AbsractAdapter
64
     */
65
    private $adapterDriver;
66
67
    private $frameworkList = array (
68
        'none',
69
        'zf1',
70
        'phalcon'
71
    );
72
73
    private $parameterList = array (
74
        'init'       => 'Creates the necessary configuration file to start using the orm-generator.',
75
        'config-ini' => 'reference to another .ini file configuration (relative path).',
76
        'config-env' => 'orm-generator configuration environment.',
77
        'framework'  => 'name framework used, which has the contents of the database configurations and framework template.',
78
        'driver'     => 'database driver name (Ex.: pgsql).',
79
        'database'   => 'database name.',
80
        'schema'     => 'database schema name (one or more than one).',
81
        'tables'     => 'table name (parameter can be used more then once).',
82
        'status'     => 'show status of implementation carried out after completing the process.',
83
        'version'    => 'shows the version of orm-generator.',
84
        'help'       => "help command explaining all the options and manner of use.",
85
        'path'       => "specify where to create the files (default is current directory).",
86
    );
87
88
    public function __construct ( $argv, $basePath, $numArgs )
89
    {
90
        if ( array_key_exists ( 'help', $argv ) or ( $numArgs > 1 && count ( $argv ) < 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...
91
            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...
92
        }
93
        if ( array_key_exists ( 'version', $argv ) ) {
94
            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...
95
        }
96
        if ( array_key_exists ( 'status', $argv ) ) {
97
            $argv[ 'status' ] = true;
98
        }
99
100
        $this->argv = $this->parseConfig ( $basePath, $argv );
101
    }
102
103
    /**
104
     * Lista de ajuda quando digita 'help'
105
     *
106
     * @return string
107
     */
108
    public function getUsage ()
109
    {
110
        $version = $this->getVersion ();
111
        $list    = $this->renderParam ();
112
113
        return <<<EOF
114
parameters:
115
$list
116
 example: php generate.php --framework=zf1 --database=foo --table=foobar --status
117
118
$version
119
EOF;
120
    }
121
122
    public function renderParam ()
123
    {
124
        $return = "";
125
        foreach ( $this->parameterList as $param => $desc ) {
126
            if ( strlen ( $param ) < 5 ) {
127
                $return .= "\t--" . $param . "\t\t: " . $desc . "\n";
128
            }
129
            else {
130
                $return .= "\t--" . $param . "\t: " . $desc . "\n";
131
            }
132
133
        }
134
135
        return $return;
136
    }
137
138
    public function getVersion ()
139
    {
140
        $version = static::$version;
141
142
        return "ORM Generator By: Pedro Alarcao Version: $version\n";
143
    }
144
145
    /**
146
     * Analisa e estrutura a Configuracao do generate
147
     *
148
     * @param  string $basePath
149
     * @param array   $argv
150
     *
151
     * @return array
152
     * @throws \Exception
153
     */
154
    private function parseConfig ( $basePath, $argv )
155
    {
156
        $this->_basePath =  $basePath;
157
158
        $configIni = isset( $argv[ 'config-ini' ] )
159
            ? $argv[ 'config-ini' ] : $this->_basePath
160
                                      . $this->configIniDefault;
161
162
        $configTemp    = $this->loadIniFile ( realpath ( $configIni ) );
163
        $configCurrent = self::parseConfigEnv ( $configTemp, $argv );
164
165
        if ( !isset( $configCurrent[ 'framework' ] ) ) {
166
            throw new \Exception( "configure which framework you want to use! \n" );
167
        }
168
169
        if ( !in_array ( $configCurrent[ 'framework' ], $this->frameworkList ) ) {
170
            $frameworks = implode ( "\n\t", $this->frameworkList );
171
            throw new \Exception( "list of frameworks: \n\t\033[1;33m" . $frameworks . "\n\033[0m" );
172
        }
173
174
        return $argv + array_filter ( $configCurrent );
175
    }
176
177
    /**
178
     *
179
     * @param $configTemp
180
     * @param $argv
181
     *
182
     * @return string
183
     */
184
    private static function parseConfigEnv ( $configTemp, $argv )
185
    {
186
        $thisSection = isset( $configTemp[ key ( $configTemp ) ][ 'config-env' ] ) ? $configTemp[ key (
187
            $configTemp
188
        ) ][ 'config-env' ] : null;
189
190
        $thisSection = isset( $argv[ 'config-env' ] ) ? $argv[ 'config-env' ] : $thisSection;
191
192
        if ( isset( $configTemp[ $thisSection ][ 'extends' ] ) ) {
193
            #faz marge da config principal com a config extendida
194
            return $configTemp[ $thisSection ] + $configTemp[ $configTemp[ $thisSection ][ 'extends' ] ];
195
        }
196
197
        return $configTemp[ key ( $configTemp ) ];
198
    }
199
200
    /**
201
     * Carregar o arquivo ini e pré-processa o separador de seção ':'
202
     * no nome da seção (que é usado para a extensão seção) de modo a que a
203
     * matriz resultante tem os nomes de seção corretos e as informações de
204
     * extensão é armazenado em uma sub-ch ve
205
     *
206
     * @param string $filename
207
     *
208
     * @throws \Exception
209
     * @return array
210
     */
211
    protected function loadIniFile ( $filename )
212
    {
213
        if ( !is_file ( $filename ) ) {
214
            throw new \Exception( "configuration file does not exist! \n" );
215
        }
216
217
        $loaded   = parse_ini_file ( $filename, true );
218
        $iniArray = array ();
219
        foreach ( $loaded as $key => $data ) {
220
            $pieces      = explode ( $this->sectionSeparator, $key );
221
            $thisSection = trim ( $pieces[ 0 ] );
222
            switch ( count ( $pieces ) ) {
223
                case 1:
224
                    $iniArray[ $thisSection ] = $data;
225
                    break;
226
227
                case 2:
228
                    $extendedSection          = trim ( $pieces[ 1 ] );
229
                    $iniArray[ $thisSection ] = array_merge ( array ( 'extends' => $extendedSection ), $data );
230
                    break;
231
232
                default:
233
                    throw new \Exception( "Section '$thisSection' may not extend multiple sections in $filename" );
234
            }
235
        }
236
237
        return $iniArray;
238
    }
239
240
    /**
241
     * analisa a opção e cria a instancia do Atapter do determinado framework
242
     *
243
     * @return \Classes\AdapterConfig\AbstractAdapter
244
     *
245
     */
246
    private function factoryConfig ()
247
    {
248
        switch ( strtolower ( $this->argv[ 'framework' ] ) ) {
249
            case 'zf1':
250
                return new ZendFrameworkOne( $this->argv );
251
            case 'phalcon':
252
                return new Phalcon( $this->argv );
253
            default:
254
                return new None( $this->argv );
255
        }
256
257
    }
258
259
    /**
260
     * Analisa a opção e instancia o determinado banco de dados
261
     *
262
     * @param AdapterConfig\AbstractAdapter $config
263
     *
264
     * @return AdaptersDriver\AbsractAdapter
265
     */
266
    private function factoryDriver ( AdapterConfig\AbstractAdapter $config )
267
    {
268
        switch ( $this->argv[ 'driver' ] ) {
269
            case 'pgsql':
270
            case 'pdo_pgsql':
271
                return new Pgsql( $config );
272
            case 'mysql':
273
            case 'pdo_mysql':
274
                return new Mysql( $config );
275
            case 'mssql':
276
                return new Mssql( $config );
277
            case 'dblib':
278
                return new Dblib( $config );
279
            case 'sqlsrv':
280
                return new Sqlsrv( $config );
281
        }
282
    }
283
284
    /**
285
     * @return AdapterConfig\AbstractAdapter
286
     */
287
    public function getAdapterConfig ()
288
    {
289
        if ( !$this->adapterConfig instanceof AbstractAdapter ) {
290
            $this->adapterConfig = $this->factoryConfig ();
291
        }
292
293
        return $this->adapterConfig;
294
    }
295
296
    /**
297
     * @return AdaptersDriver\AbsractAdapter
298
     */
299
    public function getAdapterDriver ( AdapterConfig\AbstractAdapter $config )
300
    {
301
        if ( !$this->adapterDriver ) {
302
            $this->adapterDriver = $this->factoryDriver ( $config );
303
        }
304
305
        return $this->adapterDriver;
306
    }
307
308
}
309