Completed
Push — master ( 694806...e50f75 )
by Emmanuel
03:54
created

Guesser::getColsTypeMapping()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 77
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 77
ccs 52
cts 52
cp 1
rs 8.9342
cc 1
eloc 52
nc 1
nop 0
crap 1

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
 * Inet Data Anonymization
4
 *
5
 * PHP Version 5.3 -> 7.0
6
 *
7
 * @author Emmanuel Dyan
8
 * @author Rémi Sauvat
9
 * @copyright 2005-2015 iNet Process
10
 *
11
 * @package inetprocess/neuralyzer
12
 *
13
 * @license GNU General Public License v2.0
14
 *
15
 * @link http://www.inetprocess.com
16
 */
17
18
namespace Inet\Neuralyzer;
19
20
use Inet\Neuralyzer\Exception\InetAnonGuesserException;
21
22
/**
23
 * Guesser to map field type to Faker Class
24
 */
25
class Guesser implements GuesserInterface
26
{
27
28
    /**
29
     * Returns the version of your guesser
30
     *
31
     * @return string
32
     */
33 8
    public function getVersion()
34
    {
35 8
        return '1.0.0b';
36
    }
37
38
    /**
39
     * Returns an array of fieldName => Faker class
40
     *
41
     * @return array
42
     */
43 11
    public function getColsNameMapping()
44
    {
45
        // can contain regexp
46
        return array(
47
            // Adress and coordinates
48
            '.*\..*street.*' => array(
49 11
                'method' => 'streetAddress',
50 11
            ),
51
            '.*\..*postalcode.*' => array(
52 11
                'method' => 'postcode',
53 11
            ),
54
            '.*\..*city.*' => array(
55 11
                'method' => 'city',
56 11
            ),
57
            '.*\..*state.*' => array(
58 11
                'method' => 'state',
59 11
            ),
60
            '.*\..*country.*' => array(
61 11
                'method' => 'country',
62 11
            ),
63
            '.*\..*phone.*' => array(
64 11
                'method' => 'phoneNumber',
65 11
            ),
66
            // Internet
67
            '.*\.email_address' => array(
68 11
                'method' => 'email',
69 11
            ),
70
            '.*\.url' => array(
71 11
                'method' => 'url',
72 11
            ),
73
            // Text
74
            '.*\.(comments|description)' => array(
75 11
                'method' => 'sentence',
76 11
                'params' => array(20),
77 11
            ),
78
            // Person
79
            '.*\.first_name' => array(
80 11
                'method' => 'firstName',
81 11
            ),
82
            '.*\.last_name' => array(
83 11
                'method' => 'lastName',
84 11
            ),
85 11
        );
86
    }
87
88
    /**
89
     * Retruns an array of fieldType => Faker class
90
     *
91
     * @return array
92
     */
93 9
    public function getColsTypeMapping()
94
    {
95
        return array(
96
            // Strings
97
            'char' => array(
98 9
                'method' => 'sentence',
99 9
                'params' => array(8),
100 9
            ),
101
            'varchar' => array(
102 9
                'method' => 'sentence',
103 9
                'params' => array(8),
104 9
            ),
105
            // Text & Blobs
106
            'text' => array(
107 9
                'method' => 'sentence',
108 9
                'params' => array(20),
109 9
            ),
110
            'blob' => array(
111 9
                'method' => 'sentence',
112 9
                'params' => array(20),
113 9
            ),
114
            'longtext' => array(
115 9
                'method' => 'sentence',
116 9
                'params' => array(70),
117 9
            ),
118
            'longblob' => array(
119 9
                'method' => 'sentence',
120 9
                'params' => array(70),
121 9
            ),
122
            // DateTime
123
            'date' => array(
124 9
                'method' => 'date',
125 9
                'params' => array('Y-m-d', 'now')
126 9
            ),
127
            'datetime' => array(
128 9
                'method' => 'date',
129 9
                'params' => array('Y-m-d H:i:s', 'now')
130 9
            ),
131
            'time' => array(
132 9
                'method' => 'time',
133 9
            ),
134
            // Integer
135
            'tinyint' => array(
136 9
                'method' => 'randomNumber',
137 9
                'params' => array(2),
138 9
            ),
139
            'smallint' => array(
140 9
                'method' => 'randomNumber',
141 9
                'params' => array(4),
142 9
            ),
143
            'mediumint' => array(
144 9
                'method' => 'randomNumber',
145 9
                'params' => array(6),
146 9
            ),
147
            'int' => array(
148 9
                'method' => 'randomNumber',
149 9
                'params' => array(9),
150 9
            ),
151
            'bigint' => array(
152 9
                'method' => 'randomNumber',
153 9
                'params' => array(15),
154 9
            ),
155
            // Decimal
156
            'float' => array(
157 9
                'method' => 'randomFloat',
158 9
                'params' => array(2, 0, 999999),
159 9
            ),
160
            'decimal' => array(
161 9
                'method' => 'randomFloat',
162 9
                'params' => array(2, 0, 999999),
163 9
            ),
164
            'double' => array(
165 9
                'method' => 'randomFloat',
166 9
                'params' => array(2, 0, 999999),
167 9
            ),
168 9
        );
169
    }
170
171
    /**
172
     * Will map cols first by looking for field name then by looking for field type
173
     * if the first returned nothing
174
     *
175
     * @param string $table
176
     * @param string $name
177
     * @param string $type
178
     *
179
     * @return array
180
     */
181 10
    public function mapCol($table, $name, $type, $len)
182
    {
183
        // Try to find by colsName
184 10
        $colsName = $this->getColsNameMapping();
185 10
        foreach ($colsName as $colRegex => $params) {
186 10
            preg_match("/^$colRegex\$/", $table. '.' . $name, $matches);
187 10
            if (!empty($matches)) {
188 1
                return $params;
189
            }
190 9
        }
191
192
        // Hardcoded types
193 9
        if ($type === 'enum') {
194
            return array(
195 1
                'method' => 'randomElement',
196 1
                'params' => array(explode("','", substr($len, 1, -1)))
197 1
            );
198
        }
199
200
        // Try to find by fieldType
201 8
        $colsType = $this->getColsTypeMapping();
202 8
        if (!array_key_exists($type, $colsType)) {
203 1
            throw new InetAnonGuesserException("Can't guess the type $type");
204
        }
205
206 7
        return $colsType[$type];
207
    }
208
}
209