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
04:05
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 = dirname ( $basePath . '/' );
157
158
        $configIni = isset( $argv[ 'config-ini' ] )
159
            ? $argv[ 'config-ini' ] : $this->configIniDefault;
160
161
        $configTemp    = $this->loadIniFile ( realpath ( $configIni ) );
162
        $configCurrent = self::parseConfigEnv ( $configTemp, $argv );
163
164
        if ( !isset( $configCurrent[ 'framework' ] ) ) {
165
            throw new \Exception( "configure which framework you want to use! \n" );
166
        }
167
168
        if ( !in_array ( $configCurrent[ 'framework' ], $this->frameworkList ) ) {
169
            $frameworks = implode ( "\n\t", $this->frameworkList );
170
            throw new \Exception( "list of frameworks: \n\t\033[1;33m" . $frameworks . "\n\033[0m" );
171
        }
172
173
        return $argv + array_filter ( $configCurrent );
174
    }
175
176
    /**
177
     *
178
     * @param $configTemp
179
     * @param $argv
180
     *
181
     * @return string
182
     */
183
    private static function parseConfigEnv ( $configTemp, $argv )
184
    {
185
        $thisSection = isset( $configTemp[ key ( $configTemp ) ][ 'config-env' ] ) ? $configTemp[ key (
186
            $configTemp
187
        ) ][ 'config-env' ] : null;
188
189
        $thisSection = isset( $argv[ 'config-env' ] ) ? $argv[ 'config-env' ] : $thisSection;
190
191
        if ( isset( $configTemp[ $thisSection ][ 'extends' ] ) ) {
192
            #faz marge da config principal com a config extendida
193
            return $configTemp[ $thisSection ] + $configTemp[ $configTemp[ $thisSection ][ 'extends' ] ];
194
        }
195
196
        return $configTemp[ key ( $configTemp ) ];
197
    }
198
199
    /**
200
     * Carregar o arquivo ini e pré-processa o separador de seção ':'
201
     * no nome da seção (que é usado para a extensão seção) de modo a que a
202
     * matriz resultante tem os nomes de seção corretos e as informações de
203
     * extensão é armazenado em uma sub-ch ve
204
     *
205
     * @param string $filename
206
     *
207
     * @throws \Exception
208
     * @return array
209
     */
210
    protected function loadIniFile ( $filename )
211
    {
212
        if ( !is_file ( $filename ) ) {
213
            throw new \Exception( "configuration file does not exist! \n" );
214
        }
215
216
        $loaded   = parse_ini_file ( $filename, true );
217
        $iniArray = array ();
218
        foreach ( $loaded as $key => $data ) {
219
            $pieces      = explode ( $this->sectionSeparator, $key );
220
            $thisSection = trim ( $pieces[ 0 ] );
221
            switch ( count ( $pieces ) ) {
222
                case 1:
223
                    $iniArray[ $thisSection ] = $data;
224
                    break;
225
226
                case 2:
227
                    $extendedSection          = trim ( $pieces[ 1 ] );
228
                    $iniArray[ $thisSection ] = array_merge ( array ( 'extends' => $extendedSection ), $data );
229
                    break;
230
231
                default:
232
                    throw new \Exception( "Section '$thisSection' may not extend multiple sections in $filename" );
233
            }
234
        }
235
236
        return $iniArray;
237
    }
238
239
    /**
240
     * analisa a opção e cria a instancia do Atapter do determinado framework
241
     *
242
     * @return \Classes\AdapterConfig\AbstractAdapter
243
     *
244
     */
245
    private function factoryConfig ()
246
    {
247
        switch ( strtolower ( $this->argv[ 'framework' ] ) ) {
248
            case 'zf1':
249
                return new ZendFrameworkOne( $this->argv );
250
            case 'phalcon':
251
                return new Phalcon( $this->argv );
252
            default:
253
                return new None( $this->argv );
254
        }
255
256
    }
257
258
    /**
259
     * Analisa a opção e instancia o determinado banco de dados
260
     *
261
     * @param AdapterConfig\AbstractAdapter $config
262
     *
263
     * @return AdaptersDriver\AbsractAdapter
264
     */
265
    private function factoryDriver ( AdapterConfig\AbstractAdapter $config )
266
    {
267
        switch ( $this->argv[ 'driver' ] ) {
268
            case 'pgsql':
269
            case 'pdo_pgsql':
270
                return new Pgsql( $config );
271
            case 'mysql':
272
            case 'pdo_mysql':
273
                return new Mysql( $config );
274
            case 'mssql':
275
                return new Mssql( $config );
276
            case 'dblib':
277
                return new Dblib( $config );
278
            case 'sqlsrv':
279
                return new Sqlsrv( $config );
280
        }
281
    }
282
283
    /**
284
     * @return AdapterConfig\AbstractAdapter
285
     */
286
    public function getAdapterConfig ()
287
    {
288
        if ( !$this->adapterConfig instanceof AbstractAdapter ) {
289
            $this->adapterConfig = $this->factoryConfig ();
290
        }
291
292
        return $this->adapterConfig;
293
    }
294
295
    /**
296
     * @return AdaptersDriver\AbsractAdapter
297
     */
298
    public function getAdapterDriver ( AdapterConfig\AbstractAdapter $config )
299
    {
300
        if ( !$this->adapterDriver ) {
301
            $this->adapterDriver = $this->factoryDriver ( $config );
302
        }
303
304
        return $this->adapterDriver;
305
    }
306
307
}
308