DinFilmSpeed::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace FilmTools\FilmSpeed;
3
4
use function FilmTools\MRounder\mround;
5
6
class DinFilmSpeed extends FilmSpeed implements FilmSpeedInterface
7
{
8
9
10
    const DIN_ASA_MAP = array(
11
        "−1" => "0.63",
12
        "0" => "0.75",
13
        "1" => "1",
14
        "2" => "1.25",
15
        "3" => "1.5",
16
        "4" => "2",
17
        "5" => "2.5",
18
        "6" => "3",
19
        "7" => "4",
20
        "8" => "5",
21
        "9" => "6",
22
        "10" => "8",
23
        "11" => "10",
24
        "12" => "12",
25
        "13" => "16",
26
        "14" => "20",
27
        "15" => "25",
28
        "16" => "32",
29
        "17" => "40",
30
        "18" => "50",
31
        "19" => "64",
32
        "20" => "80",
33
        "21" => "100",
34
        "22" => "125",
35
        "23" => "160",
36
        "24" => "200",
37
        "25" => "250",
38
        "26" => "320",
39
        "27" => "400",
40
        "28" => "500",
41
        "29" => "640",
42
        "30" => "800",
43
        "31" => "1000",
44
        "32" => "1250",
45
        "33" => "1600",
46
        "34" => "2000",
47
        "35" => "2500",
48
        "36" => "3200",
49
        "37" => "4000",
50
        "38" => "5000",
51
        "39" => "6400",
52
        "40" => "8000",
53
        "41" => "10000",
54
        "42" => "12500",
55
        "43" => "16000",
56
        "44" => "20000",
57
        "45" => "25000",
58
        "46" => "32000",
59
        "47" => "40000",
60
        "48" => "50000",
61
        "49" => "64000",
62
        "50" => "80000",
63
        "51" => "100000",
64
        "52" => "125000",
65
        "53" => "160000",
66
        "54" => "200000",
67
        "55" => "250000",
68
        "56" => "320000",
69
        "57" => "400000",
70
        "58" => "500000",
71
        "59" => "640000",
72
        "60" => "800000",
73
        "61" => "1000000",
74
        "62" => "1250000",
75
        "63" => "1600000",
76
        "64" => "2000000",
77
        "65" => "2500000",
78
        "66" => "3200000",
79
        "67" => "4000000"
80
    );
81
82
83
    /**
84
     * Requires a DIN ° value.
85
     * The related ASA value will be created internally.
86
     *
87
     * @param float $din Film speed DIN value.
88
     */
89 12
    public function __construct( float $din )
90
    {
91 12
        $this->din = $din;
92 12
        $this->asa = $this->lookupAsa( $din ) ?: $this->calculateAsa( $din );
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->lookupAsa($din) ?...his->calculateAsa($din) can also be of type integer. However, the property $asa is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
93 12
    }
94
95
96
    /**
97
     * @param  mixed $din
98
     * @return int|FALSE
99
     */
100 12
    protected function lookupAsa( $din ) {
101 12
        $key = (string) round($din);
102 12
        return static::DIN_ASA_MAP[ $key ] ?? false;
103
    }
104
105
106
    /**
107
     * @param  mixed $din
108
     * @return float
109
     */
110
    protected function calculateAsa( $din ) {
111
        return mround(10**(($din - 1)/10) / 10, 0.5) * 10;
112
    }
113
}
114