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.

MakerFile   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 340
Duplicated Lines 3.24 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 35
lcom 1
cbo 9
dl 11
loc 340
rs 9
c 4
b 3
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B parseReservedWord() 11 32 6
A filterLocation() 0 10 2
B parseLocation() 0 55 5
A getConfig() 0 4 1
A startTime() 0 5 1
A getRunTime() 0 4 1
A generateFilesFixed() 0 13 2
B run() 0 59 7
A waitOfDatabase() 0 5 1
A reportProcess() 0 20 2
A factoryMakerFile() 0 4 1
A countDiretory() 0 16 4
A getParsedTplContents() 0 22 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 19 and the first side effect is on line 10.

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\AdapterMakerFile\AbstractAdapter;
6
use Classes\AdapterMakerFile\FilesFixeds;
7
use Classes\Maker\AbstractMaker;
8
use Classes\Update\ProgressBar;
9
10
require_once 'Classes/AdapterMakerFile/AbstractAdapter.php';
11
require_once 'Classes/AdapterMakerFile/FilesFixeds.php';
12
require_once 'Classes/Maker/AbstractMaker.php';
13
require_once 'Classes/CleanTrash.php';
14
15
/**
16
 * @author Pedro Alarcao <[email protected]>
17
 * @link   https://github.com/pedro151/orm-generator
18
 */
19
class MakerFile extends AbstractMaker
20
{
21
22
    /**
23
     * @type string[]
24
     */
25
    public $location = array ();
26
27
    /**
28
     * caminho de pastas Base
29
     *
30
     * @type string
31
     */
32
    private $baseLocation = '';
33
34
    /**
35
     * @type \Classes\AdapterConfig\AbstractAdapter
36
     */
37
    private $config;
38
39
    /**
40
     * @type \Classes\AdaptersDriver\AbsractAdapter
41
     */
42
    private $driver;
43
44
    private $countDir;
45
    private $max;
46
47
    private $msgReservedWord = "\033[0mPlease enter the value for reserved word \033[0;31m'%index%' \033[1;33m[%config%]:\033[0m ";
48
49
    public function __construct ( Config $config )
50
    {
51
        $this->config = $config->getAdapterConfig ();
52
        $this->parseReservedWord ( $this->getConfig () );
53
        $this->driver = $config->getAdapterDriver ( $this->getConfig () );
54
        $this->parseLocation ( $config->_basePath );
55
    }
56
57
    /**
58
     * @param AdapterConfig\AbstractAdapter $config
59
     */
60
    public function parseReservedWord ( AdapterConfig\AbstractAdapter $config )
61
    {
62
        $palavrasReservadas = $config->reservedWord;
63
        if ( ! $palavrasReservadas )
0 ignored issues
show
Bug Best Practice introduced by
The expression $palavrasReservadas of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
64
        {
65
            return;
66
        }
67
68
        $schema = $config->getSchemas ();
69
        $db = $config->getDatabase ();
70
        $hasSchema = array_intersect ( $schema , array_flip ( $palavrasReservadas ) );
71
        $hasDatabase = in_array ( $db , $palavrasReservadas );
72
        if ( ! ( $hasSchema or $hasDatabase ) )
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...
73
        {
74
            return;
75
        }
76
77
        echo "- database has reserved words\n";
78 View Code Duplication
        foreach ( $palavrasReservadas as $index => $config )
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
        {
80
            $attribs = array (
81
                "%index%"  => $index ,
82
                "%config%" => $config
83
            );
84
            echo strtr ( $this->msgReservedWord , $attribs );
85
            $line = trim ( fgets ( STDIN ) );
86
            if ( ! empty( $line ) )
87
            {
88
                $this->getConfig ()->reservedWord[ $index ] = $line;
89
            }
90
        }
91
    }
92
93
    /**
94
     * @param array $arrFoldersName
95
     *
96
     * @return string
97
     */
98
    private function filterLocation ( $arrFoldersName )
99
    {
100
        foreach ( $arrFoldersName as $index => $folderName )
101
        {
102
            $arrFoldersName[ $index ] = $this->getConfig ()
103
                                             ->replaceReservedWord ( $folderName );
104
        }
105
106
        return implode ( DIRECTORY_SEPARATOR , array_filter ( $arrFoldersName ) );
107
    }
108
109
    /**
110
     * Analisa os caminhos das pastas base
111
     */
112
    public function parseLocation ( $basePath )
113
    {
114
115
        $arrBase = array (
116
            $basePath ,
117
            $this->config->path
0 ignored issues
show
Documentation introduced by
The property path does not exist on object<Classes\AdapterConfig\AbstractAdapter>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
118
        );
119
120
        $this->baseLocation = $this->filterLocation ( $arrBase );
121
122
        # pasta com nome do driver do banco
123
        $driverBase = '';
124
        if ( (bool) @$this->config->{"folder-database"} )
125
        {
126
            $classDriver = explode ( '\\' , get_class ( $this->driver ) );
127
            $driverBase = end ( $classDriver );
128
        }
129
        $folderName = '';
130
        if ( (bool) @$this->config->{"folder-name"} )
131
        {
132
            $folderName = $this->getClassName ( trim ( $this->config->{"folder-name"} ) );
133
        }
134
135
        if ( $this->config->hasSchemas () )
136
        {
137
138
            $schemas = $this->config->getSchemas ();
139
            foreach ( $schemas as $schema )
140
            {
141
                $arrUrl = array (
142
                    $this->baseLocation ,
143
                    $driverBase ,
144
                    $folderName ,
145
                    $this->getClassName ( $schema )
146
                );
147
148
                $this->location[ $schema ] = $this->filterLocation ( $arrUrl );
149
                unset( $arrUrl );
150
            }
151
152
        } else
153
        {
154
            $url = array (
155
                $this->baseLocation ,
156
                $driverBase ,
157
                $folderName ,
158
                $this->getClassName (
159
                    $this->getConfig ()
160
                         ->getDatabase ()
161
                )
162
            );
163
            $this->location = array ( $this->filterLocation ( $url ) );
164
            unset( $url );
165
        }
166
    }
167
168
    /**
169
     * @return AdapterConfig\AbstractAdapter
170
     */
171
    public function getConfig ()
172
    {
173
        return $this->config;
174
    }
175
176
    /* Get current time */
177
    public function startTime ()
178
    {
179
        echo "\033[1;32mStarting..\033[0m\n";
180
        $this->startTime = microtime ( true );
0 ignored issues
show
Bug introduced by
The property startTime does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
181
    }
182
183
    private function getRunTime ()
184
    {
185
        return round ( ( microtime ( true ) - $this->startTime ) , 3 );
186
    }
187
188
    /**
189
     * @param $objMakeFile
190
     */
191
    private function generateFilesFixed ( FilesFixeds $objFilesFixeds )
192
    {
193
        if ( $objFilesFixeds->hasData () )
194
        {
195
            $file = $this->baseLocation
196
                    . DIRECTORY_SEPARATOR
197
                    . $objFilesFixeds->getFileName ()
198
                    . '.php';
199
200
            $tpl = $this->getParsedTplContents ( $objFilesFixeds->getTpl () );
201
            self::makeSourcer ( $file , $tpl , true );
202
        }
203
    }
204
205
    /**
206
     * Executa o Make, criando arquivos e Diretorios
207
     */
208
    public function run ()
209
    {
210
        $cur = 0;
211
        $numFilesCreated = 0;
212
        $numFilesIgnored = 0;
213
214
        $this->startTime ();
215
        $this->waitOfDatabase ( $cur );
216
217
        $this->max = $this->driver->getTotalTables () * $this->countDiretory ();
218
        $progressBar = ProgressBar::getInstance ()->setMaxByte ( $this->max );
219
220
        foreach ( $this->location as $schema => $location )
221
        {
222
            foreach ( $this->factoryMakerFile () as $objMakeFile )
223
            {
224
                $path = $location . DIRECTORY_SEPARATOR . $objMakeFile->getPastName ();
225
                if ( $this->config->isCleanTrash () )
226
                {
227
                    CleanTrash::getInstance ()->run ( $path , $this->driver , $schema );
228
                }
229
                self::makeDir ( $path );
230
231
                #Cria as Classes de Exceção e Abstratas
232
                foreach ( $objMakeFile->getListFilesFixed () as $nameObject )
233
                {
234
                    $this->generateFilesFixed ( $objMakeFile->getFilesFixeds ( $nameObject ) );
235
                }
236
237
                #Cria as Classes do Banco
238
                foreach ( $this->driver->getTables ( $schema ) as $key => $objTables )
239
                {
240
                    $file = $path . DIRECTORY_SEPARATOR
241
                            . self::getClassName ( $objTables->getName () ) . '.php';
242
243
                    $tpl = $this->getParsedTplContents (
244
                        $objMakeFile->getFileTpl () ,
245
                        $objMakeFile->parseRelation ( $this , $objTables ) ,
246
                        $objTables ,
247
                        $objMakeFile
248
249
                    );
250
                    if ( self::makeSourcer ( $file , $tpl , $objMakeFile->isOverwrite () ) )
251
                    {
252
                        ++ $numFilesCreated;
253
                    } else
254
                    {
255
                        ++ $numFilesIgnored;
256
                    }
257
258
                    ++$cur;
259
                    $progressBar->setProgress ( $cur )->render();
260
                }
261
            }
262
        }
263
264
        $this->reportProcess ( $numFilesCreated , $numFilesIgnored );
265
        $progressBar->finish();
266
    }
267
268
    private function waitOfDatabase ( &$cur )
0 ignored issues
show
Unused Code introduced by
The parameter $cur is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
269
    {
270
        printf ( "\033[1;33mWait, the database is being analyzed..\033[0m\n" );
271
        $this->driver->runDatabase ();
272
    }
273
274
    private function reportProcess ( $numFilesCreated = 0 , $numFilesIgnored = 0 )
275
    {
276
        if ( $this->config->isStatusEnabled () )
277
        {
278
            $databases = count ( $this->location );
279
            $countDir = $this->countDiretory ();
280
            $totalTable = $this->driver->getTotalTables ();
281
            $totalFiles = $numFilesIgnored + $numFilesCreated;
282
            $totalFilesDeleted = CleanTrash::getInstance ()->getNumFilesDeleted ();
283
            echo "\n------";
284
            printf ( "\n\r-Files generated/updated: \033[1;33m%s\033[0m" , $numFilesCreated );
285
            printf ( "\n\r-Files not upgradeable: \033[1;33m%s\033[0m" , $numFilesIgnored );
286
            printf ( "\n\r-Files deleted: \033[1;33m%s\033[0m" , $totalFilesDeleted );
287
            printf ( "\n\r-Total files analyzed: \033[1;33m%s of %s\033[0m" , $totalFiles , $this->max );
288
            printf ( "\n\r-Diretories: \033[1;33m%s\033[0m" , $databases * $countDir );
289
            printf ( "\n\r-Scanned tables: \033[1;33m%s\033[0m" , $totalTable );
290
            printf ( "\n\r-Execution time: \033[1;33m%ssec\033[0m" , $this->getRunTime () );
291
            echo "\n------";
292
        }
293
    }
294
295
    /**
296
     * Instancia os Modulos de diretorios e tampletes
297
     *
298
     * @return AbstractAdapter[]
299
     */
300
    public function factoryMakerFile ()
301
    {
302
        return $this->config->getMakeFileInstances ();
303
    }
304
305
    /**
306
     * conta o numero de diretorios que serao criados
307
     *
308
     * @return int
309
     */
310
    public function countDiretory ()
311
    {
312
        if ( null === $this->countDir )
313
        {
314
            $this->countDir = 1;
315
            foreach ( $this->factoryMakerFile () as $abstractAdapter )
316
            {
317
                if ( $abstractAdapter->hasDiretory () )
318
                {
319
                    ++ $this->countDir;
320
                }
321
            }
322
        }
323
324
        return $this->countDir;
325
    }
326
327
    /**
328
     *
329
     * parse a tpl file and return the result
330
     *
331
     * @param String $tplFile
332
     *
333
     * @return String
334
     */
335
    protected function getParsedTplContents (
336
        $tplFile , $vars = array () , \Classes\Db\DbTable $objTables = null ,
337
        $objMakeFile = null
338
    ){
339
340
        $arrUrl = array (
341
            __DIR__ ,
342
            'templates' ,
343
            $this->config->framework ,
0 ignored issues
show
Documentation introduced by
The property framework does not exist on object<Classes\AdapterConfig\AbstractAdapter>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
344
            $tplFile
345
        );
346
347
        $filePath = implode ( DIRECTORY_SEPARATOR , filter_var_array ( $arrUrl ) );
348
349
        extract ( $vars );
350
        ob_start ();
351
        require $filePath;
352
        $data = ob_get_contents ();
353
        ob_end_clean ();
354
355
        return $data;
356
    }
357
358
}