These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * HiPanel core package |
||
4 | * |
||
5 | * @link https://hipanel.com/ |
||
6 | * @package hipanel-core |
||
7 | * @license BSD-3-Clause |
||
8 | * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/) |
||
9 | */ |
||
10 | |||
11 | namespace hipanel\actions; |
||
12 | |||
13 | use hipanel\widgets\RequestState; |
||
14 | use yii\base\Action; |
||
0 ignored issues
–
show
|
|||
15 | |||
16 | class RequestStateAction extends Action |
||
17 | { |
||
18 | /** |
||
19 | * @var \hiqdev\hiart\ActiveRecord |
||
20 | */ |
||
21 | public $model; |
||
22 | |||
23 | public function init() |
||
24 | { |
||
25 | if (!$this->model) { |
||
26 | $this->model = $this->controller->newModel(); |
||
27 | } |
||
28 | } |
||
29 | |||
30 | public function run(array $ids) |
||
31 | { |
||
32 | $data = $this->model->find()->where(['id' => $ids, 'with_request' => true])->all(); |
||
33 | |||
34 | foreach ($data as $item) { |
||
35 | $res[$item->id] = [ |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$res was never initialized. Although not strictly required by PHP, it is generally a good practice to add $res = array(); before regardless.
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code. Let’s take a look at an example: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.
Loading history...
|
|||
36 | 'id' => $item->id, |
||
37 | 'name' => $item->name, |
||
38 | 'html' => RequestState::widget([ |
||
39 | 'module' => 'server', |
||
40 | 'model' => $item, |
||
41 | ]), |
||
42 | ]; |
||
43 | } |
||
44 | |||
45 | return $this->controller->renderJson($res); |
||
0 ignored issues
–
show
The variable
$res does not seem to be defined for all execution paths leading up to this point.
If you define a variable conditionally, it can happen that it is not defined for all execution paths. Let’s take a look at an example: function myFunction($a) {
switch ($a) {
case 'foo':
$x = 1;
break;
case 'bar':
$x = 2;
break;
}
// $x is potentially undefined here.
echo $x;
}
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined. Available Fixes
Loading history...
|
|||
46 | } |
||
47 | } |
||
48 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: