Issues (55)

Helper/Wallets.php (3 issues)

1
<?php
2
3
namespace B2Binpay\Payment\Helper;
4
5
/**
6
 * Wallets value manipulation helper
7
 */
8
class Wallets
9
{
10
    /**
11
     * Check whether value is in form retrieved by _encodeArrayFieldValue()
12
     *
13
     * @param string|array $value
14
     * @return bool
15
     */
16
    protected function isEncodedArrayFieldValue($value)
17
    {
18
        if (!is_array($value)) {
19
            return false;
20
        }
21
        unset($value['__empty']);
22
        foreach ($value as $row) {
23
            if (!is_array($row)
24
                || !array_key_exists('wallet', $row)
25
                || !array_key_exists('currency', $row)
26
            ) {
27
                return false;
28
            }
29
        }
30
        return true;
31
    }
32
33
    /**
34
     * Encode value to be used in \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
35
     *
36
     * @param array $value
37
     * @return array
38
     */
39
    protected function encodeArrayFieldValue($value)
40
    {
41
        if (!is_array($value)) {
0 ignored issues
show
The condition is_array($value) is always true.
Loading history...
42
            return null;
43
        }
44
        $result = [];
45
        foreach ($value as $wallet => $currency) {
46
            $result[] = ['wallet' => $wallet, 'currency' => $currency];
47
        }
48
        return $result;
49
    }
50
51
    /**
52
     * Decode value from used in \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
53
     *
54
     * @param array $value
55
     * @return array
56
     */
57
    protected function decodeArrayFieldValue(array $value)
58
    {
59
        $result = [];
60
        unset($value['__empty']);
61
        foreach ($value as $row) {
62
            $wallet = $row['wallet'];
63
            $currency = $row['currency'];
64
            $result[$wallet] = $currency;
65
        }
66
        return $result;
67
    }
68
69
    /**
70
     * Make value readable by \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
71
     *
72
     * @param string|array $value
73
     * @return array
74
     */
75
    public function makeArrayFieldValue($value)
76
    {
77
        $value = json_decode($value, true);
0 ignored issues
show
It seems like $value can also be of type array; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        $value = json_decode(/** @scrutinizer ignore-type */ $value, true);
Loading history...
78
        if (!$this->isEncodedArrayFieldValue($value)) {
79
            $value = $this->encodeArrayFieldValue($value);
80
        }
81
        return $value;
82
    }
83
84
    /**
85
     * Make value ready for store
86
     *
87
     * @param string|array $value
88
     * @return string
89
     */
90
    public function makeStorableArrayFieldValue($value)
91
    {
92
        if ($this->isEncodedArrayFieldValue($value)) {
93
            $value = $this->decodeArrayFieldValue($value);
0 ignored issues
show
It seems like $value can also be of type string; however, parameter $value of B2Binpay\Payment\Helper\...decodeArrayFieldValue() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
            $value = $this->decodeArrayFieldValue(/** @scrutinizer ignore-type */ $value);
Loading history...
94
        }
95
        $value = json_encode($value);
96
        return $value;
97
    }
98
}
99