Completed
Push — master ( 7fed75...5e261f )
by Maxime
10:32 queued 08:08
created

DecimalGuesser::determineMaxNumOfDecimals()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace Knp\FriendlyContexts\Guesser;
4
5
class DecimalGuesser extends AbstractGuesser implements GuesserInterface
6
{
7
    public function supports(array $mapping)
8
    {
9
        $mapping = array_merge([ 'type' => null ], $mapping);
10
11
        return in_array($mapping['type'], [ 'decimal', 'float' ]);
12
    }
13
14
    public function transform($str, array $mapping = null)
15
    {
16
        return (float) $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
        $maxNumOfDecimals = $this->determineMaxNumOfDecimals($mapping);
22
        $min = 0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
23
        $max = $this->determineMaxValue($mapping);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
24
25
        return current($this->fakers)->fake('randomFloat', [$maxNumOfDecimals, $min, $max]);
26
    }
27
28
    /**
29
     * @param array $mapping
30
     *
31
     * @return int|null
32
     */
33
    private function determineMaxNumOfDecimals(array $mapping)
34
    {
35
        if (isset($mapping['scale'])) {
36
            return $mapping['scale'];
37
        }
38
39
        if (isset($mapping['precision'])) {
40
            return 0;
41
        }
42
43
        return null;
44
    }
45
46
    /**
47
     * @param array $mapping
48
     *
49
     * @return float|null
50
     */
51
    private function determineMaxValue(array $mapping)
52
    {
53
        if (!isset($mapping['precision']) && !isset($mapping['scale'])) {
54
            return null;
55
        }
56
57
        $scale = isset($mapping['scale']) ? (int)$mapping['scale'] : 0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
58
        $precision = isset($mapping['precision']) ? (int)$mapping['precision'] : 0;
59
        if ($precision < $scale) {
60
            $precision = $scale;
61
        }
62
63
        $maxValueStr = (int)str_repeat('9', $precision);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
64
        $integerPartLength = $precision - $scale;
65
66
        $fractionalPart = substr($maxValueStr, 0, $integerPartLength);
67
        $integerPart = substr($maxValueStr, $integerPartLength);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
68
        $maxValue = (float)"$fractionalPart.$integerPart";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $fractionalPart instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $integerPart instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
69
70
        return $maxValue;
71
    }
72
73
    public function getName()
74
    {
75
        return 'decimal';
76
    }
77
}
78