Completed
Push — master ( 6255e6...7fed75 )
by Maxime
09:47
created

IntGuesser::determineMaxValue()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 5
nop 1
1
<?php
2
3
namespace Knp\FriendlyContexts\Guesser;
4
5
class IntGuesser extends AbstractGuesser implements GuesserInterface
6
{
7
    public function supports(array $mapping)
8
    {
9
        $mapping = array_merge([ 'type' => null ], $mapping);
10
11
        return $mapping['type'] === 'integer';
12
    }
13
14
    public function transform($str, array $mapping = null)
15
    {
16
        return (int) round($str);
17
    }
18
19
    public function fake(array $mapping)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
20
    {
21
        $min = 0;
22
        $max = $this->determineMaxValue($mapping);
23
24
        return current($this->fakers)->fake('numberBetween', [$min, $max]);
25
    }
26
27
    /**
28
     * @param array $mapping
29
     *
30
     * @return int
31
     */
32
    private function determineMaxValue(array $mapping)
33
    {
34
        $defaultMax = 2000000000;
35
36
        $lengthUndefined = !isset($mapping['length']);
37
        if ($lengthUndefined) {
38
            return $defaultMax;
39
        }
40
41
        $lengthInvalid = $mapping['length'] < 1
42
            || $mapping['length'] >= strlen($defaultMax);
43
        if ($lengthInvalid) {
44
            return $defaultMax;
45
        }
46
47
        $maxValue = (int)str_repeat('9', $mapping['length']);
48
49
        return $maxValue;
50
    }
51
52
    public function getName()
53
    {
54
        return 'int';
55
    }
56
}
57