AbstractMeta   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 38
ccs 20
cts 20
cp 1
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A newInstance() 0 3 1
A get() 0 12 3
A set() 0 3 1
A getClass() 0 8 2
1
<?php
2
3
4
namespace JsonFieldCast\Casts;
5
6
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
7
8
/**
9
 * @psalm-suppress MissingTemplateParam
10
 */
11
abstract class AbstractMeta implements CastsAttributes
12
{
13 70
    public function get($model, string $key, $value, array $attributes): \JsonFieldCast\Json\AbstractMeta|\JsonFieldCast\Json\ArrayOfJsonObjectsField
14
    {
15 70
        $data = is_string($value) ? json_decode($value, true): $value;
16 70
        $data = is_array($data) ? $data : [];
17
18 70
        return $this->newInstance(
19 70
            $this->getClass($model, $data, $key, $value, $attributes),
20 70
            $model,
21 70
            $data,
22 70
            $key,
23 70
            $value,
24 70
            $attributes
25 70
        );
26
    }
27
28 69
    public function set($model, string $key, $value, array $attributes): string
29
    {
30 69
        return json_encode($value);
31
    }
32
33 70
    protected function getClass($model, array $data = [], string $key = '', mixed $value = null, array $attributes = []): string
0 ignored issues
show
Unused Code introduced by
The parameter $value 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

33
    protected function getClass($model, array $data = [], string $key = '', /** @scrutinizer ignore-unused */ mixed $value = null, array $attributes = []): string

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 $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

33
    protected function getClass($model, array $data = [], /** @scrutinizer ignore-unused */ string $key = '', mixed $value = null, array $attributes = []): string

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

33
    protected function getClass($model, array $data = [], string $key = '', mixed $value = null, /** @scrutinizer ignore-unused */ array $attributes = []): string

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...
34
    {
35 70
        $class = $this->metaClass();
36 70
        if (method_exists($class, 'getCastableClassByModel')) {
37 1
            $class = $class::getCastableClassByModel($model, $data);
38
        }
39
40 70
        return $class;
41
    }
42
43 70
    protected function newInstance(string $class, $model, array $data = [], string $key = '', mixed $value = null, array $attributes = [])
0 ignored issues
show
Unused Code introduced by
The parameter $value 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

43
    protected function newInstance(string $class, $model, array $data = [], string $key = '', /** @scrutinizer ignore-unused */ mixed $value = null, 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

43
    protected function newInstance(string $class, $model, array $data = [], string $key = '', mixed $value = null, /** @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...
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

43
    protected function newInstance(string $class, $model, array $data = [], /** @scrutinizer ignore-unused */ string $key = '', mixed $value = null, 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...
44
    {
45 70
        return new $class($model, $data);
46
    }
47
48
    abstract protected function metaClass(): string;
49
}
50