Passed
Push — master ( 64ec9f...625618 )
by Arthur
03:13 queued 10s
created

ResolveCasting::processCasting()   B

Complexity

Conditions 10
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 10.0751

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 19
ccs 10
cts 11
cp 0.9091
rs 7.6666
cc 10
nc 5
nop 2
crap 10.0751

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Larapie\Actions\Concerns;
4
5
use Illuminate\Support\Collection;
6
use Larapie\Actions\Attribute;
7
8
trait ResolveCasting
9
{
10 11
    protected function resolveAttributeCasting(array $data)
11
    {
12 11
        return collect($this->rules())
0 ignored issues
show
Bug introduced by
It seems like rules() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

12
        return collect($this->/** @scrutinizer ignore-call */ rules())
Loading history...
13
            ->filter(function ($rule) {
14 10
                return $rule instanceof Attribute;
15 11
            })
16
            ->intersectByKeys($data)->map(function (Attribute $attribute, $key) use ($data) {
17 7
                return $this->processCasting($attribute, $data[$key]);
18 11
            })->toArray();
19
    }
20
21 7
    private function processCasting(Attribute $attribute, $value)
22
    {
23 7
        if ($attribute->isNullable() && $value === null)
24 1
            return $value;
25
26 6
        if (($castFunction = $attribute->getCast()) !== null && is_callable($castFunction)) {
27 2
            return $castFunction($value);
28
        }
29
30 6
        if ((($type = $attribute->getCast()) !== null && is_string($type)) ||
31 6
            (is_string($type = $attribute->cast(null)) && in_array(strtolower($type), ['bool', 'boolean', 'string', 'double', 'float', 'int', 'integer', 'array', 'object']))) {
32 2
            return $this->castFromString($type, $value);
33
        }
34
35 4
        if (!($attribute->cast(null) instanceof Attribute)) {
0 ignored issues
show
introduced by
$attribute->cast(null) is always a sub-type of Larapie\Actions\Attribute.
Loading history...
36
            return $attribute->cast($value);
37
        }
38
39 4
        return $value;
40
    }
41
42 2
    private function castFromString(string $type, $value)
43
    {
44 2
        switch (strtolower($type)) {
45 2
            case 'boolean':
46 2
            case 'bool':
47 1
                return (bool)$value;
48 1
            case 'string':
49 1
                return (string)$value;
50
            case 'double':
51
                return (float)$value;
52
            case 'integer':
53
            case 'int':
54
                return (int)$value;
55
            case 'float':
56
                return (float)$value;
57
            case 'array':
58
                return (array)$value;
59
            case 'object':
60
                return (object)$value;
61
            default:
62
                throw new \RuntimeException('cast type not supported');
63
        }
64
    }
65
}
66