Passed
Push — master ( 74b41a...e78beb )
by Dominik
07:41
created

ModelReference::jsonSerializableOrException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model\Reference;
6
7
use Chubbyphp\Model\ModelInterface;
8
9
final class ModelReference implements ModelReferenceInterface
10
{
11
    /**
12
     * @var string|null
13
     */
14
    private $id;
15
16
    /**
17
     * @var ModelInterface|null
18
     */
19
    private $model;
20
21
    /**
22
     * @param ModelInterface|null $model
23
     * @return ModelReferenceInterface
24
     */
25 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...
26
    {
27 1
        $this->model = $model;
28 1
        $this->id = null !== $model ? $model->getId() : null;
29
30 1
        return $this;
31
    }
32
33
    /**
34
     * @return ModelInterface|null
35
     */
36 1
    public function getModel()
37
    {
38 1
        return $this->model;
39
    }
40
41
    /**
42
     * @return null|string
43
     */
44 2
    public function getId()
45
    {
46 2
        return $this->id;
47
    }
48
49
    /**
50
     * @return ModelInterface|null
51
     */
52 1
    public function getInitialModel()
53
    {
54 1
        return null;
55
    }
56
57
    /**
58
     * @return array|null
59
     */
60 2
    public function jsonSerialize()
61
    {
62 2
        if (null === $this->model) {
63 1
            return null;
64
        }
65
66 1
        $this->jsonSerializableOrException();
67
68 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...
69
    }
70
71
    /**
72
     * @throws \LogicException
73
     */
74 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...
75
    {
76
        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...
77
            throw new \LogicException(
78
                sprintf('Model %s does not implement %s', get_class($this->model), \JsonSerializable::class)
79
            );
80
        }
81
    }
82
}
83