AnotherValueObject   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 21
c 1
b 1
f 1
dl 0
loc 63
rs 10
wmc 14

8 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
A supportsFromNative() 0 10 3
A fromMissingValue() 0 3 1
A toNative() 0 3 2
A supportsToNative() 0 3 1
A __construct() 0 7 3
A fromNative() 0 6 2
A __toString() 0 3 1
1
<?php
2
3
4
namespace Apie\TypeJuggling;
5
6
use Apie\TypeJuggling\Exceptions\CanNotCallFromNativeWithAbstractClassOrInterface;
7
use Apie\TypeJuggling\Exceptions\MissingValueException;
8
use Apie\TypeJuggling\Exceptions\OnlyValueObjectInterfaceSupportException;
9
use Apie\ValueObjects\ValueObjectInterface;
0 ignored issues
show
Bug introduced by
The type Apie\ValueObjects\ValueObjectInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
class AnotherValueObject implements TypeUtilInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $fieldName;
17
    /**
18
     * @var string
19
     */
20
    private $className;
21
22
    public function __construct(string $fieldName, ?string $className)
23
    {
24
        if ($className === null || !is_a($className, ValueObjectInterface::class, true)) {
25
            throw new OnlyValueObjectInterfaceSupportException($fieldName, $className);
26
        }
27
        $this->fieldName = $fieldName;
28
        $this->className = $className;
29
    }
30
31
    public function fromNative($input)
32
    {
33
        if (is_callable([$this->className, 'fromNative'])) {
34
            return $this->className::fromNative($input);
35
        }
36
        throw new CanNotCallFromNativeWithAbstractClassOrInterface($this->className);
37
    }
38
39
    public function toNative($input)
40
    {
41
        return $input instanceof ValueObjectInterface ? $input->toNative() : null;
42
    }
43
44
    public function fromMissingValue()
45
    {
46
        throw new MissingValueException($this->fieldName);
47
    }
48
49
    public function supports($input): bool
50
    {
51
        return is_a($input, $this->className);
52
    }
53
54
    public function supportsToNative($input): bool
55
    {
56
        return $this->supports($input);
57
    }
58
59
    public function supportsFromNative($input): bool
60
    {
61
        $res = is_callable([$this->className, 'fromNative']);
62
        if (!$res) {
63
            return false;
64
        }
65
        if (is_callable([$this->className, 'supportsFromNative'])) {
66
            return (bool) $this->className::supportsFromNative($input);
67
        }
68
        return true;
69
    }
70
71
    public function __toString()
72
    {
73
        return $this->className;
74
    }
75
}
76