Completed
Push — master ( da9ea2...cfe8fe )
by Rougin
08:08
created

ModelGenerator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 5.56 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 74.47%

Importance

Changes 6
Bugs 2 Features 2
Metric Value
wmc 6
lcom 1
cbo 3
dl 5
loc 90
ccs 35
cts 47
cp 0.7447
rs 10
c 6
b 2
f 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A prepareData() 0 10 1
B generate() 5 45 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\Describe\Describe;
6
use Rougin\Combustor\Common\Tools;
7
use Rougin\Combustor\Common\Inflector;
8
9
/**
10
 * Model Generator
11
 *
12
 * Generates CodeIgniter-based models.
13
 * 
14
 * @package Combustor
15
 * @author  Rougin Royce Gutib <[email protected]>
16
 */
17
class ModelGenerator implements GeneratorInterface
18
{
19
    /**
20
     * @var \Rougin\Describe\Describe
21
     */
22
    protected $describe;
23
24
    /**
25
     * @var array
26
     */
27
    protected $data = [];
28
29
    /**
30
     * @param \Rougin\Describe\Describe $describe
31
     * @param array $data
32
     */
33 6
    public function __construct(Describe $describe, array $data)
34
    {
35 6
        $this->describe = $describe;
36 6
        $this->data = $data;
37 6
    }
38
39
    /**
40
     * Prepares the data before generation.
41
     * 
42
     * @param  array &$data
43
     * @return void
44
     */
45 6
    public function prepareData(array &$data)
46
    {
47 6
        $data['camel'] = [];
48 6
        $data['columns'] = [];
49 6
        $data['indexes'] = [];
50 6
        $data['primaryKeys'] = [];
51 6
        $data['underscore'] = [];
52 6
        $data['columns'] = $this->describe->getTable($data['name']);
53 6
        $data['primaryKey'] = $this->describe->getPrimaryKey($data['name']);
54 6
    }
55
56
    /**
57
     * Generates set of code based on data.
58
     * 
59
     * @return array
60
     */
61 6
    public function generate()
62
    {
63 6
        $this->prepareData($this->data);
64
65 6
        foreach ($this->data['columns'] as $column) {
66 6
            $field = strtolower($column->getField());
67 6
            $accessor = 'get_'.$field;
68 6
            $mutator = 'set_'.$field;
69
70 6
            $this->data['camel'][$field] = [
71 6
                'field' => lcfirst(Inflector::camelize($field)),
72 6
                'accessor' => lcfirst(Inflector::camelize($accessor)),
73 6
                'mutator' => lcfirst(Inflector::camelize($mutator))
74 6
            ];
75
76 6
            $this->data['underscore'][$field] = [
77 6
                'field' => lcfirst(Inflector::underscore($field)),
78 6
                'accessor' => lcfirst(Inflector::underscore($accessor)),
79 6
                'mutator' => lcfirst(Inflector::underscore($mutator))
80 6
            ];
81
82 6
            if ($column->isForeignKey()) {
83
                $field = $column->getField();
84
85
                array_push($this->data['indexes'], $field);
86
87
                $this->data['primaryKeys'][$field] = 'get_'. 
88
                    $this->describe->getPrimaryKey(
89
                        $column->getReferencedTable()
90
                    );
91
92 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...
93
                    $this->data['primaryKeys'][$field] = Inflector::camelize(
94
                        $this->data['primaryKeys'][$field]
95
                    );
96
                }
97
            }
98
99 6
            $column->setReferencedTable(
100 6
                Tools::stripTableSchema($column->getReferencedTable())
101 6
            );
102 6
        }
103
104 6
        return $this->data;
105
    }
106
}
107