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

ModelCollection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 8
loc 88
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A addModel() 0 6 1
A removeModel() 8 8 2
A setModels() 0 9 2
A getModels() 0 4 1
A getInitialModels() 0 4 1
A jsonSerialize() 0 9 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 ModelCollection implements ModelCollectionInterface
10
{
11
    /**
12
     * @var ModelInterface[]|array
13
     */
14
    private $initialModels;
15
16
    /**
17
     * @var ModelInterface[]|array
18
     */
19
    private $models;
20
21
    /**
22
     * @param ModelInterface[]|array $models
23
     */
24
    public function __construct(array $models = [])
25
    {
26
        $this->setModels($models);
27
        $this->initialModels = $this->models;
28
    }
29
30
    /**
31
     * @param ModelInterface $model
32
     * @return ModelCollectionInterface
33
     */
34
    public function addModel(ModelInterface $model): ModelCollectionInterface
35
    {
36
        $this->models[$model->getId()] = $model;
37
38
        return $this;
39
    }
40
41
    /**
42
     * @param ModelInterface $model
43
     * @return ModelCollectionInterface
44
     */
45 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...
46
    {
47
        if (isset($this->models[$model->getId()])) {
48
            unset($this->models[$model->getId()]);
49
        }
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param ModelInterface[]|array $models
56
     * @return ModelCollectionInterface
57
     */
58
    public function setModels(array $models): ModelCollectionInterface
59
    {
60
        $this->models = [];
61
        foreach ($models as $model) {
62
            $this->addModel($model);
63
        }
64
65
        return $this;
66
    }
67
68
    /**
69
     * @return ModelInterface[]|array
70
     */
71
    public function getModels(): array
72
    {
73
        return $this->models;
74
    }
75
76
    /**
77
     * @return ModelInterface[]|array
78
     */
79
    public function getInitialModels(): array
80
    {
81
        return $this->initialModels;
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function jsonSerialize(): array
88
    {
89
        $serializedModels = [];
90
        foreach ($this->models as $model) {
91
            $serializedModels[] = $model->jsonSerialize();
92
        }
93
94
        return $serializedModels;
95
    }
96
}
97