Completed
Push — master ( 113f0b...9c2399 )
by Dominik
01:55
created

LazyModelCollection   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 152
Duplicated Lines 9.87 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 15
loc 152
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadModels() 0 13 2
A modelsWithIdKey() 15 15 3
A current() 0 6 1
A next() 0 6 1
A key() 0 6 1
A valid() 0 6 1
A rewind() 0 6 1
A setModels() 0 6 1
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
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 loadModels()
35
    {
36
        if (null !== $this->initialModels) {
37
            return;
38
        }
39
40
        $resolver = $this->resolver;
41
42
        $models = $this->modelsWithIdKey((array) $resolver());
43
44
        $this->initialModels = $models;
45
        $this->models = $models;
46
    }
47
48
    /**
49
     * @param ModelInterface[]|array $models
50
     *
51
     * @return ModelInterface[]|array
52
     */
53 View Code Duplication
    private function modelsWithIdKey(array $models): array
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...
54
    {
55
        $modelsWithIdKey = [];
56
        foreach ($models as $model) {
57
            if (!$model instanceof ModelInterface) {
58
                throw new \InvalidArgumentException(
59
                    sprintf('Model with index %d needs to implement: %s', ModelInterface::class)
60
                );
61
            }
62
63
            $modelsWithIdKey[$model->getId()] = $model;
64
        }
65
66
        return $modelsWithIdKey;
67
    }
68
69
    /**
70
     * @return ModelInterface
71
     */
72
    public function current()
73
    {
74
        $this->loadModels();
75
76
        return current($this->models);
77
    }
78
79
    /**
80
     * @return ModelInterface|false
81
     */
82
    public function next()
83
    {
84
        $this->loadModels();
85
86
        return next($this->models);
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function key()
93
    {
94
        $this->loadModels();
95
96
        return key($this->models);
97
    }
98
99
    /**
100
     * @return bool
101
     */
102
    public function valid()
103
    {
104
        $this->loadModels();
105
106
        return (bool) current($this->models);
107
    }
108
109
    public function rewind()
110
    {
111
        $this->loadModels();
112
113
        reset($this->models);
114
    }
115
116
    /**
117
     * @param ModelInterface[]|array $models
118
     */
119
    public function setModels(array $models)
120
    {
121
        $this->loadModels();
122
123
        $this->models = $this->modelsWithIdKey($models);
124
    }
125
126
    /**
127
     * @return ModelInterface[]|array
128
     */
129
    public function getModels(): array
130
    {
131
        $this->loadModels();
132
133
        return $this->models;
134
    }
135
136
    /**
137
     * @return ModelInterface[]|array
138
     */
139
    public function getInitialModels(): array
140
    {
141
        $this->loadModels();
142
143
        return $this->initialModels;
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public function jsonSerialize(): array
150
    {
151
        $this->loadModels();
152
153
        $serializedModels = [];
154
        foreach ($this->models as $model) {
155
            $serializedModels[] = $model->jsonSerialize();
156
        }
157
158
        return $serializedModels;
159
    }
160
}
161