1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | /* |
||||
6 | * This file is part of Biurad opensource projects. |
||||
7 | * |
||||
8 | * PHP version 7.2 and above required |
||||
9 | * |
||||
10 | * @author Divine Niiquaye Ibok <[email protected]> |
||||
11 | * @copyright 2019 Biurad Group (https://biurad.com/) |
||||
12 | * @license https://opensource.org/licenses/BSD-3-Clause License |
||||
13 | * |
||||
14 | * For the full copyright and license information, please view the LICENSE |
||||
15 | * file that was distributed with this source code. |
||||
16 | */ |
||||
17 | |||||
18 | namespace Biurad\DependencyInjection\Exceptions; |
||||
19 | |||||
20 | use InvalidArgumentException; |
||||
21 | use Psr\Container\NotFoundExceptionInterface; |
||||
22 | use Throwable; |
||||
23 | |||||
24 | /** |
||||
25 | * This exception is thrown when a non-existent parameter is used. |
||||
26 | * |
||||
27 | * @author Fabien Potencier <[email protected]> |
||||
28 | */ |
||||
29 | class ParameterNotFoundException extends InvalidArgumentException implements NotFoundExceptionInterface |
||||
30 | { |
||||
31 | /** @var string */ |
||||
32 | private $key; |
||||
33 | |||||
34 | /** @var array */ |
||||
35 | private $alternatives; |
||||
36 | |||||
37 | /** @var array */ |
||||
38 | private $nonNestedAlternative; |
||||
39 | |||||
40 | /** |
||||
41 | * @param string $key The requested parameter key |
||||
42 | * @param Throwable $previous The previous exception |
||||
43 | * @param string[] $alternatives Some parameter name alternatives |
||||
44 | * @param null|string $nonNestedAlternative The alternative parameter name when the user |
||||
45 | * expected dot notation for nested parameters |
||||
46 | */ |
||||
47 | public function __construct( |
||||
48 | string $key, |
||||
49 | Throwable $previous = null, |
||||
50 | array $alternatives = [], |
||||
51 | string $nonNestedAlternative = null |
||||
52 | ) { |
||||
53 | $this->key = $key; |
||||
54 | $this->alternatives = $alternatives; |
||||
55 | $this->nonNestedAlternative = $nonNestedAlternative; |
||||
0 ignored issues
–
show
|
|||||
56 | |||||
57 | parent::__construct('', 0, $previous); |
||||
58 | |||||
59 | $this->updateRepr(); |
||||
60 | } |
||||
61 | |||||
62 | public function updateRepr(): void |
||||
63 | { |
||||
64 | $this->message = \sprintf('You have requested a non-existent parameter "%s".', $this->key); |
||||
65 | |||||
66 | if ($this->alternatives) { |
||||
0 ignored issues
–
show
The expression
$this->alternatives of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||||
67 | if (1 == \count($this->alternatives)) { |
||||
68 | $this->message .= ' Did you mean this: "'; |
||||
69 | } else { |
||||
70 | $this->message .= ' Did you mean one of these: "'; |
||||
71 | } |
||||
72 | $this->message .= \implode('", "', $this->alternatives) . '"?'; |
||||
73 | } elseif (null !== $this->nonNestedAlternative) { |
||||
74 | $this->message .= ' You cannot access nested array items, do you want to inject "' . |
||||
75 | $this->nonNestedAlternative . '" instead?'; |
||||
0 ignored issues
–
show
Are you sure
$this->nonNestedAlternative of type array can be used in concatenation ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
76 | } |
||||
77 | } |
||||
78 | |||||
79 | public function getKey() |
||||
80 | { |
||||
81 | return $this->key; |
||||
82 | } |
||||
83 | } |
||||
84 |
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.