Passed
Push — master ( 893bce...1901a3 )
by Dominik
02:01
created

LazyModelReference::jsonSerializableOrException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model\Reference;
6
7
use Chubbyphp\Model\ModelInterface;
8
use Chubbyphp\Model\ResolverInterface;
9
10
final class LazyModelReference implements ModelReferenceInterface
11
{
12
    /**
13
     * @var ResolverInterface
14
     */
15
    private $resolver;
16
17
    /**
18
     * @var string
19
     */
20
    private $modelClass;
21
22
    /**
23
     * @var string|null
24
     */
25
    private $id;
26
27
    /**
28
     * @var bool
29
     */
30
    private $resolved = false;
31
32
    /**
33
     * @var ModelInterface|null
34
     */
35
    private $initialModel;
36
37
    /**
38
     * @var ModelInterface|null
39
     */
40
    private $model;
41
42
    /**
43
     * @param ResolverInterface $resolver
44
     * @param string            $modelClass
45
     * @param string|null       $id
46
     */
47 8
    public function __construct(ResolverInterface $resolver, string $modelClass, string $id = null)
48
    {
49 8
        $this->resolver = $resolver;
50 8
        $this->modelClass = $modelClass;
51 8
        $this->id = $id;
52 8
    }
53
54 8
    private function resolveModel()
55
    {
56 8
        if ($this->resolved) {
57 4
            return;
58
        }
59
60 8
        $this->resolved = true;
61
62 8
        $this->initialModel = $this->resolver->find($this->modelClass, $this->id);
63 8
        $this->model = $this->initialModel;
64 8
    }
65
66
    /**
67
     * @param ModelInterface|null $model
68
     *
69
     * @return ModelReferenceInterface
70
     */
71 1 View Code Duplication
    public function setModel(ModelInterface $model = null): ModelReferenceInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73 1
        $this->resolveModel();
74
75 1
        $this->model = $model;
76 1
        $this->id = null !== $model ? $model->getId() : null;
77
78 1
        return $this;
79
    }
80
81
    /**
82
     * @return ModelInterface|null
83
     */
84 1
    public function getModel()
85
    {
86 1
        $this->resolveModel();
87
88 1
        return $this->model;
89
    }
90
91
    /**
92
     * @return null|string
93
     */
94 2
    public function getId()
95
    {
96 2
        return $this->id;
97
    }
98
99
    /**
100
     * @return ModelInterface|null
101
     */
102 1
    public function getInitialModel()
103
    {
104 1
        $this->resolveModel();
105
106 1
        return $this->initialModel;
107
    }
108
109
    /**
110
     * @return array|null
111
     */
112 3
    public function jsonSerialize()
113
    {
114 3
        $this->resolveModel();
115
116 3
        if (null === $this->model) {
117 1
            return null;
118
        }
119
120 2
        $this->jsonSerializableOrException();
121
122 1
        return $this->model->jsonSerialize();
0 ignored issues
show
Bug introduced by
The method jsonSerialize() does not seem to exist on object<Chubbyphp\Model\ModelInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
    }
124
125
    /**
126
     * @throws \LogicException
127
     */
128 2 View Code Duplication
    private function jsonSerializableOrException()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130 2
        if (!$this->model instanceof \JsonSerializable) {
0 ignored issues
show
Bug introduced by
The class JsonSerializable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
131 1
            throw new \LogicException(
132 1
                sprintf('Model %s does not implement %s', get_class($this->model), \JsonSerializable::class)
133
            );
134
        }
135 1
    }
136
}
137