TransformerGenerator   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 147
rs 9.0399
c 0
b 0
f 0
wmc 42

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRootNamespace() 0 4 1
A getPathConfigNode() 0 3 1
A getBasePath() 0 3 1
D getTypeFromField() 0 45 34
A getPath() 0 8 1
A getReplacements() 0 19 1
A getColumns() 0 13 3

How to fix   Complexity   

Complex Class

Complex classes like TransformerGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TransformerGenerator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Yeelight\Generators;
4
5
/**
6
 * Class TransformerGenerator
7
 *
8
 * @category Yeelight
9
 *
10
 * @package Yeelight\Generators
11
 *
12
 * @author Sheldon Lee <[email protected]>
13
 *
14
 * @license https://opensource.org/licenses/MIT MIT
15
 *
16
 * @link https://www.yeelight.com
17
 */
18
class TransformerGenerator extends Generator
19
{
20
    /**
21
     * Get stub name.
22
     *
23
     * @var string
24
     */
25
    protected $stub = 'transformer/transformer';
26
27
    /**
28
     * Get root namespace.
29
     *
30
     * @return string
31
     */
32
    public function getRootNamespace()
33
    {
34
        return parent::getRootNamespace() .
35
            parent::getConfigGeneratorClassPath($this->getPathConfigNode());
0 ignored issues
show
Bug introduced by
Are you sure parent::getConfigGenerat...s->getPathConfigNode()) of type Illuminate\Config\Repository|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            /** @scrutinizer ignore-type */ parent::getConfigGeneratorClassPath($this->getPathConfigNode());
Loading history...
36
    }
37
38
    /**
39
     * Get generator path config node.
40
     *
41
     * @return string
42
     */
43
    public function getPathConfigNode()
44
    {
45
        return 'transformers';
46
    }
47
48
    /**
49
     * Get destination path for generated file.
50
     *
51
     * @return string
52
     */
53
    public function getPath()
54
    {
55
        return $this->getBasePath() .
56
            '/' .
57
            parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) .
0 ignored issues
show
Bug introduced by
Are you sure parent::getConfigGenerat...PathConfigNode(), true) of type Illuminate\Config\Repository|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
            /** @scrutinizer ignore-type */ parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) .
Loading history...
58
            '/' .
59
            $this->getName() .
60
            'Transformer.php';
61
    }
62
63
    /**
64
     * Get base path of destination file.
65
     *
66
     * @return string
67
     */
68
    public function getBasePath()
69
    {
70
        return config('repository.generator.basePath', app_path());
71
    }
72
73
    /**
74
     * Get default Columns description.
75
     *
76
     * @return string
77
     */
78
    public function getColumns()
79
    {
80
        $fields = $this->fields;
0 ignored issues
show
Bug Best Practice introduced by
The property fields does not exist on Yeelight\Generators\TransformerGenerator. Since you implemented __get, consider adding a @property annotation.
Loading history...
81
        $result = '';
82
        if (!empty($fields)) {
83
            foreach ($fields as $index => $field) {
84
                $type = $this->getTypeFromField($field);
85
                $result .= "\t\t\t'{$field['name']}' => ($type) ".'$model->'."{$field['name']},\r\n";
86
            }
87
        }
88
        $result .= '';
89
90
        return $result;
91
    }
92
93
    private function getTypeFromField($filed)
94
    {
95
        switch ($filed['type']) {
96
            case 'string':
97
            case 'text':
98
            case 'date':
99
            case 'time':
100
            case 'dateTime':
101
            case 'timestamp':
102
            case 'char':
103
            case 'mediumText':
104
            case 'longText':
105
            case 'enum':
106
            case 'json':
107
            case 'jsonb':
108
            case 'dateTimeTz':
109
            case 'timeTz':
110
            case 'timestampTz':
111
            case 'nullableTimestamps':
112
            case 'binary':
113
            case 'ipAddress':
114
            case 'macAddress':
115
                return 'string';
116
117
            case 'integer':
118
            case 'tinyInteger':
119
            case 'smallInteger':
120
            case 'mediumInteger':
121
            case 'bigInteger':
122
            case 'unsignedTinyInteger':
123
            case 'unsignedSmallInteger':
124
            case 'unsignedMediumInteger':
125
            case 'unsignedInteger':
126
            case 'unsignedBigInteger':
127
                return 'int';
128
129
            case 'float':
130
            case 'decimal':
131
                return 'float';
132
133
            case 'double':
134
                return 'double';
135
136
            case 'boolean':
137
                return 'boolean';
138
        }
139
    }
140
141
    /**
142
     * Get array replacements.
143
     *
144
     * @return array
145
     */
146
    public function getReplacements()
147
    {
148
        $modelGenerator = new ModelGenerator(
149
            [
150
                'name' => $this->name,
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Yeelight\Generators\TransformerGenerator. Since you implemented __get, consider adding a @property annotation.
Loading history...
151
            ]
152
        );
153
        $model = $modelGenerator->getRootNamespace().'\\'.$modelGenerator->getName();
154
        $model = str_replace([
155
            '\\',
156
            '/',
157
        ], '\\', $model);
158
159
        return array_merge(
160
            parent::getReplacements(),
161
            [
162
                'model' => $model,
163
                'transformer_fields' => $this->getColumns(),
164
                '_id_name' => $this->getIdName(),
165
            ]
166
        );
167
    }
168
}
169