FilmSpeed::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace FilmTools\FilmSpeed;
3
4
class FilmSpeed extends FilmSpeedAbstract implements FilmSpeedInterface
5
{
6
7
    /**
8
     * @param int|float $asa ASA value
9
     * @param int|float $din DIN value
10
     */
11 28
    public function __construct( $asa, $din)
12
    {
13 28
        if (is_numeric($asa)):
14 28
            $this->asa = $asa;
0 ignored issues
show
Documentation Bug introduced by
It seems like $asa can also be of type integer or string. 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...
15
        else:
16
            throw new \InvalidArgumentException("Numeric ASA value expected");
17
        endif;
18
19 28
        if (is_numeric($din)):
20 28
            $this->din = $din;
0 ignored issues
show
Documentation Bug introduced by
It seems like $din can also be of type integer or string. 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...
21
        else:
22
            throw new \InvalidArgumentException("Numeric DIN value expected");
23
        endif;
24 28
    }
25
26
27
    /**
28
     * Returns a string representation, for example: "ISO 200/24°"
29
     * @return string [description]
30
     */
31
    public function __toString()
32
    {
33
        return $this->getIso( "rounded" );
0 ignored issues
show
Documentation introduced by
'rounded' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
34
    }
35
36
37
    /**
38
     * @param bool $rounded Optional: round value; default is FALSE
39
     * @inheritDoc
40
     */
41 44
    public function getDin( $rounded = false) : float
42
    {
43 44
        $din = parent::getDin();
44 44
        return $rounded ? round($din) : $din;
45
    }
46
47
48
    /**
49
     * @param bool $rounded Optional: round value; default is FALSE
50
     * @inheritDoc
51
     */
52 44
    public function getAsa( $rounded = false) : float
53
    {
54 44
        $asa = parent::getAsa();
55 44
        return $rounded ? round($asa) : $asa;
56
    }
57
58
59
    /**
60
     * @param bool $rounded Optional: round value; default is TRUE
61
     * @inheritDoc
62
     */
63 36
    public function getIso( $rounded = true ) : string
64
    {
65 36
        $din = $this->getDin( $rounded );
66 36
        $asa = $this->getAsa( $rounded );
67
68 36
        return sprintf("ISO %s/%s°", $asa, $din);
69
    }
70
}
71