These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Peridot\Leo\Matcher; |
||
3 | |||
4 | use Peridot\Leo\Matcher\Template\ArrayTemplate; |
||
5 | use Peridot\Leo\Matcher\Template\TemplateInterface; |
||
6 | |||
7 | /** |
||
8 | * RangeMatcher matches a number or the length of a countable |
||
9 | * between a lower and upper bound - both of which are inclusive. |
||
10 | * |
||
11 | * @package Peridot\Leo\Matcher |
||
12 | */ |
||
13 | class RangeMatcher extends CountableMatcher |
||
14 | { |
||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | protected $lowerBound; |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $upperBound; |
||
24 | |||
25 | /** |
||
26 | * @param int|float|double $lower |
||
27 | * @param int|float|double $upper |
||
28 | */ |
||
29 | public function __construct($lower, $upper) |
||
30 | { |
||
31 | $this->setLowerBound($lower); |
||
32 | $this->setUpperBound($upper); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * {@inheritdoc} |
||
37 | * |
||
38 | * @return TemplateInterface |
||
39 | */ |
||
40 | public function getDefaultCountableTemplate() |
||
41 | { |
||
42 | return new ArrayTemplate([ |
||
43 | 'default' => "Expected {{actual}} to be within {$this->lowerBound}..{$this->upperBound}", |
||
44 | 'negated' => "Expected {{actual}} to not be within {$this->lowerBound}..{$this->upperBound}" |
||
45 | ]); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | * |
||
51 | * @return TemplateInterface |
||
52 | */ |
||
53 | public function getDefaultTemplate() |
||
54 | { |
||
55 | return new ArrayTemplate([ |
||
56 | 'default' => "Expected {{actual}} to be within {$this->lowerBound}..{$this->upperBound}", |
||
57 | 'negated' => "Expected {{actual}} to not be within {$this->lowerBound}..{$this->upperBound}" |
||
58 | ]); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Set the lower bound of the range matcher. |
||
63 | * |
||
64 | * @param int $lowerBound |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function setLowerBound($lowerBound) |
||
68 | { |
||
69 | if (!is_numeric($lowerBound)) { |
||
70 | throw new \InvalidArgumentException("Lower bound must be a numeric value"); |
||
71 | } |
||
72 | |||
73 | $this->lowerBound = $lowerBound; |
||
0 ignored issues
–
show
|
|||
74 | return $this; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Set the upper bound of the range matcher. |
||
79 | * |
||
80 | * @param int $upperBound |
||
81 | * @return $this |
||
82 | */ |
||
83 | public function setUpperBound($upperBound) |
||
84 | { |
||
85 | if (!is_numeric($upperBound)) { |
||
86 | throw new \InvalidArgumentException("Upper bound must be a numeric value"); |
||
87 | } |
||
88 | |||
89 | $this->upperBound = $upperBound; |
||
0 ignored issues
–
show
It seems like
$upperBound can also be of type double or string . However, the property $upperBound is declared as type integer . 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 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 | return $this; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Return the lower bound of the range matcher. |
||
95 | * |
||
96 | * @return int |
||
97 | */ |
||
98 | public function getLowerBound() |
||
99 | { |
||
100 | return $this->lowerBound; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Return the upper bound of the range matcher. |
||
105 | * |
||
106 | * @return int |
||
107 | */ |
||
108 | public function getUpperBound() |
||
109 | { |
||
110 | return $this->upperBound; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Determine if the number is between an upper and lower bound (inclusive). |
||
115 | * |
||
116 | * @param $number |
||
117 | * @return bool |
||
118 | */ |
||
119 | protected function matchNumeric($number) |
||
120 | { |
||
121 | return $number <= $this->upperBound && $number >= $this->lowerBound; |
||
122 | } |
||
123 | } |
||
124 |
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 theid
property of an instance of theAccount
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.