Completed
Push — master ( 9d011a...df45e0 )
by Ariel
07:48
created

Preferenceable::cast()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 26
ccs 16
cts 16
cp 1
rs 6.7272
cc 7
eloc 22
nc 7
nop 2
crap 7
1
<?php
2
3
namespace Timegridio\Concierge\Traits;
4
5
use Timegridio\Concierge\Models\Preference;
6
7
trait Preferenceable
8
{
9
    /**
10
     * Preferences morph.
11
     *
12
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
13
     */
14 8
    public function preferences()
15
    {
16 8
        return $this->morphMany(Preference::class, 'preferenceable');
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
17
    }
18
19
    /**
20
     * Get or set preference value.
21
     *
22
     * @param  mixed $key
23
     * @param  mixed $value
24
     * @param  string $type
25
     *
26
     * @return mixed  Value.
27
     */
28 7
    public function pref($key, $value = null, $type = 'string')
29
    {
30 7
        if (isset($value)) {
31 6
            $this->preferences()->updateOrCreate(['key' => $key], ['value' => $this->cast($value, $type),
32 6
                                                                   'type'  => $type, ]);
33
34 6
            return $value;
35
        }
36
37 7
        if ($pref = $this->preferences()->forKey($key)->first()) {
38 6
            $value = $pref->value();
39 6
            $type = $pref->type();
40 6
        } else {
41 1
            $default = Preference::getDefault($this, $key);
42 1
            $value = $default->value();
43 1
            $type = $default->type();
44
        }
45
46 7
        return $this->cast($value, $type);
47
    }
48
49
    /**
50
     * Cast value.
51
     *
52
     * @param  mixed $value
53
     * @param  mixed $type
54
     *
55
     * @return mixed Value.
56
     */
57 7
    private function cast($value, $type)
58
    {
59
        switch ($type) {
60 7
            case 'bool':
61 1
                return boolval($value);
62
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
63 6
            case 'int':
64 1
                return intval($value);
65
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
66 5
            case 'float':
67 1
                return floatval($value);
68
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
69 4
            case 'string':
70 1
                return $value;
71
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
72 3
            case 'array':
73 1
                if (is_array($value)) {
74 1
                    return serialize($value);
75
                } else {
76 1
                    return unserialize($value);
77
                }
78
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
79 2
            default:
80 2
                return $value;
81 2
        }
82
    }
83
}
84