AsaFilmSpeed::__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
class AsaFilmSpeed extends FilmSpeed implements FilmSpeedInterface
5
{
6
7
    const ASA_DIN_MAP = array(
8
               "0.63"   => "−1",
9
               "0.75"   => "0",
10
               "1"      => "1",
11
               "1.25"   => "2",
12
               "1.5"    => "3",
13
               "2"      => "4",
14
               "2.5"    => "5",
15
               "3"      => "6",
16
               "4"      => "7",
17
               "5"      => "8",
18
               "6"      => "9",
19
               "8"      => "10",
20
              "10"      => "11",
21
              "12"      => "12",
22
              "16"      => "13",
23
              "20"      => "14",
24
              "25"      => "15",
25
              "32"      => "16",
26
              "40"      => "17",
27
              "50"      => "18",
28
              "64"      => "19",
29
              "80"      => "20",
30
             "100"      => "21",
31
             "125"      => "22",
32
             "160"      => "23",
33
             "200"      => "24",
34
             "250"      => "25",
35
             "320"      => "26",
36
             "400"      => "27",
37
             "500"      => "28",
38
             "640"      => "29",
39
             "800"      => "30",
40
            "1000"      => "31",
41
            "1250"      => "32",
42
            "1600"      => "33",
43
            "2000"      => "34",
44
            "2500"      => "35",
45
            "3200"      => "36",
46
            "4000"      => "37",
47
            "5000"      => "38",
48
            "6400"      => "39",
49
            "8000"      => "40",
50
           "10000"      => "41",
51
           "12500"      => "42",
52
           "16000"      => "43",
53
           "20000"      => "44",
54
           "25000"      => "45",
55
           "32000"      => "46",
56
           "40000"      => "47",
57
           "50000"      => "48",
58
           "64000"      => "49",
59
           "80000"      => "50",
60
          "100000"      => "51",
61
          "125000"      => "52",
62
          "160000"      => "53",
63
          "200000"      => "54",
64
          "250000"      => "55",
65
          "320000"      => "56",
66
          "400000"      => "57",
67
          "500000"      => "58",
68
          "640000"      => "59",
69
          "800000"      => "60",
70
         "1000000"      => "61",
71
         "1250000"      => "62",
72
         "1600000"      => "63",
73
         "2000000"      => "64",
74
         "2500000"      => "65",
75
         "3200000"      => "66",
76
         "4000000"      => "67"
77
    );
78
79
80
    /**
81
     * Requires an ASA value.
82
     * The related DIN ° number will be created internally.
83
     *
84
     * @param float $asa Film speed ASA value.
85
     */
86 12
    public function __construct( float $asa )
87
    {
88 12
        $this->asa = $asa;
89 12
        $this->din = $this->lookupDin( $asa ) ?: $this->calculateDin( $asa );
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->lookupDin($asa) ?...his->calculateDin($asa) can also be of type integer. However, the property $din 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...
90 12
    }
91
92
93
94
    /**
95
     * @param  mixed $asa
96
     * @return int|FALSE
97
     */
98 12
    protected function lookupDin( $asa ) {
99 12
        $key = (string) round($asa);
100 12
        return static::ASA_DIN_MAP[ $key ] ?? false;
101
    }
102
103
    /**
104
     * @param  mixed $asa
105
     * @return float
106
     */
107
    protected function calculateDin( $asa ) : float {
108
        // DIN = 21° + 10° · log(ASA/100)
109
        $din = 21 + 10 * log10( $asa / 100);
110
        return round($din);
111
    }
112
}
113