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 |
||
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) |
|
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) |
|
203 | |||
204 | /** |
||
205 | * @return array |
||
206 | */ |
||
207 | public function getSupportedProviders() |
||
211 | |||
212 | /** |
||
213 | * @param string $providerName |
||
214 | * @return mixed |
||
215 | */ |
||
216 | 9 | private function generate($providerName) |
|
220 | |||
221 | /** |
||
222 | * @param string $name |
||
223 | * @return bool |
||
224 | */ |
||
225 | 13 | private function hasProvider($name) |
|
242 | |||
243 | /** |
||
244 | * @param string $className |
||
245 | * @return mixed |
||
246 | * @throws \Exception |
||
247 | */ |
||
248 | 1 | private function callCustomProvider($className) |
|
257 | |||
258 | /** |
||
259 | * @param string $name |
||
260 | * @return bool |
||
261 | */ |
||
262 | 14 | private function hasCustomProvider($name) |
|
268 | |||
269 | /** |
||
270 | * @param string $name |
||
271 | * @return string |
||
|
|||
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); |
|
287 | 13 | $name = str_replace('_', '', $name); |
|
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) |
|
355 | } |
||
356 |
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.