These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | /** |
||
5 | * This file is part of the Ray.Di package. |
||
6 | * |
||
7 | * @license http://opensource.org/licenses/MIT MIT |
||
8 | */ |
||
9 | namespace Ray\Di\Exception; |
||
10 | |||
11 | class Unbound extends \LogicException implements ExceptionInterface |
||
12 | { |
||
13 | 3 | public function __toString() |
|
14 | { |
||
15 | 3 | $messages = [sprintf("- %s\n", $this->getMessage())]; |
|
16 | 3 | $e = $this->getPrevious(); |
|
17 | 3 | if (! $e instanceof \Exception) { |
|
18 | 1 | return $this->getMainMessage($this); |
|
0 ignored issues
–
show
|
|||
19 | } |
||
20 | 2 | if ($e instanceof self) { |
|
21 | 1 | return $this->buildMessage($e, $messages) . "\n" . $e->getTraceAsString(); |
|
0 ignored issues
–
show
$e is of type object<Ray\Di\Exception\Unbound> , but the function expects a object<self> .
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);
![]() |
|||
22 | } |
||
23 | |||
24 | 1 | return parent::__toString(); |
|
25 | } |
||
26 | |||
27 | 1 | private function buildMessage(self $e, array $msg) : string |
|
28 | { |
||
29 | 1 | $lastE = $e; |
|
30 | 1 | while ($e instanceof self) { |
|
31 | 1 | $msg[] = sprintf("- %s\n", $e->getMessage()); |
|
32 | 1 | $lastE = $e; |
|
33 | 1 | $e = $e->getPrevious(); |
|
34 | } |
||
35 | 1 | array_pop($msg); |
|
36 | 1 | $msg = array_reverse($msg); |
|
37 | |||
38 | 1 | return $this->getMainMessage($lastE) . implode('', $msg); |
|
0 ignored issues
–
show
It seems like
$lastE defined by $e on line 32 can also be of type object<Ray\Di\Exception\Unbound> ; however, Ray\Di\Exception\Unbound::getMainMessage() does only seem to accept object<self> , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
39 | } |
||
40 | |||
41 | 2 | private function getMainMessage(self $e) |
|
42 | { |
||
43 | 2 | return sprintf( |
|
44 | 2 | "exception '%s' with message '%s'\n", |
|
45 | 2 | \get_class($e), |
|
46 | 2 | $e->getMessage() |
|
47 | ); |
||
48 | } |
||
49 | } |
||
50 |
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: