Completed
Push — master ( 267f53...0f0d6a )
by Rougin
04:34
created

ViewGenerator::prepareData()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 5.2742

Importance

Changes 8
Bugs 1 Features 0
Metric Value
c 8
b 1
f 0
dl 0
loc 51
ccs 28
cts 36
cp 0.7778
rs 8.6588
cc 5
eloc 31
nc 16
nop 1
crap 5.2742

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Rougin\Combustor\Generator;
4
5
use Rougin\Combustor\Common\Tools;
6
7
/**
8
 * View Generator
9
 *
10
 * Generates CodeIgniter-based views.
11
 * 
12
 * @package Combustor
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 */
15
class ViewGenerator extends BaseGenerator implements GeneratorInterface
16
{
17
    /**
18
     * Prepares the data before generation.
19
     * 
20
     * @param  array $data
21
     * @return void
22
     */
23 9
    public function prepareData(array &$data)
24
    {
25
        $bootstrap = [
26 9
            'button' => 'btn btn-default',
27 9
            'buttonPrimary' => 'btn btn-primary',
28 9
            'formControl' => 'form-control',
29 9
            'formGroup' => 'form-group col-lg-12 col-md-12 col-sm-12 col-xs-12',
30 9
            'label' => 'control-label',
31 9
            'table' => 'table table table-striped table-hover',
32
            'textRight' => 'text-right'
33 9
        ];
34
35 9
        if ($data['isBootstrap']) {
36 9
            $data['bootstrap'] = $bootstrap;
37 9
        }
38
39 9
        $data['camel'] = [];
40 9
        $data['underscore'] = [];
41 9
        $data['foreignKeys'] = [];
42 9
        $data['primaryKeys'] = [];
43
44 9
        $data['plural'] = plural($data['name']);
45 9
        $data['singular'] = singular($data['name']);
46
47 9
        $primaryKey = 'get_' . $this->describe->getPrimaryKey($data['name']);
48 9
        $data['primaryKey'] = $primaryKey;
49
50
        // Workaround...
51 9
        if ($data['primaryKey'] == 'get_') {
52
            $singular = singular($data['name']);
53
            $primaryKey = 'get_' . $this->describe->getPrimaryKey($singular);
54
55
            $data['primaryKey'] = $primaryKey;
56
        }
57
58 9
        if ($this->data['isCamel']) {
59 6
            $data['primaryKey'] = camelize($data['primaryKey']);
60 6
        }
61
62 9
        $data['columns'] = $this->describe->getTable(
63 9
            $data['name']
64 9
        );
65
66
        // Workaround...
67 9
        if (empty($data['columns'])) {
68
            $singular = singular($data['name']);
69
70
            $data['columns'] = $primaryKey;
71
            $data['columns'] = $this->describe->getTable($singular);
72
        }
73 9
    }
74
75
    /**
76
     * Generates set of code based on data.
77
     * 
78
     * @return array
79
     */
80 9
    public function generate()
81
    {
82 9
        $this->prepareData($this->data);
83
84 9
        foreach ($this->data['columns'] as $column) {
85 9
            $field = strtolower($column->getField());
86
87 9
            $this->data['camel'][$field] = $this->transformField(
88 9
                $field,
89
                'camelize'
90 9
            );
91
92 9
            $this->data['underscore'][$field] = $this->transformField(
93 9
                $field,
94
                'underscore'
95 9
            );
96
97 9
            if ($column->isForeignKey()) {
98 9
                $referencedTable = Tools::stripTableSchema(
99 9
                    $column->getReferencedTable()
100 9
                );
101
102 9
                $this->data['foreignKeys'][$field] = plural($referencedTable);
103
104 9
                $singular = $field . '_singular';
105
106 9
                $this->data['foreignKeys'][$singular] = singular(
107
                    $referencedTable
108 9
                );
109
110 9
                $this->data = $this->getPrimaryKey(
111 9
                    $this->data,
112 9
                    $field,
113
                    $referencedTable
114 9
                );
115 9
            }
116 9
        }
117
118 9
        return $this->data;
119
    }
120
}
121