Completed
Push — master ( 7ed496...ae90dd )
by Luke
28s queued 10s
created

Mula::expectsCurrencyAndAmountInSameColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Lukeraymonddowning\Mula\Casts;
4
5
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
6
use Illuminate\Support\Str;
7
use Lukeraymonddowning\Mula\Facades;
8
9
class Mula implements CastsAttributes
10
{
11
    protected $columns = [];
12
13
    public function __construct(...$columns)
14
    {
15
        $this->columns = $columns;
16
    }
17
18
    public function get($model, string $key, $money, array $attributes)
19
    {
20
        return Facades\Mula::create(...$this->getMonetaryValues($money, $attributes));
0 ignored issues
show
Documentation introduced by
$this->getMonetaryValues($money, $attributes) is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
21
    }
22
23
    protected function getMonetaryValues($money, $attributes)
24
    {
25
        return $this->expectsCurrencyAndAmountInSameColumn()
26
            ? explode('|', $money, 2)
27
            : [$attributes[$this->columns[0]], $attributes[$this->columns[1]]];
28
    }
29
30
    protected function expectsCurrencyAndAmountInSameColumn()
31
    {
32
        return empty($this->columns);
33
    }
34
35
    public function set($model, string $key, $money, array $attributes)
36
    {
37
        if ($this->expectsCurrencyAndAmountInSameColumn()) {
38
            return "{$money->value()}|{$money->currency()}";
39
        }
40
41
        return [
42
            $this->columns[0] => $money->value(),
43
            $this->columns[1] => $money->currency()
44
        ];
45
    }
46
}
47