AbstractMeta::getClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 5
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
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