Passed
Push — master ( 2915de...908b7b )
by Arthur
03:36
created

ResolveCasting::castFromString()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 13.1494

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 10
eloc 19
c 1
b 1
f 0
nc 10
nop 2
dl 0
loc 21
ccs 13
cts 19
cp 0.6842
crap 13.1494
rs 7.6666

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 Larapie\Actions\Attribute;
6
7
trait ResolveCasting
8
{
9 68
    protected function resolveAttributeCasting(array $data)
10
    {
11 68
        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

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