Completed
Push — master ( 50173f...31cc74 )
by Dominik
01:46
created

LazyModelCollection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 118
Duplicated Lines 8.47 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 10
loc 118
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolveModels() 0 13 2
A addModel() 0 8 1
A removeModel() 10 10 2
A setModels() 0 11 2
A getModels() 0 6 1
A getInitialModels() 0 6 1
A jsonSerialize() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model\Collection;
6
7
use Chubbyphp\Model\ModelInterface;
8
9
final class LazyModelCollection implements ModelCollectionInterface
10
{
11
    /**
12
     * @var \Closure;
13
     */
14
    private $resolver;
15
16
    /**
17
     * @var ModelInterface[]|array
18
     */
19
    private $initialModels;
20
21
    /**
22
     * @var ModelInterface[]|array
23
     */
24
    private $models;
25
26
    /**
27
     * @param \Closure $resolver
28
     */
29
    public function __construct(\Closure $resolver)
30
    {
31
        $this->resolver = $resolver;
32
    }
33
34
    private function resolveModels()
35
    {
36
        if (null === $this->resolver) {
37
            return;
38
        }
39
40
        $resolver = $this->resolver;
41
42
        $this->resolver = null;
43
44
        $this->setModels($resolver());
45
        $this->initialModels = $this->models;
46
    }
47
48
    /**
49
     * @param ModelInterface $model
50
     * @return ModelCollectionInterface
51
     */
52
    public function addModel(ModelInterface $model): ModelCollectionInterface
53
    {
54
        $this->resolveModels();
55
56
        $this->models[$model->getId()] = $model;
57
58
        return $this;
59
    }
60
61
    /**
62
     * @param ModelInterface $model
63
     * @return ModelCollectionInterface
64
     */
65 View Code Duplication
    public function removeModel(ModelInterface $model): ModelCollectionInterface
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...
66
    {
67
        $this->resolveModels();
68
69
        if (isset($this->models[$model->getId()])) {
70
            unset($this->models[$model->getId()]);
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * @param ModelInterface[]|array $models
78
     * @return ModelCollectionInterface
79
     */
80
    public function setModels(array $models): ModelCollectionInterface
81
    {
82
        $this->resolveModels();
83
84
        $this->models = [];
85
        foreach ($models as $model) {
86
            $this->addModel($model);
87
        }
88
89
        return $this;
90
    }
91
92
    /**
93
     * @return ModelInterface[]|array
94
     */
95
    public function getModels(): array
96
    {
97
        $this->resolveModels();
98
99
        return $this->models;
100
    }
101
102
    /**
103
     * @return ModelInterface[]|array
104
     */
105
    public function getInitialModels(): array
106
    {
107
        $this->resolveModels();
108
109
        return $this->initialModels;
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    public function jsonSerialize(): array
116
    {
117
        $this->resolveModels();
118
119
        $serializedModels = [];
120
        foreach ($this->models as $model) {
121
            $serializedModels[] = $model->jsonSerialize();
122
        }
123
124
        return $serializedModels;
125
    }
126
}
127