AvailableMerchants   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 31
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 21 6
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
Coding Style Comprehensibility introduced by
$out was never initialized. Although not strictly required by PHP, it is generally a good practice to add $out = 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 $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar 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.

Loading history...
41
        $out[] = implode(',&nbsp; ', $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