HasRoundedValue::precision()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\LaravelMetrics\Metrics\Concerns;
6
7
/**
8
 * Trait     HasRoundedValue
9
 *
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
trait HasRoundedValue
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
19
    /**
20
     * Rounding precision.
21
     *
22
     * @var int
23
     */
24
    public $roundingPrecision = 0;
25
26
    /**
27
     * Rounding mode.
28
     *
29
     * @var int
30
     */
31
    public $roundingMode = PHP_ROUND_HALF_UP;
32
33
    /* -----------------------------------------------------------------
34
     |  Getters & Setters
35
     | -----------------------------------------------------------------
36
     */
37
38
    /**
39
     * Set the precision level used when rounding the value.
40
     *
41
     * @param  int  $precision
42
     * @param  int  $mode
43
     *
44
     * @return $this
45
     */
46 4
    public function precision($precision = 0, $mode = PHP_ROUND_HALF_UP)
47
    {
48 4
        $this->roundingPrecision = $precision;
49 4
        $this->roundingMode      = $mode;
50
51 4
        return $this;
52
    }
53
54
    /* -----------------------------------------------------------------
55
     |  Main Methods
56
     | -----------------------------------------------------------------
57
     */
58
59
    /**
60
     * Round the value.
61
     *
62
     * @param  int|float  $value
63
     *
64
     * @return float
65
     */
66 64
    public function roundValue($value)
67
    {
68 64
        return round($value, $this->roundingPrecision, $this->roundingMode);
69
    }
70
}
71