Test Failed
Pull Request — master (#3)
by
unknown
17:36
created

Preferenceable::cast()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0145

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 14
cts 15
cp 0.9333
rs 8.5706
c 0
b 0
f 0
cc 7
nc 7
nop 2
crap 7.0145
1
<?php
2
3
namespace Timegridio\Concierge\Traits;
4
5
use Illuminate\Support\Facades\Cache;
6
use Timegridio\Concierge\Models\Preference;
7
8
trait Preferenceable
9
{
10
    /**
11
     * Preferences morph.
12
     *
13
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
14
     */
15 15
    public function preferences()
16
    {
17 15
        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...
18
    }
19
20
    /**
21
     * Get or set preference value.
22
     *
23
     * @param  mixed $key
24
     * @param  mixed $value
25
     * @param  string $type
26
     *
27
     * @return mixed  Value.
28
     */
29 14
    public function pref($key, $value = null, $type = 'string')
30
    {
31 14
        if (isset($value)) {
32 10
            $this->preferences()->updateOrCreate(['key' => $key], ['value' => $this->cast($value, $type),
33 10
                                                                   'type'  => $type, ]);
34
35 10
            Cache::put("{$this->slug}.'/'.{$key}", $value, 60);
0 ignored issues
show
Bug introduced by
The property slug does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36 10
            return $value;
37
        }
38
39 14
        if($value = Cache::get("{$this->slug}.'/'.{$key}"))
40
        {
41 10
            return $value;
42
        }
43
44 5
        if ($pref = $this->preferences()->forKey($key)->first()) {
45 1
            $value = $pref->value();
46 1
            $type = $pref->type();
47
        } else {
48 4
            $default = Preference::getDefault($this, $key);
49 4
            $value = $default->value();
50 4
            $type = $default->type();
51
        }
52
        
53 5
        Cache::put("{$this->slug}.'/'.{$key}", $value, 60);
54 5
        return $this->cast($value, $type);
55
    }
56
57
    /**
58
     * Cast value.
59
     *
60
     * @param  mixed $value
61
     * @param  mixed $type
62
     *
63
     * @return mixed Value.
64
     */
65 14
    private function cast($value, $type)
66
    {
67 14
        switch ($type) {
68 4
            case 'bool':
69 1
                return boolval($value);
70
                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...
71 4
            case 'int':
72 1
                return intval($value);
73
                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...
74 4
            case 'float':
75 1
                return floatval($value);
76
                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...
77 4
            case 'string':
78 5
                return $value;
79
                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...
80 4
            case 'array':
81 1
                if (is_array($value)) {
82 1
                    return serialize($value);
83
                } else {
84
                    return unserialize($value);
85
                }
86
                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...
87
            default:
88 5
                return $value;
89
        }
90
    }
91
}
92