Passed
Push — chore/add-feature-tests ( 36c2df...e56562 )
by Bas
04:12 queued 19s
created

AsArrayObject   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 52
ccs 8
cts 10
cp 0.8
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ get() 0 13 4
A hp$0 ➔ castUsing() 0 42 1
A hp$0 ➔ set() 0 3 1
A hp$0 ➔ serialize() 0 3 1
castUsing() 0 42 ?
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Eloquent\Casts;
6
7
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
8
use Illuminate\Database\Eloquent\Casts\ArrayObject;
9
use Illuminate\Database\Eloquent\Casts\AsArrayObject as IlluminateAsArrayObject;
10
use LaravelFreelancerNL\Aranguent\Eloquent\Model;
11
12
class AsArrayObject extends IlluminateAsArrayObject
13
{
14
    /**
15
     * Get the caster class to use when casting from / to this cast target.
16
     *
17
     * @param  array<array-key, mixed>  $arguments
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
18
     * @return \Illuminate\Contracts\Database\Eloquent\CastsAttributes<\Illuminate\Database\Eloquent\Casts\ArrayObject<array-key, mixed>, iterable<array-key, mixed>>
19
     *
20
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
21
     */
22 1
    public static function castUsing(array $arguments)
23
    {
24 1
        return new class () implements CastsAttributes {
25
            public function get($model, $key, $value, $attributes)
26
            {
27 1
                if (! isset($attributes[$key])) {
28
                    return;
29
                }
30
31 1
                $data = $attributes[$key];
32
33 1
                if (is_object($data)) {
34 1
                    $data = (array) $data;
35
                }
36
37 1
                return is_array($data) ? new ArrayObject($data, ArrayObject::ARRAY_AS_PROPS) : null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return is_array($data) ?...:ARRAY_AS_PROPS) : null also could return the type Illuminate\Database\Eloquent\Casts\ArrayObject which is incompatible with the return type mandated by Illuminate\Contracts\Dat...\CastsAttributes::get() of Illuminate\Contracts\Database\Eloquent\TGet|null.
Loading history...
38
            }
39
40
            /**
41
             * @param Model $model
42
             * @param string $key
43
             * @param mixed $value
44
             * @param mixed[] $attributes
45
             * @return mixed[]
46
             *
47
             * @SuppressWarnings(PHPMD.UnusedFormalParameter)
48
             */
49
            public function set($model, $key, $value, $attributes)
50
            {
51 1
                return [$key => $value];
52
            }
53
54
            /**
55
             * @param Model $model
56
             * @param string $key
57
             * @param mixed $value
58
             * @param mixed[] $attributes
59
             * @return mixed
60
             */
61
            public function serialize($model, string $key, $value, array $attributes)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

61
            public function serialize($model, /** @scrutinizer ignore-unused */ string $key, $value, array $attributes)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

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

61
            public function serialize(/** @scrutinizer ignore-unused */ $model, string $key, $value, array $attributes)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $attributes is not used and could be removed. ( Ignorable by Annotation )

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

61
            public function serialize($model, string $key, $value, /** @scrutinizer ignore-unused */ array $attributes)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
            {
63
                return $value->getArrayCopy();
64
            }
65 1
        };
66
    }
67
}
68