These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Finance module for HiPanel |
||
4 | * |
||
5 | * @link https://github.com/hiqdev/hipanel-module-finance |
||
6 | * @package hipanel-module-finance |
||
7 | * @license BSD-3-Clause |
||
8 | * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
||
9 | */ |
||
10 | |||
11 | namespace hipanel\modules\finance\widgets; |
||
12 | |||
13 | use hipanel\modules\finance\models\Merchant; |
||
14 | use Yii; |
||
15 | use yii\base\Widget; |
||
16 | use yii\helpers\Html; |
||
17 | |||
18 | class AvailableMerchants extends Widget |
||
19 | { |
||
20 | /** |
||
21 | * @var Merchant[] |
||
22 | */ |
||
23 | public $merchants; |
||
24 | |||
25 | const PAYPAL_PROCESSING = 'PayPal'; |
||
26 | |||
27 | public function run() |
||
28 | { |
||
29 | if ($this->merchants === null && Yii::$app->hasModule('merchant') && Yii::$app->user->can('deposit')) { |
||
30 | $this->merchants = Yii::$app->getModule('merchant')->getPurchaseRequestCollection()->getItems(); |
||
31 | } |
||
32 | |||
33 | if (empty($this->merchants)) { |
||
34 | return ''; |
||
35 | } |
||
36 | |||
37 | $list = array_map(function ($merchant) { return $merchant->label; }, $this->merchants); |
||
38 | $htmlList = array_unique(array_map(function ($merchant) { return Html::tag('b', $merchant); }, $list)); |
||
39 | |||
40 | $out[] = Yii::t('hipanel:finance', 'We accept the following automatic payment methods') . ':'; |
||
0 ignored issues
–
show
|
|||
41 | $out[] = implode(', ', $htmlList); |
||
42 | if (\in_array(self::PAYPAL_PROCESSING, $list, true)) { |
||
43 | $out[] = Yii::t('hipanel:finance', 'as well as PayPal payments from your Visa and MasterCard'); |
||
44 | } |
||
45 | |||
46 | return implode(' ', $out); |
||
47 | } |
||
48 | } |
||
49 |
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:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.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.