Completed
Push — master ( cfe8fe...f9982c )
by Rougin
04:59
created

ModelGenerator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 7.58 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 67.57%

Importance

Changes 7
Bugs 2 Features 2
Metric Value
wmc 5
c 7
b 2
f 2
lcom 1
cbo 4
dl 5
loc 66
ccs 25
cts 37
cp 0.6757
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareData() 0 10 1
B generate() 5 41 4

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
namespace Rougin\Combustor\Generator;
4
5
use Rougin\Combustor\Common\Tools;
6
use Rougin\Combustor\Common\Inflector;
7
8
/**
9
 * Model Generator
10
 *
11
 * Generates CodeIgniter-based models.
12
 * 
13
 * @package Combustor
14
 * @author  Rougin Royce Gutib <[email protected]>
15
 */
16
class ModelGenerator extends BaseGenerator implements GeneratorInterface
17
{
18
    /**
19
     * Prepares the data before generation.
20
     * 
21
     * @param  array &$data
22
     * @return void
23
     */
24 9
    public function prepareData(array &$data)
25
    {
26 9
        $data['camel'] = [];
27 9
        $data['columns'] = [];
28 9
        $data['indexes'] = [];
29 9
        $data['primaryKeys'] = [];
30 9
        $data['underscore'] = [];
31 9
        $data['columns'] = $this->describe->getTable($data['name']);
32 9
        $data['primaryKey'] = $this->describe->getPrimaryKey($data['name']);
33 9
    }
34
35
    /**
36
     * Generates set of code based on data.
37
     * 
38
     * @return array
39
     */
40 9
    public function generate()
41
    {
42 9
        $this->prepareData($this->data);
43
44 9
        foreach ($this->data['columns'] as $column) {
45 9
            $field = strtolower($column->getField());
46
47 9
            $this->data['camel'][$field] = $this->transformField(
48 9
                $field,
49
                'camelize'
50 9
            );
51
52 9
            $this->data['underscore'][$field] = $this->transformField(
53 9
                $field,
54
                'underscore'
55 9
            );
56
57 9
            if ($column->isForeignKey()) {
58
                $field = $column->getField();
59
60
                array_push($this->data['indexes'], $field);
61
62
                $this->data['primaryKeys'][$field] = 'get_' . 
63
                    $this->describe->getPrimaryKey(
64
                        $column->getReferencedTable()
65
                    );
66
67 View Code Duplication
                if ($this->data['isCamel']) {
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...
68
                    $this->data['primaryKeys'][$field] = Inflector::camelize(
69
                        $this->data['primaryKeys'][$field]
70
                    );
71
                }
72
            }
73
74 9
            $column->setReferencedTable(
75 9
                Tools::stripTableSchema($column->getReferencedTable())
76 9
            );
77 9
        }
78
79 9
        return $this->data;
80
    }
81
}
82