Passed
Push — master ( 29344f...675168 )
by Paweł
03:21 queued 12s
created

FakerValueResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
/**
3
 * Copyright (c) 2020.
4
 * @author Paweł Antosiak <[email protected]>
5
 */
6
7
declare(strict_types=1);
8
9
namespace Gorynych\Testing;
10
11
use Faker\Generator;
12
use Webmozart\Assert\Assert;
13
14
final class FakerValueResolver
15
{
16
    private Generator $faker;
17
18 5
    public function __construct(Generator $faker)
19
    {
20 5
        $this->faker = $faker;
21 5
    }
22
23
    /**
24
     * @return bool|int|string|null
25
     */
26 5
    public function resolvePropertyValue(\ReflectionProperty $property)
27
    {
28 5
        $propertyType = $property->getType();
0 ignored issues
show
Bug introduced by
The method getType() does not exist on ReflectionProperty. ( Ignorable by Annotation )

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

28
        /** @scrutinizer ignore-call */ 
29
        $propertyType = $property->getType();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29 5
        Assert::isInstanceOf($propertyType, \ReflectionNamedType::class);
30
31 5
        switch ($propertyType->getName()) {
32 5
            case 'string':
33
                try {
34 2
                    $value = $this->faker->{$property->getName()};
35 2
                } catch (\Throwable $t) {
36 2
                    $value = $this->faker->word;
37
                }
38 2
                break;
39 4
            case 'int':
40 2
                $value = $this->faker->randomDigit;
41 2
                break;
42 3
            case 'bool':
43 2
                $value = $this->faker->boolean;
44 2
                break;
45 2
            case 'DateTime':
46 2
                $value = $this->faker->dateTime->format('Y-m-d H:i');
47 2
                break;
48
            default:
49
                $value = null;
50
        }
51
52 5
        return $value;
53
    }
54
}
55