HasAttributes::fromJson()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Eloquent\Concerns;
6
7
trait HasAttributes
8
{
9
    /**
10
     * Determine whether a value is JSON castable for inbound manipulation.
11
     *
12
     * @param  string  $key
13
     * @return bool
14
     */
15 44
    protected function isJsonCastable($key)
16
    {
17 44
        return $this->hasCast($key, ['encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object']);
0 ignored issues
show
Bug introduced by
It seems like hasCast() 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

17
        return $this->/** @scrutinizer ignore-call */ hasCast($key, ['encrypted:array', 'encrypted:collection', 'encrypted:json', 'encrypted:object']);
Loading history...
18
    }
19
20
    /**
21
     * Decode the given JSON back into an array or object.
22
     *
23
     * @param  string  $value
24
     * @param  bool  $asObject
25
     * @return mixed
26
     *
27
     * @SuppressWarnings("PHPMD.BooleanArgumentFlag")
28
     */
29 5
    public function fromJson($value, $asObject = false)
30
    {
31
        // As data is stored as json in ArangoDB we don't have to decode it here.
32 5
        if ($asObject) {
33 1
            return (object) $value;
34
        }
35
36
        // Recursively cast objects within the value to arrays
37 4
        return mapObjectToArray($value);
38
    }
39
}
40