ObjectGenerator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 46.24 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 8
dl 43
loc 93
ccs 0
cts 39
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 2
A generate() 23 23 3
A generateBase() 20 20 2

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
2
3
/*
4
 * This file is part of the "RocketORM" package.
5
 *
6
 * https://github.com/RocketORM/ORM
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rocket\ORM\Generator\Model\Object;
13
14
use Rocket\ORM\Generator\Generator;
15
use Rocket\ORM\Generator\Schema\Schema;
16
use Rocket\ORM\Rocket;
17
18
/**
19
 * @author Sylvain Lorinet <[email protected]>
20
 */
21
class ObjectGenerator extends Generator
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $modelNamespace;
27
28
    /**
29
     * @var \Twig_Environment
30
     */
31
    protected $twig;
32
33
34
    /**
35
     * @param string $modelNamespace
36
     * @param array  $templateDirs
37
     */
38
    public function __construct($modelNamespace = '', array $templateDirs = [])
39
    {
40
        $this->modelNamespace = $modelNamespace;
41
42
        $loader = new \Twig_Loader_Filesystem(array_merge($templateDirs, [
43
            __DIR__ . '/../../Resources/Skeletons/Model/Object'
44
        ]));
45
        $loader->addPath(__DIR__ . '/../../Resources/Skeletons/Model/Object/Driver/SQLite', 'sqlite');
46
47
        $this->twig = new \Twig_Environment($loader, [
48
            'cache'            => false,
49
            'strict_variables' => true
50
        ]);
51
52
        $this->twig->addFilter(new \Twig_SimpleFilter('type', function ($variable, $type) {
53
            switch ($type) {
54
                case 'string': return is_string($variable);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
55
            }
56
57
            throw new \LogicException('Unknown type "' . $type . '" for the Twig filter "type"');
58
        }));
59
    }
60
61
    /**
62
     * @param Schema $schema
63
     *
64
     * @return void
65
     */
66 View Code Duplication
    public function generate(Schema $schema)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
67
    {
68
        // First generate base
69
        $this->generateBase($schema);
70
71
        $outputDirectory = $schema->absoluteDirectory;
72
        $this->createDirectory($outputDirectory);
73
74
        foreach ($schema->getTables() as $table) {
75
            $outputFile = $outputDirectory . DIRECTORY_SEPARATOR . $table->phpName . '.php';
76
77
            // Check if file already exists, do not override the file
78
            if (is_file($outputFile)) {
79
                continue;
80
            }
81
82
            $template = $this->twig->render('object.php.twig', [
83
                'table'  => $table
84
            ]);
85
86
            file_put_contents($outputFile, $template);
87
        }
88
    }
89
90
    /**
91
     * @param Schema $schema
92
     */
93 View Code Duplication
    protected function generateBase(Schema $schema)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
94
    {
95
        $outputDirectory = $schema->absoluteDirectory . DIRECTORY_SEPARATOR . 'Base';
96
        $this->createDirectory($outputDirectory);
97
98
        // Allow overriding template for a given driver
99
        $driver = Rocket::getConnectionDriver($schema->connection);
100
101
        foreach ($schema->getTables() as $table) {
102
            $template = $this->twig->resolveTemplate([
103
                '@' . $driver . '/base_object.php.twig',
104
                'base_object.php.twig'
105
            ])->render([
106
                'table'  => $table,
107
                'driver' => $driver
108
            ]);
109
110
            file_put_contents($outputDirectory . DIRECTORY_SEPARATOR . 'Base' . $table->phpName . '.php', $template);
111
        }
112
    }
113
}
114