Completed
Pull Request — master (#21)
by Dennis
21:11 queued 02:37
created

Faker   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 80.22%

Importance

Changes 0
Metric Value
wmc 49
lcom 1
cbo 4
dl 0
loc 207
ccs 73
cts 91
cp 0.8022
rs 8.48
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 14 3
A getSupportedProviders() 0 4 1
A generate() 0 4 1
A hasProvider() 0 17 5
A callCustomProvider() 0 9 2
A hasCustomProvider() 0 6 2
A __call() 0 10 2
D guessProviderName() 0 62 32

How to fix   Complexity   

Complex Class

Complex classes like Faker 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 Faker, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace TildBJ\Seeder\Provider;
3
4
/***************************************************************
5
 *
6
 *  Copyright notice
7
 *
8
 *  (c) 2016 Dennis Römmich <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * Class Faker
32
 *
33
 * @method string getName()
34
 * @method string getFirstName()
35
 * @method string getFirstNameMale()
36
 * @method string getFirstNameFemale()
37
 * @method string getLastName()
38
 * @method string getTitle()
39
 * @method string getTitleMale()
40
 * @method string getTitleFemale()
41
 * @method string getCitySuffix()
42
 * @method string getStreetSuffix()
43
 * @method string getBuildingNumber()
44
 * @method string getCity()
45
 * @method string getStreetName()
46
 * @method string getStreetAddress()
47
 * @method string getPostcode()
48
 * @method string getAddress()
49
 * @method string getCountry()
50
 * @method string getLatitude()
51
 * @method string getLongitude()
52
 * @method string getEan13()
53
 * @method string getEan8()
54
 * @method string getIsbn13()
55
 * @method string getIsbn10()
56
 * @method string getPhoneNumber()
57
 * @method string getCompany()
58
 * @method string getCompanySuffix()
59
 * @method string getJobTitle()
60
 * @method string getCreditCardType()
61
 * @method string getCreditCardNumber()
62
 * @method string getCreditCardExpirationDate()
63
 * @method string getCreditCardExpirationDateString()
64
 * @method string getCreditCardDetails()
65
 * @method string getBankAccountNumber()
66
 * @method string getIban()
67
 * @method string getSwiftBicNumber()
68
 * @method string getVat()
69
 * @method string getWord()
70
 * @method string getWords()
71
 * @method string getSentence()
72
 * @method string getParagraph()
73
 * @method string getText()
74
 * @method string getRealText()
75
 * @method string getEmail()
76
 * @method string getSafeEmail()
77
 * @method string getFreeEmail()
78
 * @method string getCompanyEmail()
79
 * @method string getFreeEmailDomain()
80
 * @method string getSafeEmailDomain()
81
 * @method string getUserName()
82
 * @method string getPassword()
83
 * @method string getDomainName()
84
 * @method string getDomainWord()
85
 * @method string getTld()
86
 * @method string getUrl()
87
 * @method string getSlug()
88
 * @method string getIpv4()
89
 * @method string getIpv6()
90
 * @method string getLocalIpv4()
91
 * @method string getMacAddress()
92
 * @method string getUnixTime()
93
 * @method string getDateTime()
94
 * @method string getDateTimeAD()
95
 * @method string getIso8601()
96
 * @method string getDateTimeThisCentury()
97
 * @method string getDateTimeThisDecade()
98
 * @method string getDateTimeThisYear()
99
 * @method string getDateTimeThisMonth()
100
 * @method string getAmPm()
101
 * @method string getDayOfMonth()
102
 * @method string getDayOfWeek()
103
 * @method string getMonth()
104
 * @method string getMonthName()
105
 * @method string getYear()
106
 * @method string getCentury()
107
 * @method string getTimezone()
108
 * @method string getDate()
109
 * @method string getTime()
110
 * @method string getMd5()
111
 * @method string getSha1()
112
 * @method string getSha256()
113
 * @method string getLocale()
114
 * @method string getCountryCode()
115
 * @method string getCountryISOAlpha3()
116
 * @method string getLanguageCode()
117
 * @method string getCurrencyCode()
118
 * @method string getBoolean()
119
 * @method string getRandomDigit()
120
 * @method string getRandomDigitNotNull()
121
 * @method string getRandomLetter()
122
 * @method string getRandomAscii()
123
 * @method string getRandomNumber()
124
 * @method string getRandomFloat()
125
 * @method string getMacProcessor()
126
 * @method string getLinuxProcessor()
127
 * @method string getUserAgent()
128
 * @method string getChrome()
129
 * @method string getFirefox()
130
 * @method string getSafari()
131
 * @method string getOpera()
132
 * @method string getInternetExplorer()
133
 * @method string getWindowsPlatformToken()
134
 * @method string getMacPlatformToken()
135
 * @method string getLinuxPlatformToken()
136
 * @method string getUuid()
137
 * @method string getMimeType()
138
 * @method string getFileExtension()
139
 * @method string getImageUrl()
140
 * @method string getHexColor()
141
 * @method string getSafeHexColor()
142
 * @method string getRgbColor()
143
 * @method string getRgbCssColor()
144
 * @method string getSafeColorName()
145
 * @method string getColorName()
146
 *
147
 * @package TildBJ\Seeder\Provider\Faker
148
 */
149
class Faker implements \TildBJ\Seeder\Faker
150
{
151
    /** @var \Faker\Generator $generator */
152
    protected $generator = null;
153
154
    /** @var \Faker\Guesser\Name $guesser */
155
    protected $guesser = null;
156
157
    /**
158
     * Fields we don't take care of
159
     *
160
     * @var array $skippedProvider
161
     */
162
    public static $skippedProvider = [
163
        'l10n_parent',
164
        'l10n_diffsource',
165
        'cruser_id',
166
        'TSconfig',
167
        'tx_extbase_type',
168
        'felogin_redirectPid',
169
        't3ver_label',
170
        'starttime',
171
        'endtime',
172
    ];
173
174
    /**
175
     * Faker constructor.
176
     */
177 12
    public function __construct(\Faker\Generator $generator)
178
    {
179 12
        $this->generator = $generator;
180 12
    }
181
182
    /**
183
     * Returns random dummy data by property
184
     *
185
     * @param string $property
186
     * @return mixed
187
     * @throws NotFoundException
188
     */
189 10
    public function get($property)
190
    {
191 10
        if (!$provider = $this->guessProviderName($property)) {
192 1
            $provider = $property;
193
        }
194
195 10
        if (!$this->hasProvider($provider)) {
196 1
            throw new \TildBJ\Seeder\Provider\NotFoundException(
197 1
                'No provider found for ' . $provider
198
            );
199
        }
200
201 9
        return $this->generate($provider);
202
    }
203
204
    /**
205
     * @return array
206
     */
207
    public function getSupportedProviders()
208
    {
209
        return $this->generator->getProviders();
210
    }
211
212
    /**
213
     * @param string $providerName
214
     * @return mixed
215
     */
216 9
    private function generate($providerName)
217
    {
218 9
        return $this->generator->$providerName;
219
    }
220
221
    /**
222
     * @param string $name
223
     * @return bool
224
     */
225 13
    private function hasProvider($name)
226
    {
227 13
        if (empty($name)) {
228
            return false;
229
        }
230 13
        if ($this->hasCustomProvider($name)) {
231
            return true;
232
        }
233
        try {
234 13
            if ($this->generator->getFormatter($name)) {
235 9
                return true;
236
            }
237 8
        } catch (\InvalidArgumentException $exception) {
238 8
            return false;
239
        }
240
        return false;
241
    }
242
243
    /**
244
     * @param string $className
245
     * @return mixed
246
     * @throws \Exception
247
     */
248 1
    private function callCustomProvider($className)
249
    {
250
        /** @var \TildBJ\Seeder\Provider $providerClass */
251 1
        $providerClass = GeneralUtility::makeInstance($className, $this);
252 1
        if (!$providerClass instanceof \TildBJ\Seeder\Provider) {
253
            throw new \Exception(get_class($providerClass) . ' must implement ' . \TildBJ\Seeder\Provider::class);
254
        }
255 1
        return $providerClass->generate();
256
    }
257
258
    /**
259
     * @param string $name
260
     * @return bool
261
     */
262 14
    private function hasCustomProvider($name)
263
    {
264 14
        return (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['seeder']['provider'][$name]) && class_exists(
265 14
            $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['seeder']['provider'][$name]
266
        ));
267
    }
268
269
    /**
270
     * @param string $name
271
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
272
     * @throws NotFoundException
273
     * @throws \Exception
274
     */
275 14
    public function guessProviderName($name)
276
    {
277 14
        if (empty($name)) {
278 1
            throw new \TildBJ\Seeder\Provider\NotFoundException();
279
        }
280 13
        if (preg_match('/^is[_A-Z]/', $name)) {
281
            return 'boolean';
282
        }
283 13
        if (preg_match('/(_a|A)t$/', $name)) {
284
            return 'unixtime';
285
        }
286 13
        $name = strtolower($name);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $name. This often makes code more readable.
Loading history...
287 13
        $name = str_replace('_', '', $name);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $name. This often makes code more readable.
Loading history...
288 13
        if ($this->hasProvider($name)) {
289 8
            return $name;
290
        }
291 8
        switch ($name) {
292 8
            case 'mail':
293 8
            case 'emailaddress':
294
                return 'email';
295 8
            case 'phone':
296 8
            case 'telephone':
297 8
            case 'fax':
298 8
            case 'telnumber':
299
                return 'phonenumber';
300 8
            case 'town':
301
                return 'city';
302 8
            case 'zipcode':
303 8
            case 'zip':
304 2
                return 'postcode';
305 6
            case 'currency':
306
                return 'currencycode';
307 6
            case 'website':
308
                return 'url';
309 6
            case 'companyname':
310 6
            case 'employer':
311
                return 'company';
312 6
            case 'body':
313 6
            case 'bodytext':
314 5
            case 'summary':
315 5
            case 'teaser':
316 5
            case 'article':
317 5
            case 'description':
318 2
                return 'text';
319 4
            case 'middlename':
320
                return 'name';
321 4
            case 'uri':
322 4
            case 'www':
323
                return 'url';
324 4
            case 'image':
325
                return 'imageurl';
326 4
            case 'lastlogin':
327 4
            case 'crdate':
328 4
            case 'tstamp':
329 1
                return 'unixtime';
330 3
            case 'disable':
331
                return 'boolean';
332
            // Default provider is text:
333
            default:
334 3
                return null;
335
        }
336
    }
337
338
    /**
339
     * @param $name
340
     * @param $value
341
     * @return mixed
342
     * @throws NotFoundException
343
     * @throws \Exception
344
     */
345 2
    public function __call($name, $value)
346
    {
347 2
        $propertyArray = explode('get', $name);
348 2
        $property = strtolower($propertyArray[1]);
349 2
        if ($this->hasCustomProvider($property)) {
350 1
            return $this->callCustomProvider($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['seeder']['provider'][$property]);
351
        }
352
353 1
        return $this->get($property);
354
    }
355
}
356